The plugins I could find to integrate Jenkins with GitHub did not allow me to target a specific branch, or maybe they did, and I missed it. Furthermore, I wanted to have the freedom to manipulate the webhook GitHub sends when some action is done to a repository.
So, I built this simple Node.js service which acts as a conduit between GitHub and Jenkins.
Medium Article: https://trahulsam.medium.com/trigger-jenkins-jobs-only-when-pull-requests-are-made-to-specific-github-branches-189822a21ed
It captures the payload, allows you to manipulate it and then sends a POST request to Jenkins. For my use case, I have specified the POST request to be sent to Jenkins (triggering a pipeline) only when a pull request is made to a branch I specify in the service.
- Clone
JenkinsPRHandlerForSpecificBranch
repository into your project folder.https://github.com/TRahulSam1997/JenkinsPRHandlerForSpecificBranch
- Install Docker if you haven't got it already.
- cd
JenkinsPRHandlerForSpecificBranch
- Set the .env variables in
docker-compose.yml
PORT: 8080
GITHUB_TOKEN: undefined
JENKINS_BASE_URL: undefined
JENKINS_BASIC_AUTH: undefined
GITHUB_STATE: undefined
MESSAGE: undefined
JENKINS_TOKEN: undefined
SUBJECT_BRANCH: undefined
PORT: By defualt I've set the port to 8080.
GITHUB_TOKEN: Creating a Personal access token to update the commit status in GitHub.
JENKINS_BASE_URL: The Jenkins job you are targetting to receive the webhook content. Simply copy the URL of whatever job you wish to be triggered.
JENKINS_BASIC_AUTH: This service will create POST request. Therefore, you need to create a basic authentication token. Use Postman.
GITHUB_STATE: The service uses Create a commit status API request. You need to set the state of the status: error, failure, pending or success.
MESSAGE: You can set a message on the commit page, e.g. Jenkins Tests Passed.
JENKINS_TOKEN: Since this Jenkins build is being triggered remotely, you can specify an authentication token. It is just a unique identifier, and you can call it whatever you wish.
SUBJECT_BRANCH: This applies to my use case as I was trying to compare the base branch with this value. You can get rid of this part if you wish.
- run
docker-compose up -d