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.

    Build the sample code

    1. Download a copy of the code:

    2. Use Docker to build a container image for this service. Replace with your Docker Hub username in the following commands.

      1. export DOCKER_HUB_USERNAME=username
      2. # Build the container, run from the project folder
      3. docker build -t ${DOCKER_HUB_USERNAME}/gitwebhook-go .
      4. # Push the container to the registry
      5. docker push ${DOCKER_HUB_USERNAME}/gitwebhook-go
    3. Create a secret that holds two values from GitHub:

      • A 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.
      1. Follow the GitHub instructions to
      1. Base64 encode the access token:

        1. $ echo -n "45d382d4a9a93c453fb7c8adc109121e7c29fa3ca" | base64
        2. NDVkMzgyZDRhOWE5M2M0NTNmYjdjOGFkYzEwOTEyMWU3YzI5ZmEzY2E=
      2. Copy the encoded access token into github-secret.yaml next to personalAccessToken:.

      3. Create a webhook secret value unique to this sample, base64 encode it, and copy it into github-secret.yaml next to webhookSecret::

        1. $ echo -n "mygithubwebhooksecret" | base64
        2. bXlnaXRodWJ3ZWJob29rc2VjcmV0
      4. Apply the secret to your cluster:

      1. apiVersion: serving.knative.dev/v1
      2. kind: Service
      3. metadata:
      4. name: gitwebhook
      5. namespace: default
      6. template:
      7. spec:
      8. containers:
      9. - # Replace {DOCKER_HUB_USERNAME} with your actual docker hub username
      10. image: docker.io/{DOCKER_HUB_USERNAME}/gitwebhook-go:latest
      11. env:
      12. - name: GITHUB_PERSONAL_TOKEN
      13. valueFrom:
      14. secretKeyRef:
      15. name: githubsecret
      16. key: personalAccessToken
      17. - name: WEBHOOK_SECRET
      18. valueFrom:
      19. name: githubsecret
      20. key: webhookSecret
    4. Use kubectl to apply the service.yaml file.

      1. $ kubectl apply --filename service.yaml

      Response:

      1. service "gitwebhook" created
    5. Create a webhook in your GitHub repo using the URL for your gitwebhook service:

      1. Retrieve the hostname for this service, using the following command:

        Example response:

        1. NAME DOMAIN

        where MYCUSTOMDOMAIN is the domain that you set as your custom domain.

      2. Go to the GitHub repository for which you have privileges to create a webhook.

      3. Click Settings > Webhooks > Add webhook to open the Webhooks page.

      4. Set the Content type to application/json.

      5. Enter your webhook secret in Secret using the original base value that you set in webhookSecret (not the base64 encoded value). For example: mygithubwebhooksecret

      6. If you did not , click Disable under SSL Validation.

      7. Click Add webhook to create the webhook.

    Once deployed, you can inspect the created resources with kubectl commands:

    1. # This will show the Knative service that we created:
    2. kubectl get ksvc --output yaml
    3. # This will show the Route, created by the service:
    4. kubectl get route --output yaml
    5. # This will show the Configuration, created by the service:
    6. kubectl get configurations --output yaml
    7. # This will show the Revision, created by the Configuration:
    8. kubectl get revisions --output yaml

    Testing the service

    Now that you have the service running and the webhook created, send a Pull Request to the same GitHub repo where you added the webhook. If all is working right, you’ll see the title of the PR will be modified, with the text (looks pretty legit) appended the end of the title.

    To clean up the sample service:

    1. kubectl delete --filename service.yaml

    Feedback

    Was this page helpful?

    Sorry to hear that. Please tell us how we can improve.