Jenkins Automated Build Trigger On Github Pull Request

Jenkins Automated Build Trigger On Github Pull Request

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

  1. Go to Manange Jenkins --> Manage Plugins
  2. Click on the available tab at the top and search for Github Pull Request Builder. Select the plugin using the checkbox and click Install without restart as shown in the image below.github pull request builder plugin
  3. Once the plugin is installed, select the restart checkbox as shown in the image below.jenkins plugin restart

Github Pull Request Builder Configuration

Once Jenkins is restarted, follow the steps given below for configuring the plugin with your GitHub account.

  1. Head over to Manange Jenkins --> Configure System
  2. Find "GitHub Pull Request Builder" section and click add credentials.Jenkins - Github connectivity
  3. Enter your Github username and password and add it. jenkins - github credentials
  4. 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.pull request builder credentials configuration

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.

  1. 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/
    github jenkins webhools settings

    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.add webhook in github for jenkins

  2. 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.github activated webhook

Job Configuration for Automated Pull Request Builds

Lets get started with the build job configuration for PR plugin.

  1. 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.jenkins github repo URL
  2. Click advanced option and enable automatic PR build trigger and add the target branches you would raise the PR for.PR build target branches
  3. Add your pipeline build steps and save the configuration.
  4. 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.

About the author
devopscube

devopscube

Since 2014, we're a thriving DevOps community dedicated to helping DevOps Engineers grow with high-quality, in-depth articles, comprehensive learning paths, expert insights, and practical resources.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to DevOpsCube – Easy DevOps, SRE Guides & Reviews.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.