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
A Knative Service is created. In this example, Quickly deploy a Knative application is used.
Knative Eventing and the GitHub add-on are deployed. For more information, see Manage Knative components.
A custom domain name is configured in Knative. For more information, see Configure a custom domain name.
Step 1: Create a GitHub token
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.
Enter a custom token name.
To trigger events from your public repositories and create webhooks for these repositories, select
repo:public_repo
andadmin:repo_hook
.
Use the following method to generate a random string as secretToken:
head -c 8 /dev/urandom | base64
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
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.
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
Run the following command to create a GitHub event source in the default namespace:
kubectl --namespace default apply github-source.yaml
Verify the result
In the GitHub repository, choose
and check the verified hook URL.NoteThe domain name must have an Internet Content Provider (ICP) number.
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.