How to trigger a Jenkins job automatically from a GitHub PR
2 min readSep 11, 2021
Two words answer is GitHub Webhook. For a detailed answer, keep reading.
In case you are not familiar with Webhooks, they are like reverse APIs where the application supporting the Webhook is sending a POST request to an endpoint with the details of the event in the payload. I recommend watching some YouTube videos about Webhooks if you are not familiar with them.
Now, coming back to the topic of the post, GitHub Webhook sends a notification to an external web server whenever a certain event happens in the repository or organization.
Let’s assume we want to trigger a Jenkins job that runs unit test cases whenever the developer opens/reopens/adds more commits to a PR.
- Install Generic Webhook Trigger plugin in Jenkins.
And add this block in your scripted Jenkinsfile:
triggers {
GenericTrigger(
genericVariables: [
[ key: 'action', value: '$.action'],
[ key: 'draft_pr', value: '$.pull_request.draft'],
[ key: 'pr_url', value: '$.pull_request.html_url'],
[ key: 'git_sha_statuses_url', value:
'$.pull_request.statuses_url']
], causeString: 'Triggered from PR: $pr_url token: '<some-random-string>', printContributedVariables: true,
printPostContent: true…