How to trigger a Jenkins 2.0 Pipeline job from a GitHub pull request
Asked Answered
I

4

32

It looks like the GitHubPullRequestBuilder is not compatible with Jenkins v2.0 pipeline jobs.

How do you configure a pipeline job to be triggered from a GitHub pull request event?
The documentation on this topic is sparse and I cannot find any examples of this. Or is it better to create a web-hook in GitHub to trigger the pipeline job on the PR event?

Illlooking answered 25/4, 2016 at 20:30 Comment(0)
M
2

The most straightforward way to use Pipeline with GitHub pull requests is to put the script into your repository under the name Jenkinsfile and then install the GitHub Branch Source plugin. Documentation

Maculate answered 26/4, 2016 at 18:55 Comment(5)
will that work only for PRs of members of the organization, or also for PRs from external contributors?Barkeeper
@elopio, the plugin has options to enable/disable building of PRs from the same repository as well as from external repositories. It's not exactly what you're asking for, but probably close enough.Exhibitionism
There is this official jenkinsci/workflow-demo docker image for playing around with PRs tests and Jenkinsfile. I was wondering if it was possible to have those nice red and green checks on the PRs and it's totally possible (example here) :) And here's a useful post about multibranch workflowsRussia
while this does allow you build PR branches, as far as I can tell there's no way to get the info about the PR that the old plugin used to offer: wiki.jenkins-ci.org/display/JENKINS/…Karlykarlyn
@Karlykarlyn some comparable information is available, such as the CHANGE_TITLE environment variable for example. See env documentation for available variables.Maculate
M
47

I had similar issue. Here’s what worked for me

Pre-req

  • Jenkins ver. 2+ (I was using Jenkins 2.60)
  • Github (or Githhub enterprise) account
  • Your github and Jenkins must be able to talk to each other.

On Github

  1. create a github Personal Access Token (PAT) with relevant rights.
  2. For your repo, create a webhook with
    • URL as YourJenkinsURL/github-webhook/
    • Choose ‘Let me select individual events’ and check ‘Pull Request’
  3. Add a Jenkinsfile to the root folder of your repo. For testing purpose you could put content as a basic hello world like below
    pipeline {
        agent any
        stages {
            stage('Test') {
                steps {
                    echo 'Hello World ...'
                }
            }
        }
    }

