NealR
NealR

Reputation: 10709

Trigger Jenkins build when pull request is merged in Github

This should be an easy, out-of-the-box configuration in Jenkins but I haven't found anything straightforward on the internet. All I want to do is a trigger a build ONLY when a pull request i merged in our Github repo.

To start with, Github aggregates almost all activity around the pull request into one webhook (versus bitbucket which allows you to differentiate between actions).

enter image description here

On the Jenkins side I've seen posts point towards the Generic Webhook Plugin which allows you to ingest the json of the webhook and create variables, however from here it looks like those need to be used in a script in order to trigger/not trigger a build.

Github Pull Request Build is another popular plugin, but again there is nothing explicit that states "only trigger this build when a PR is merged" or even seems to give the option of looking for a specific value in the webhook json.

Unless there are other plugins out there I haven't found the best option (i.e. least configuration to just get the build started) is to configure the GitHub hook trigger for GITSCM polling in Jenkins and on the Github side send the webhook only on push events... however this isn't the exact behavior we're looking for.

enter image description here

Right now this is all being done via the UI, and it's been awhile since I've used Jenkins so maybe the declarative pipeline infrastructure has passed the UI by, but it seems like this should be much more intuitive. Can someone explain the easiest implementation they've found, using Jenkins and Github, to trigger a build ONLY when a pull request is merged to a specific branch?

Upvotes: 13

Views: 52306

Answers (5)

Komal Kadam
Komal Kadam

Reputation: 978

You can use github actions to trigger jenkins pipeline

https://github.com/features/actions https://github.com/marketplace/actions/trigger-jenkins-job

Upvotes: 0

ark
ark

Reputation: 1

To answer this "trigger a build ONLY when a pull request is merged to a specific branch?" question, the easiest way to do it is to use the Generic Webhook Trigger Plugin in Jenkins. This webhook lets you use the JSON payload objects as environment variables. Search "How to setup Generic Webhook Trigger Plugin" , once done configuring the webhook, look into the payload, you will find many different objects to use as variables in the pipeline. Using those variables you can trigger your builds. use this https://jsonpath.curiousconcept.com/ to find JSONPath.

Upvotes: 0

Omisha gupta
Omisha gupta

Reputation: 141

Tried looking everywhere, then figured out this solution myself

I'm assuming you've already configured webhook for jenkins, hence skipping that. The idea is to capture merge status and only trigger build if its true.

I'm using generic webhook trigger on jenkins and optional filters to achieve this.

This variable returns true if the build was merged.enter image description here

And the triggering the build only if this variable is true enter image description here

Upvotes: 8

VonC
VonC

Reputation: 1328692

No need for webhooks anymore, since you now how GitHub Actions (assuming you are using github.com, although the Actions are coming with GHE, GitHub Enterprise, in Beta, starting Sept. 2020).

As explained on this thread, you can trigger, on GitHub side, a job when a pull request is merged on master:

on:
  pull_request:
    branches:
      - master
    types: [closed]

jobs:
  <job id>:
    if: github.event.pull_request.merged == true
    steps: // the rest of the code

And that job can then use a GitHub Action like Trigger Jenkins Job for GitHub Actions, which will call your Jenkins and trigger one or several Jenkins jobs.

jobs:

  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
    - name: trigger single Job
      uses: appleboy/jenkins-action@master
      with:
        url: "http://example.com"
        user: "example"
        token: ${{ secrets.TOKEN }}
        job: "foobar"

After discussion with the OP and following the GitHub Actions tutorial, I confirm that:

  • Jenkins does not have to be started with Docker, it only has to be addressable from GitHub through its public URL;
  • The "trigger remote build" needs to be activated on Jenkins;
  • a token needs to be generated for the Jenkins user with the right to launch the Jenkins job;
  • A file triggerJenkinsBuild.yml (or any other name you want) must be created in the folder .github/workflows in your GitHub repository, with the two YAML sections mentioned above;
  • The "url:" field is just the base URL of the Jenkins instance, no extra path.

Upvotes: 7

Shaheer Akram
Shaheer Akram

Reputation: 765

you can configure the generic webhook trigger plugin to parse the payload json coming from the github.
first make sure that you have checked Pull requests in github webhook configuration

enter image description here

now you can look up for the merged key in webhook json, more details given at this url: https://developer.github.com/webhooks/event-payloads/#pull_request

first you need to check for the action key if it is closed then check for the merged key. according to the official documentation If the action is closed and the merged key is false, the pull request was closed with unmerged commits. If the action is closed and the merged key is true, the pull request was merged.

now configure the generic webhook to read they action and merged key from the payload in your jenkins pipline you can follow this example: https://github.com/jenkinsci/generic-webhook-trigger-plugin/blob/master/sandbox/multibranch.jenkinsfile

in your pipeline you can use these keys to check if you want to trigger the build or not.

for triggering the build on merge on a specific branch you can use environment variables in jenkins pipeline the env.BRANCH_NAME will tell you what branch you got the commit from

Upvotes: 0

Related Questions