Monday, 17 August 2020

Setting GitHub hooks in Jenkins

While setting up a Freestyle project in Jenkins we may need to set Github webhooks in Jenkins so that Jenkins will get notified each time there is a push and subsequently fetch the code and build it. Basically the procedure is we set up a URL in Github and it will send a post request to this URL when someone pushes some code. There are 2 ways to get this URL in Jenkins

I have assumed that Jenkins is listening to the default port 8080

Getting the Notification URL

Method 1

Jenkins provides a common URL which can be used as the notification URL. By default the URL will be
http://<IP ADDRESS>:8080/github-webhook/
You can override this URL by going to Manage Jenkins -> Configure System and under Github click Advanced and check "Specify another hook URL for GitHub configuration"
when creating your freestyle project you must specify the build trigger as follows


Method 2

This method though a bit complex allows you to get a URL for each project (you can send a post request to this link from anywhere even from postman to start a build). On the left side, panel click on People (If nothing shows up click on All People option)

* Click on your User ID and click on Configure (on the left side)
* Click on Add new token under API Token. Give it a name and click Generate. Copy the generated token to somewhere safe (NOTE you won't be able to retrieve it later)
* Save

when creating your freestyle project you must specify the build trigger as follows (Set your secret Authentication Token)


The Notification URL will be as follows
http://<JENKINS USERNAME>:<API TOKEN>@<IP ADDRESS>:8080/job/<PROJECT NAME>/build?token=<AUTHENTICATION TOKEN>

Setting the URL in GitHub

To set the URL in Github, go to your project


Confirm it is you by entering your password when asked


That's it now Github will send a post request to the mentioned link each time some code is pushed

No comments:

Post a Comment

Setting GitHub hooks in Jenkins

While setting up a Freestyle project in Jenkins we may need to set Github webhooks in Jenkins so that Jenkins will get notified each time th...