On Jenkins

  1. Install GitHub Pull Request Builder plugin. (You also need “Github” plugin but that should normally be installed as part of Jenkins ver 2+)
  2. Jenkins – Credentials
    • Add github Personal Access Token (PAT) as a ‘secret text’ credential.
    • Add github username-password as ‘username-password’ credential.
  3. Manage Jenkins – Configure System
    • Github – Github Servers : This is part of the Github plugin. Add a github server. ‘API URL’ It will default to https://api.github.com. If you are using enterprise github, replace with enterprise github url followed by /api/v3. For credential select the PAT option. Test the connection. ‘Manage Hooks’ is checked.
    • GitHub Pull Request Builder : for ‘GitHub Server API URL’ use same url as specified in Github Server section. Leave ‘Shared Secret’ blank. For credentials use ‘username-password’ credential. Test credentials to ensure its working. In my settings, ‘Auto-manage webhooks’ was checked.
  4. Pipeline Job
    • Create a new item using ‘Pipeline’ option. Note: This is the vanilla Pipeline job, not Multibranch Pipeline.
    • General Section: Check ‘Github Project’ – Project URL : Enter your github repo url
    • Build Triggers: Check ‘GitHub Pull Request Builder’
      • For ‘GitHub API credentials’ select option you set for GitHub pull request builder in ‘Manage Jenkins – Configure System’ screen
      • For admin list: add your username
      • Check Use github hooks for build triggering
    • Pipeline:
      1. Select ‘Pipeline Script from SCM’. Note this assumes that the root folder of your repo will contain a ‘Jenkinsfile’
      2. SCM: Select ‘Git’
      3. Repositories – enter repo detail. For credentials use ‘username-password’ based credentials.
      4. Click Advanced and add refspec as +refs/pull/*:refs/remotes/origin/pr/*
      5. Branch – should be ${sha1}
      6. Script Path: defaulted to Jenkinsfile, leave as is.
      7. Lightweight Checkout - Uncheck this (https://github.com/jenkinsci/ghprb-plugin/issues/507)

That’s it. You are all set. Creating a PR on master branch of your repo should now trigger your Jenkins Pipeline job

Some observations

  • Redelivering the webhook payload of a PR from github does not trigger the pipeline but opening a new PR or even re-opening a closed PR on github, triggers the pipeline job
  • In Pipeline Job Configuration, if you choose “Pipeline Script” and paste your pipeline script in there, the job doesn't trigger !!!
Millwork answered 15/7, 2017 at 18:43 Comment(3)
Can't we do the same with multibranch pipeline? Because the branch I want play with is not the master branch. @Deepak RaoSemple
Build Triggers: Check ‘GitHub Pull Request Builder’ that option isn't there for me. The plugin is installed because that option is present on other projects which are not using a jenkinsfile. Any thoughts?Redhead
Thank you for the detailed and well documented answer. This saves the day, this should have been the accepted answer IMHO.Sixtieth
G
3

The flow in a nutshell can go like this:

  1. You create your pipeline as code and save it in a file called Jenkinsfile at the root dir of your project. That code should describe how your project will get built. See here for examples: https://jenkins.io/doc/pipeline/examples/

  2. Then you should create a new "Multibranch Pipeline Project" item in your Jenkins. You should set this up so as to scan your repo of step 1.

  3. Now whenever you get a new PR branch opened on your step 1 repo, the branch will be checked-out and will get built according to the Jenkinsfile instructions included with it. You can set up more conditions on what gets built and when if you want to.

Notes:

  1. "Multibranch Pipeline Project" example documentation: https://jenkins.io/doc/book/pipeline-as-code/ (scroll down to Multibranch Pipeline Projects)

  2. Keep in mind that getting the plugin to build a PR after commending on the PR does not work. There is a discussion analyzing this here: https://github.com/jenkinsci/gitlab-plugin/issues/298 There are workarounds (also described in that discussion) but they are quite messy to set up.

Gaberones answered 5/4, 2017 at 12:59 Comment(0)
M
2

The most straightforward way to use Pipeline with GitHub pull requests is to put the script into your repository under the name Jenkinsfile and then install the GitHub Branch Source plugin. Documentation

Maculate answered 26/4, 2016 at 18:55 Comment(5)
will that work only for PRs of members of the organization, or also for PRs from external contributors?Barkeeper
@elopio, the plugin has options to enable/disable building of PRs from the same repository as well as from external repositories. It's not exactly what you're asking for, but probably close enough.Exhibitionism
There is this official jenkinsci/workflow-demo docker image for playing around with PRs tests and Jenkinsfile. I was wondering if it was possible to have those nice red and green checks on the PRs and it's totally possible (example here) :) And here's a useful post about multibranch workflowsRussia
while this does allow you build PR branches, as far as I can tell there's no way to get the info about the PR that the old plugin used to offer: wiki.jenkins-ci.org/display/JENKINS/…Karlykarlyn
@Karlykarlyn some comparable information is available, such as the CHANGE_TITLE environment variable for example. See env documentation for available variables.Maculate
K
0

Follow the Below Steps for Triggering Jenkins Job Automatically on Pull request generated on GitHub

  1. Create a web hook on GitHub i.e. http:///generic- webhook-trigger/invoke
  2. Content Type :application/json
  3. Select Pull Request as event Now github Configuration Part is done.

Jenkins Job Configuration

  1. Download Generic Webhook Trigger in Jenkins

  2. Git Hub Configuration On Jenkins

Git Hub Configuration

7.Select Generic Webhook Trigger on jenkins 8.Generic Webhook Trigger Configuration on Jenkinsand follow step 9

9.After doing step 7 Jenkins job will get trigger on PullRequest 10.Step 8 required to get information from Pull request Payload 11.Branch configuration inside Generic Webhook Trigger to get Branch details from Pull Request

Thanks

Krys answered 12/10, 2018 at 20:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.