Building projects based on pull request is something you cannot avoid in CI/CD pipelines. Nowadays every team does several deployments/operations per day and lots of builds have to happen in this process.
Also, the teams work on the same repo collaborating code require faster code integrations. So it is better to have an automated build process that kicks off the CI/CD pipeline on a pull request rather than manually triggering the jobs.
Trigger Builds Automatically On Github Pull Request
In this tutorial, we will explain how to configure a pull request based build trigger on Jenkins using Github webhooks and Github pull request builder plugin.
Note: Multipbranch Pipeline is the best way to achieve Jenkins pull request based workflow as it is natively available in Jenkins. Check out this article on the multibranch pipeline for setup and configuration.
Install Github Pull Request Builder Plugin
- Go to
Manange Jenkins --> Manage Plugins
Click on the
available
tab at the top and search forGithub Pull Request Builder
. Select the plugin using the checkbox and clickInstall without restart
as shown in the image below.- Once the plugin is installed, select the restart checkbox as shown in the image below.
Github Pull Request Builder Configuration
Once Jenkins is restarted, follow the steps given below for configuring the plugin with your GitHub account.
- Head over to
Manange Jenkins --> Configure System
- Find “GitHub Pull Request Builder” section and click add credentials.
- Enter your Github username and password and add it.
- You can test the Github API connection using the test credentials button. It should show “connected” as shown below. Save the configuration after testing the API connection.
Github Repo Webhook Configuration
For Jenkins to receive PR events through the pull request plugin, you need to add the Jenkins pull request builder payload URL in the Github repository settings.
- Go to Github repository settings, and under webhooks, add the Jenkins pull request builder payload URL. It has the following format
http://<Jenkins-IP>:<port>/ghprbhook/
If you need just the PR triggers, you can select the “Let me select individual events” option and select just the “Pull requests” option. Save the webhook after selecting the required events.
- Once saved, go back to the webhook option and see if there is a green tick. It means Github is able to successfully deliver the events to the Jenkins webhook URL.
Job Configuration for Automated Pull Request Builds
Lets get started with the build job configuration for PR plugin.
- Under the General tab, select Github project option and enter the Github repo URL for which you want the PR builds without .git extension as shown below.
- Click advanced option and enable automatic PR build trigger and add the target branches you would raise the PR for.
- Add your pipeline build steps and save the configuration.
- Now raise a PR against the whitelisted branch you have given in the Jenkins PR trigger settings. You should see the job getting triggered on Jenkins.
Other Jenkins PR based Build Workflows
Github Pull request builder plugin is not actively developed as the same functionality is being provided by multi-branch pipelines and Github organisation project.
There is also a Generic Webhook Plugin that can be used to trigger Jenkins jobs on a Pull Request.
Also, you can write custom API endpoints that accept Github webhooks and process PR requests to trigger Jenkins job remotely. Custom APIs help only when the native Jenkins functionalities are not providing the workflow you are looking for.
15 comments
i have follow same steps but not working
in github pass request I have see in webhook but jenkins not able to get that
@ronak Multipbranch Pipeline is the best way to achieve Jenkins pull request based workflow as it is natively available in Jenkins
What if I am just testing the jenkins ? It is not deployed and it isn’t reachable over the public Internet. Is there still a way to configure this ?
Not using Github. But if you setup Gitlab or other git tools locally, you can do that. But its an overkill
you can use ngrok or socketxp
Is it possible to get the pull request number during the build? I want to use in the execute shell section.
yes. It is. ghprbPullId variable should be having the PR number
Following variables are supported
ghprbActualCommit
ghprbActualCommitAuthor
ghprbActualCommitAuthorEmail
ghprbPullDescription
ghprbPullId
ghprbPullLink
ghprbPullTitle
ghprbSourceBranch
ghprbTargetBranch
ghprbCommentBody
sha1
@devopscube:disqus Thanks!.
Also, is it possible to update the PR before starting the build.
For example, let’s say my PR branch is not updated with the base branch (on which the PR has been raised). Can this plugin update the PR with the base branch as in merge base branch into PR branch and then checkout the PR branch and start it’s build?
There’s one ‘Merge before build’ option available but that doesn’t achieve this.
I created a pull request on my repo but it did not trigger any jenkins job,However i follwed the same step.
is the github webhook reaching jenkins? you can check this from your GitHub webhook settings…
I’m confused, GPRB is no longer maintained and hasn’t been for awhile, but this article is not that old. The GPRB page says to use GitHub Branch Source plugin instead, but I can’t find an explanation of how to use GBS to do the same thing
when i submit a PR git should trigger job , create a branch and merge code from origin branch to upstream branch ensure the conflicts also , perform the build on temp branch ensure the build works with latest and merged code. and Post build it should run Unit Test cases
and after Post Unit Test Case it should run sanity test
Can you tell me how we do .
Jenkins triggering when create pull request as well jenkins triggering the build when user git commit the code. How to avoid this? I dont want jenkins trigger for each commit.
I have the same problem.
I opened a pull request on my repo but it did not trigger any jenkins job,