Build or Run a Jenkins Job on Github Pull Request

In this post we will learn – How to trigger a Jenkins job or build when a github pull request is raised using Gtihub Pull Request Builder plugin.

Prerequisites

  1. Jenkins Server
  2. Github Account
  3. Github Project

Steps to Setup Automated Trigger build on Jenkins using Github Pull Request Builder plugin

  1. Download Github Pull Request Builder Plugin

    # Go to manage –>manage plugins
    # 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
    # once the plugin is install ,select the restart checkbox as shows in the image below.

    install-github-pull-request-builder

  2. Configure Github Pull Request Builder

    Once the plugin is installed, we will configure the plugin with github account as shown in the steps below:
    # Go to Manage Jenkins –> Configure System
    # Look for Github Pull Request Builder Section
    # Click on Add button and add credentials for github account. Once added, select the credentials from drop-down as highlighted in below image. You can click on Test Crdentials to test if credentials are working fine.
    # Click on Save

    configure-github-pull-request-builder-plugin

  3. Configure Github Repository Webhook

    Jenkins will receive Pull-Request (PR) events through github webhooks. Whenever a PR is raised, the github webhooks will come into action.
    # Go to Github repository –> Settings –> Webhooks –>Add Webhook
    # Enter jenkins url in the format –> http://<Jenkins-IP>:<port>/ghprbhook/
    # Change Content type to application/json
    # From “Let me select individual” section, select pull requests
    # Save/Add the webhook
    # Once saved, you will see a green tick if webhook is successful

    github-webhook
    github-webhook

  4. Create Jenkins Job and Configure to Run on Pull Request

    After creating webhook, we need to create a jenkins job which will be triggered when a PR will be raised on github.
    # Go to Jenkins Dashboard
    # Click on New Item
    # Enter a Project Name & Select Freestyle project and click on OK
    # In General tab, tick the checkbox of Github project and enter the url of the github repository

    jenkins-job
    # Scroll down, under Source Code Management select git.
    # Provide repository url, select credentials from the drop-down
    # Specify the branch to be build

    configure-jenkins-job

    # Under Build Trigger, tick Github Pull Request Builder
    # Tick Use github hooks for build triggering

    configure-jenkins-job

    # Once all above configurations are done, click on Save

This is all for the setup, now simply raise a PR on your github repository master branch. As soon as the PR is raised, github will trigger a webhook to jenkins and jenkins will start building the jenkins job.