GitHub webhook sample - Go
You must meet the following requirements to run this sample:
- Own a public domain. For example, you can create a domain with Google Domains.
- A Kubernetes cluster running with the following:
- Knative Serving must be installed. For details about setting up a Knative cluster, see the .
- Your Knative cluster must be configured to use your custom domain.
- You must ensure that your Knative cluster uses a static IP address:
- For Google Kubernetes Engine, see .
- For other cloud providers, refer to your provider’s documentation.
- An installed version of Docker.
- A to which you are able to upload your sample’s container image.
Download a copy of the code:
Use Docker to build a container image for this service. Replace with your Docker Hub username in the following commands.
export DOCKER_HUB_USERNAME=username
# Build the container, run from the project folder
docker build -t ${DOCKER_HUB_USERNAME}/gitwebhook-go .
# Push the container to the registry
docker push ${DOCKER_HUB_USERNAME}/gitwebhook-go
Create a secret that holds two values from GitHub:
- A personal access token that you will use to make API requests to GitHub.
- Ensure that you grant
read/write
permission in the repo for that personal access token.
- Follow the GitHub instructions to
Base64 encode the access token:
$ echo -n "45d382d4a9a93c453fb7c8adc109121e7c29fa3ca" | base64
NDVkMzgyZDRhOWE5M2M0NTNmYjdjOGFkYzEwOTEyMWU3YzI5ZmEzY2E=
Copy the encoded access token into
github-secret.yaml
next topersonalAccessToken:
.Create a webhook secret value unique to this sample, base64 encode it, and copy it into
github-secret.yaml
next towebhookSecret:
:$ echo -n "mygithubwebhooksecret" | base64
bXlnaXRodWJ3ZWJob29rc2VjcmV0
Next, update the
service.yaml
file in the project to reference the tagged image from step 1.apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: gitwebhook
namespace: default
template:
spec:
containers:
- # Replace {DOCKER_HUB_USERNAME} with your actual docker hub username
image: docker.io/{DOCKER_HUB_USERNAME}/gitwebhook-go:latest
env:
- name: GITHUB_PERSONAL_TOKEN
valueFrom:
secretKeyRef:
name: githubsecret
key: personalAccessToken
- name: WEBHOOK_SECRET
secretKeyRef:
name: githubsecret
key: webhookSecret
Use
kubectl
to apply theservice.yaml
file.$ kubectl apply --filename service.yaml
Response:
service "gitwebhook" created
Create a webhook in your GitHub repo using the URL for your
gitwebhook
service:Retrieve the hostname for this service, using the following command:
Example response:
NAME DOMAIN
where
MYCUSTOMDOMAIN
is the domain that you set as your .Click Settings > Webhooks > Add webhook to open the Webhooks page.
Enter the Payload URL as
http://{DOMAIN}
, where{DOMAIN}
is the address from thekubectl get ksvc gitwebhook
command. For example:http://gitwebhook.default.MYCUSTOMDOMAIN.com
Set the Content type to
application/json
.Enter your webhook secret in Secret using the original base value that you set in
webhookSecret
(not the base64 encoded value). For example:mygithubwebhooksecret
If you did not enabled TLS certificates, click Disable under SSL Validation.
Click Add webhook to create the webhook.
Once deployed, you can inspect the created resources with kubectl
commands:
# This will show the Knative service that we created:
kubectl get ksvc --output yaml
# This will show the Route, created by the service:
kubectl get route --output yaml
# This will show the Configuration, created by the service:
kubectl get configurations --output yaml
# This will show the Revision, created by the Configuration:
To clean up the sample service:
kubectl delete --filename service.yaml