Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Copilot Lvl 3
Message 1 of 15

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.

14 Replies
Highlighted
Pilot Lvl 2
Message 2 of 15

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

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.

Highlighted
Copilot Lvl 3
Message 3 of 15

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

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.

Highlighted
Pilot Lvl 2
Message 4 of 15

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

You could write something in the middle.

 

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

Highlighted
Copilot Lvl 3
Message 5 of 15

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

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 ] -> "

 
Highlighted
Pilot Lvl 2
Message 6 of 15

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

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

Highlighted
Copilot Lvl 2
Message 7 of 15

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

Can you please provide any sample script for triggering a Jenkins job based on pull request approval ?
Highlighted
Copilot Lvl 2
Message 8 of 15

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

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 

Highlighted
Copilot Lvl 2
Message 9 of 15

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

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

that would be very helpful for me

Highlighted
Ground Controller Lvl 1
Message 10 of 15

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

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