Setup Github Webhook for Jenkins on Amazon Linux (AWS)

setup-github-webhook

This article will help you automate tasks on basis of triggers generated github repository. For eg: A customer wants to run a job in jenkins whenever a push request is done on their development Github repository.

One of the way you can achieve this is using webhooks. In this article we will Setup Github Webhook for Jenkins on Amazon Linux instance

What are Webhooks?

Webhooks allow you to build or set up integrations which subscribe to certain events on GitHub.com. Inshort, whenever there are any changes done on your repositories a signal/payload will be broadcasted to a specific address/URL which you specify while creating webhooks. Webhooks can be used to update an external issue tracker, trigger CI builds, update a backup mirror, or even deploy to your production server. You’re only limited by your imagination.

Now before we start configuring webhooks we need to put few set of things in place first: 

  • A CI (Continuos Integration tool) – I will be using Jenkins in this case but it can be any CI tools of your choice.
  • A URL – In this case IP address and port to you Amazon Linux instance.
  • Proper security group port listening to 0.0.0.0/0 – In this case, I have installed jenkins hence I will open its by default port is 8080 in security group which should be accessible by github API so that the payload can be delivered. Again, it can be any port 80, 443 or any random TCP or UDP port.

Once, all of this things are taken care off, we are good to setup github webhook.

Step 1. Login and select a repository for which you would like to configure webhooks for..

Step 2. Go to settings option of repository

select-repo-setup-github-webhook

Step 3. Select “Webhooks” option and click on “Add”

nav-settings-setup-github-webhook 

Step 4. Fill out the details

  • Payload URL : This can be URL to which github API can send payloads to your instance. For eg. https://bhargavamin.com:8080 or http://53.22.21.123:80
  • Content type : This will detemine which format you want triggers to delivered to instance URL.
  • Secret text : A alphanumeric charaters. For eg. xyz1234xyz123xyz123, beware don’t confuse secret text with login secret text, both are different. 
  • Which events would you like to trigger this webhook?

config-webhook-setup-github-webhook

Once this is done, now we can test webhook. I’m going to test it by creating a pull request, you can do it by just pushing code to the repository for which webhook is configured.

Inorder to do this follow below procedure.

Step 5. Create a pull request  

You can only create pull request if you have multiple branch and would like to review code amongst them.  To know more about pull request visit: https://help.github.com/articles/about-pull-requests/

test-webhook-setup-github-webhook

 

Now, the moment you hit “Create new pull request” button, Github webhook will be triggered and a payload will be prepared and sent to the URL.

The status of it can be seen under webhooks tab in settings: 

Just select the webhook and scroll down until you see this.. 

go-to-payload

Understanding the notifications..

check-payload-delivery

if you get a red notification it means the payload was not delivered to URL, a green tick means payload was delivered to the URL. If you wan to resend same payload again then just expand the notification and click resend option.

Incase of unsuccessful delivery, make sure you have opened proper ports in security groups. Once it is configured you can now integrate this with jenkins to build jobs at an event.

This way you can achive automation seemlessly using webhooks and jenkins.

Hope this helped! If you have any queries feel free to write in comment sections below.

-Bhargav

References: https://developer.github.com/webhooks/

Blogger & Assc Cloud Architect

Site Footer