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

Prerequisites

Create a service

  1. Log on to the Container Service console.
  2. In the left-side navigation pane under Container Service - Kubernetes, choose Knative > Services. The Knative Services page appears.
  3. In the upper-right corner, click Create Service.
  4. Set the following parameters: cluster, namespace, service name, image name, and image version.
    Note
    In this example, set the parameters as follows:
    • Service Name: The name of the service. In this example, enter github-event-display.
    • Image Name: Click Select Image. In the dialog box that appears, select an image and click OK. You can also enter a private registry URL to specify the image. The format is as follows: domainname/namespace/imagename:tag. In this example, enter registry.cn-shanghai.aliyuncs.com/knative-release/eventing-sources-event_display.
    • Image Version: Click Select Image Version to select the image version. If this parameter is not set, the latest image version is used. In this example, enter bf45b3eb1e7fc4cb63d6a5a6416cf696295484a7662e0cf9ccdf5c080542c21d.
    For more information about the other parameters, see this document.
  5. Click Create.

Create a GitHub token

  1. Create a personal access token.
    Personal access tokens are used to access GitHub APIs. For more information, see Personal access tokens.
    The following example creates a token named GitHubSource Sample.Create a GitHub token
    Note To trigger events from your public repositories and create webhooks for those repositories, select the repo:public_repo and admin:repo_hook scopes.

    You can enter a custom token name.

  2. Connect to Kubernetes clusters through kubectl.
  3. Use the following method to generate a random string as the secretToken.
    head -c 8 /dev/urandom | base64
  4. Update githubsecret.yaml with these values.
    If your access token is personal_access_token_value and you set secretToken as asdfasfdsaf, githubsecret.yaml is as follows: 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 the GitHub token.
    kubectl --namespace default apply githubsecret.yaml

Create an event source for GitHub events

To receive GitHub events, you need to create a GitHubSource.

  1. Connect to Kubernetes clusters through kubectl.
  2. Create a github-source.yaml file with the following content:
    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 the GitHubSource in the default namespace.
    kubectl --namespace default apply github-source.yaml

Result

In your GitHub repository, choose Settings > Webhooks to see whether a hook URL with a green check mark is displayed.
Note You need to obtain an ICP filing for the domain name. Otherwise, it would be inaccessible.
Run the following command create apull request in your GitHub repository. GitHub events will be generated.
kubectl --namespace default get pods
kubectl --namespace default logs github-event-display-XXXX user-container
In the Knative Eventing system, you can see event logs as follows:
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-46e3c8ad2b4d
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: 127.0.0.1, 127.0.0.1
X-Forwarded-Proto: http
X-Request-Id: 8a2201af-5075-9447-b593-ec3a243aff52

{"action":"opened","number":1,"pull_request": ...}