GitHub Source example shows how to wire GitHub events for consumption by a Knative Service.
Clone the repository and run the commands below from this sample directory:
git clone https://github.com/knative/docs.git knative-docs
cd knative-docs/code-samples/eventing/github-source- Set up Knative Serving.
- Ensure Knative Serving is configured with a domain name that allows GitHub to call into the cluster.
- You must ensure that your Knative cluster uses a static IP address refer to your provider's documentation.
- Set up Knative Eventing.
GitHub Event source lives in knative-extensions/eventing-github. Head to the releases page, find the latest release with the github.yaml
artifact and replace <RELEASE> with the version tag:
kubectl apply -f https://github.com/knative-extensions/eventing-github/releases/download/<RELEASE>/github.yamlCheck that the manager is running:
kubectl -n knative-sources get pods --selector control-plane=github-controller-managerTo verify the GitHub source is working, create a simple Knative
Service that dumps incoming messages to its log.
service.yaml defines this Service using the Eventing
event_display receiver image.
Enter the following command to create the service from service.yaml:
kubectl --namespace default apply --filename service.yamlCreate a personal access token for GitHub that the GitHub source can use to register webhooks with the GitHub API. Also decide on a secret token that your code will use to authenticate the incoming webhooks from GitHub (secretToken).
The token can be named anything you find convenient. The Source
requires repo:public_repo and admin:repo_hook, to let it fire
events from your public repositories and to create webhooks for those
repositories. Copy and save this token; GitHub will force you to
generate it again if misplaced.
Here's an example for a token named "GitHubSource Sample" with the recommended scopes:
Update githubsecret.yaml with those values: set
stringData.accessToken to your generated access token and
stringData.secretToken to your chosen webhook secret. Keep the secret name
and keys unchanged because the source references them. Do not commit real tokens.
Hint: you can makeup a random secretToken with:
head -c 8 /dev/urandom | base64Then, apply the githubsecret using kubectl:
kubectl --namespace default apply --filename githubsecret.yamlIn order to receive GitHub events, you have to create a concrete Event
Source for a specific namespace. Be sure to replace the
ownerAndRepository value with a valid GitHub public repository owned
by your GitHub user.
Edit github-source.yaml, which subscribes to
pull_request events, references the token Secret, and sends events to
github-message-dumper.
If using GitHub Enterprise, add spec.githubAPIURL with your enterprise API
endpoint, for example https://github.mycompany.com/api/v3/.
Then, apply the file using kubectl:
kubectl --namespace default apply --filename github-source.yamlVerify the GitHub webhook was created by looking at the list of webhooks under the Settings tab in your GitHub repository. A hook should be listed that points to your Knative cluster with a green check mark to the left of the hook URL, as shown in the following image:
Create a pull request in your GitHub repository. We will verify that the GitHub events were sent into the Knative eventing system by looking at our message dumper function logs.
kubectl --namespace default get pods -l serving.knative.dev/service=github-message-dumper
kubectl --namespace default logs -l serving.knative.dev/service=github-message-dumper -c user-container --tail=50You should see a CloudEvent similar to the following (IDs, source, and payload will depend on your repository and pull request):
☁️ cloudevents.Event
Context Attributes,
specversion: 1.0
type: dev.knative.source.github.pull_request
source: https://github.com/someuser/somerepo
id: a8d4cf20-e383-11e8-8069-46e3c8ad2b4d
datacontenttype: application/json
Data,
{
"action": "opened",
"number": 1,
"pull_request": { ... }
}
You can remove the Github webhook by deleting the Github source:
kubectl --namespace default delete --filename github-source.yamlSimilarly, you can remove the Service and Secret via:
kubectl --namespace default delete --filename service.yaml
kubectl --namespace default delete --filename githubsecret.yaml

