Create your own CI/CD pipeline for repositories on Github — Step 1: Add webhooks
There are lots of reasons that you need a CI/CD pipeline. For example, the reason might be building a nightly release for testing, running test cases or lint tools on your source code.
Currently, as I am studying at The University of Adelaide and my team was asked to develop a CI/CD pipeline for my course. We decided using webhooks
to trigger our CI/CD pipeline.
How it works.
Webhooks can be associated with repositories. When events (such as push
) happen in a repository, Github will make a POST request with payload to the specified URL defined in the webhook. Once the endpoint receives the notification, it can start the pipeline.
Add webhook
Let’s use my repository as an example.
This URL https://github.com/wxw-matt/nginx-cors/settings/hooks/new will lead me to the page to add a webhook, where wxw-matt
is my Github account and nginx-cors
is the repository name. You can change it to your account name and repository.
Or you can find this page by following the path: Settings->Webhooks-> Add webhook.
Note the application/json
is selected for Content type
, which will make the payload in JSON
format.
After creating a webhook and starting your server end which listens for the notifications, you can receive the notifications when events happen in a repository. The payload can be found in Manage webhook
page and looks like:
In your backend, it may look like this:
Now you can do your job in your backend, such as
- Clone or pull the repo from Github
- Run testing, building or linting tasks on your code (can be done by using pre-built Docker images for convenience)
- Collect logs (either from Docker containers or local log files) and report to the developer (e.g., via email)
In this article, I demonstrated how webhooks trigger a pipeline and how to add a webhook. In the next article, I will add some to the backend to show how to the rest of the pipeline.