All Products
Search
Document Center

Container Service for Kubernetes:Use GitHub events in Knative

Last Updated:Nov 19, 2024

You can create a GitHub event source in Knative to receive events from GitHub. This event source is suitable for scenarios such as continuous integration and continuous delivery (CI/CD), data synchronization, and automated testing.

Prerequisites

Step 1: Create a GitHub token

  1. Create a personal access token to call the GitHub API.

    For more information, see Personal access tokens.

    The following figure shows how to create a token named GitHubSource Sample.

    image.png

    创建github token

    • Enter a custom token name.

    • To trigger events from your public repositories and create webhooks for these repositories, select repo:public_repo and admin:repo_hook.

  2. Use the following method to generate a random string as secretToken:

    head -c 8 /dev/urandom | base64
  3. Update the githubsecret.yaml file.

    If the generated token is personal_access_token_value, you must set the secretToken field. Example:

    apiVersion: v1
    kind: Secret
    metadata:
      name: githubsecret
    type: Opaque
    stringData:
      accessToken: GitHubSourceSample
      secretToken: rVzY0DQ5AM8=
    apiVersion: v1
    kind: Secret
    metadata:
      name: githubsecret
    type: Opaque
    stringData:
      accessToken: personal_access_token_value
      secretToken: asdfasfdsaf
  4. Run the following command to create a GitHub token:

    kubectl apply -f githubsecret.yaml --namespace default

Step 3: Create a GitHub event source

You can create a GitHub event source to receive events generated by GitHub.

  1. Create a file named github-source.yaml and copy the following content to the file:

    apiVersion: sources.eventing.knative.dev/v1alpha1
    kind: GitHubSource
    metadata:
      name: githubsourcesample
    spec:
      eventTypes:
        - pull_request
      ownerAndRepository: <YOUR USER>/<YOUR REPO> # Replace <YOUR USER> with your GitHub username and <YOUR REPO> with your GitHub repository name. 
      accessToken:
        secretKeyRef:
          name: githubsecret
          key: accessToken
      secretToken:
        secretKeyRef:
          name: githubsecret
          key: secretToken
      sink:
        apiVersion: serving.knative.dev/v1alpha1
        kind: Service
        name: github-event-display
  2. Run the following command to create a GitHub event source in the default namespace:

    kubectl --namespace default apply github-source.yaml

Verify the result

  1. In the GitHub repository, choose Settings > Webhooks and check the verified hook URL.

    Note

    The domain name must have an Internet Content Provider (ICP) number.

  2. Run the following command in the GitHub repository to create a pull request. Then, an event is triggered.

    kubectl --namespace default get pods
    kubectl --namespace default logs github-event-display-XXXX user-container

    In Knative Eventing, you can view event details that are similar to the following content:

    2018/11/08 18:25:34 Message Dumper received a message: POST / HTTP/1.1
    Host: github-event-display.knative-demo.svc.cluster.local
    Accept-Encoding: gzip
    Ce-Cloudeventsversion: 0.1
    Ce-Eventid: a8d4cf20-e383-11e8-8069-46e3c8ad****
    Ce-Eventtime: 2018-11-08T18:25:32.819548012Z
    Ce-Eventtype: dev.knative.source.github.pull_request
    Ce-Source: https://github.com/someuser/somerepo/pull/1
    Content-Length: 21060
    Content-Type: application/json
    User-Agent: Go-http-client/1.1
    X-B3-Parentspanid: b2e514c3dbe94c03
    X-B3-Sampled: 1
    X-B3-Spanid: c85e346d89c8be4e
    X-B3-Traceid: abf6292d458fb8e7
    X-Envoy-Expected-Rq-Timeout-Ms: 60000
    X-Envoy-Internal: true
    X-Forwarded-For: 12*.*.*.*, 12*.*.*.*
    X-Forwarded-Proto: http
    X-Request-Id: 8a2201af-5075-9447-b593-ec3a243a****
    
    {"action":"opened","number":1,"pull_request": ...}

References

You can also use the EventBridge event source to trigger events. For more information, see Use EventBridge to trigger Knative Services to consume events.