Trigger Jenkins job when a pull request is merged to a branch

This is our tool set:

jenkins: Open source Jenkins running on AWS server - 2.107.2

Github: 2.13.3 version

Requirement:

We need to create a GitHub hook in such a way that, if anyone merges the branch (create pull request and merge it), then it should trigger the Jenkins job.We do not use multibranch pipeline, we use just pipeline jobs i.e we use jenkisnfile to run our jobs.

so, i have enabled the “PullRequestEvent” in github webhooks. Then i have enabled scm polling in Jenkins job for that particular job.But, the issue is the job is getting triggered for a commit to the xyz branch and also if we merge the pull request. But, the expected behavior is, the job should be triggered only for the merge.

https://developer.github.com/v3/activity/events/types/#pullrequestevent

The “action” description makes it pretty clear:

The action that was performed. Can be one of “assigned”, “unassigned”, “review_requested”, “review_request_removed”, “labeled”, “unlabeled”, “opened”, “edited”, “closed”, or “reopened”. 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.

Thanks.

But how do i do that , in UI i only have option to chekcbox that event.

where to do the settings or how to get jenkins notfy about that.

You could write something in the middle.

Github Web hook -> [Check if merged: true] -> Jenkins Webhook trigger “Trigger builds remotely”

I have enabled this “Jenkins Webhook trigger “Trigger builds remotely””

Now what token should i give there ? and where/how to do the below thing.I am very new to github please help.

"Github Web hook -> [Check if merged: true] -> "

Hi,

I don’t know if you still need help but at least the next person to look on this page might benefit. I have just managed to do this.

So I literally just set up the branch I wanted to trigger a Jenkins build with the ‘Just push the event’ option and the gitscm option on Jenkins (like you normally set up auto-build for a push event).

Turns out it works when a PR is merged into the branch in question without any additional settings.

Hope this helps,

Dario

6 Likes

With this setup, every commit will trigger the Jenkins, not what we want.

This approach works: https://stackoverflow.com/questions/31407332/how-to-process-a-github-webhook-payload-in-jenkins

1 Like

@jironghu wrote:

This approach works: https://stackoverflow.com/questions/31407332/how-to-process-a-github-webhook-payload-in-jenkins

 

Agreed. This looks promising. Going to try it out.

You can use https://wiki.jenkins.io/display/JENKINS/Generic+Webhook+Trigger+Plugin and configure a rule to do the check.

1 Like

Hi dgabrielm,

Can you please share complete details of your configuration step by step ?

Thanks in advance.

I got the same requirement, can you elaborate the solution mentioned in the above.

that would be very helpful for me

Can you please provide any sample script for triggering a Jenkins job based on pull request approval ?

pls check this repo
https://github.com/pedrodparkes/JenkinsMergedPRBuilder

name: Trigger Jenkins job when a pull request is merged to a branch
on:
    pull_request_review:
      types: [closed]
jobs:
    triggerJenkins:
        name: Trigger Jenkins
        runs-on: ubuntu-latest
        steps:
 if: github.event.pull\_request.merged == true - name: trigger
                run: curl -X POST https://githubUsername:${{secrets.JenkinsToken}}@cloudbees.xyz.com/teams/job/repositoryName/job/jenkinsJobName/build

But here it will scan the repository in jenkins. So if you can search how to trigger specific jenkins job will be great help. thanks