Set up GitHub webhooks to trigger new builds of Kubernetes apps
With every push to a repo, trigger new builds on Red Hat OpenShift on IBM Cloud
In this tutorial, you learn how to set up a GitHub webhook to trigger a new build of a Kubernetes app every time a push is made on the GitHub repo. Examples in this tutorial use Red Hat OpenShift on IBM Cloud and the Node.js Application Modernization Source-to-Image Code Pattern for the application.
After completing this tutorial you will be able to accomplish the following tasks:
- Deploy an application to an OpenShift cluster.
- Set up a GitHub webhook for an application.
- Verify that the webhook works.
- View and modify the webhook.
To complete this tutorial you need to set up the following accounts:
- A GitHub account
- An IBM Cloud account
- A Red Hat OpenShift cluster created on IBM Cloud
Completing this tutorial should take about 30 minutes.
1. Fork the repo
Begin by forking the
node-s2i-openshift repo at github.com/IBM/node-s2i-openshift.
2. Set up the OpenShift project
After you create your OpenShift cluster on IBM Cloud, you see the following OpenShift dashboard:
- Click the Create Project button to add a new project, and give it a name, for example,
- Click Browse Catalog.
- Select Node.js.
- Click Next.
- Click Advanced Options.The GitHub URL used in this tutorial is
https://github.com/moficodes/node-s2i-openshift/, but you might want to use your GitHub URL by forking the repo. You need write permissions in the repo that you decide to use.The following screen capture shows an example:
- Scroll down to the bottom and click Create.After the project is created, OpenShift creates a GitHub webhook payload URL. You can click the button to copy the URL.It is in the following structure:
https://c100-e.us-east.containers.cloud.ibm.com:32376/apis/build.openshift.io/v1/namespaces/health-webhook/buildconfigs/health/webhooks/<SECRET>/githubAlso note the
<SECRET>portion of the URL. You need that value in a later step.Click the Overview tab and wait for the build to finish. After the build is finished, you see a route is created. You can click that link to see that the app is available. Now that the app is up and running, you set up the webhook.
3. Set up a webhook
- In your GitHub repo, go to Settings.
- Select Webhooks and then click Add webhook.
- Fill in the form with the payload URL and Secret from the previous section. For the content type, select application/json. Click Add webhook.
If you make any push in your repo, it kicks off a new build. The following example makes some minor UI updates and triggers the webhook. From the GitHub UI, you can see the recent deliveries that completed. The first one was when you set it up. The second one is the result of the latest commit with the UI changes.
4. Verify the new OpenShift build
Back on the OpenShift dashboard, from the left menu, click Builds > Builds.
You can see the last build is
#2 and verify that the changes are there by clicking on the link to see the app. See the following example:
5. Retrieve the webhook
If you want to retrieve the webhook information, you can always go back to the build.
- Click on the build name.
- Click Configuration and copy the GitHub webhook URL.
6. Change the webhook
If you want to change the GitHub settings for build, click Actions > Edit.
You can change the GitHub repo settings from here. For example, you can change the context or branch to build from.
In the triggers section you can find the secret that is set up for the GitHub webhook, as shown in the following screen capture:
You have now successfully deployed an application to OpenShift using the Source-to-Image (S2I) toolkit, added a GitHub webhook to the application that triggers a new build of the application when a new push is made on the GitHub repo, and learned where to view and modify the webhook.
With Red Hat Openshift on IBM Cloud, you can try it out on your own environment. To get started, see the Red Hat Openshift on IBM Cloud documentation.