Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 358
NealR Avatar asked Sep 24 '20 16:09

NealR


People also ask

Can Jenkins build be triggered automatically?

Follow the steps as mentioned below to trigger a Jenkins job automatically based on GitHub's webhook configurations: Step 1: Go to the Configuration page of the respective job and under the build trigger section, check the "GitHub hook trigger for GITScm polling" checkbox and click on the Save button.

What happens when a pull request is merged?

Once the repository maintainer has approved a pull request, the developer's new updates in the forked repository are merged with the main project repository. The product is then updated with the new feature or bug fix, and can now be viewed by end users.

How can we make Jenkins pipeline jobs triggered by Pull requests?

Configure your pipelineOn the Datalog Tagging tab, check “GitHub project”, and put your project URL in the field. On the Build Triggers tab, check “GitHub Pull Request Builder”. Then the GitHub API credentials is automatically filled in. Make sure the Admin list is filled in with at least one admin name.

How do I trigger Jenkins pipeline from GitHub actions?

Create a trigger in your GitHub repository's settings page. Set the GitHub payload URL to be your Jenkins' IP address with /github-webhook/ appended to it. Set the Jenkins API token as the GitHub webhook secret token. Save the GitHub Webhook and then Jenkins builds will occur when a commit is pushed to the repo.


Video Answer


2 Answers

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

like image 167
Omisha gupta Avatar answered Oct 31 '22 11:10

Omisha gupta


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.
like image 36
VonC Avatar answered Oct 31 '22 09:10

VonC