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
- create a github Personal Access Token (PAT) with relevant rights.
- For your repo, create a webhook with
- URL as
YourJenkinsURL/github-webhook/
- Choose ‘Let me select individual events’ and check ‘Pull Request’
- 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
- Install GitHub Pull Request Builder plugin. (You also need “Github” plugin but that should normally be installed as part of Jenkins ver 2+)
- Jenkins – Credentials
- Add github Personal Access Token (PAT) as a ‘secret text’ credential.
- Add github username-password as ‘username-password’ credential.
- 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.
- 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:
- Select ‘Pipeline Script from SCM’. Note this assumes that the root folder of your repo will contain a ‘Jenkinsfile’
- SCM: Select ‘Git’
- Repositories – enter repo detail. For credentials use ‘username-password’ based credentials.
- Click Advanced and add refspec as
+refs/pull/*:refs/remotes/origin/pr/*
- Branch – should be
${sha1}
- Script Path: defaulted to Jenkinsfile, leave as is.
- 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 !!!