This topic describes how to use Knative to manage GitHub events.

Prerequisites

Step 1: Create a Knative Service

For more information, see Create a Knative Service.

Create a GitHub token

  1. Create a personal access token.
    A personal access token is required when you call the GitHub API. For more information, see Personal access tokens.
    The following figure shows how to create a token named GitHubSource Sample.Create a GitHub token
    Note To trigger events from your public repositories and create webhooks for those repositories, select repo:public_repo and admin:repo_hook.

    You can enter a custom token name.

  2. Use kubectl to connect to an ACK cluster.
  3. Use the following method to generate a random string as secretToken:
    head -c 8 /dev/urandom | base64
  4. Update the githubsecret.yaml file.
    If the generated token is personal_access_token_value, you must set the secretToken field. The following code block is an example:
    apiVersion: v1
    kind: Secret
    metadata:
      name: githubsecret
    type: Opaque
    stringData:
      accessToken: personal_access_token_value
      secretToken: asdfasfdsaf
  5. Run the following command to create a GitHub token:
    kubectl --namespace default apply githubsecret.yaml

Create a GitHub event source

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

  1. Use kubectl to connect to an ACK cluster.
  2. Create the github-source.yaml file 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>
      accessToken:
        secretKeyRef:
          name: githubsecret
          key: accessToken
      secretToken:
        secretKeyRef:
          name: githubsecret
          key: secretToken
      sink:
        apiVersion: serving.knative.dev/v1alpha1
        kind: Service
        name: github-event-display
  3. Run the following command to create a GitHub event source in the default namespace:
    kubectl --namespace default apply github-source.yaml

Result

In the GitHub repository, choose Settings > Webhooks and check whether a verified hook URL is displayed.
Note The domain name must have an Internet Content Provider (ICP) number. Otherwise, the domain name is inaccessible.
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": ...}