Trigger Jenkins build when pull request is merged in Github
Solution 1
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.
Solution 2
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.
And the triggering the build only if this variable is true
Related videos on Youtube
NealR
Updated on October 14, 2022Comments
-
NealR over 1 year
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).
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.
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?
-
NealR over 3 yearsI have the generic-webhook-trigger configured to look for those values in the payload, but how am I supposed to use them?
-
NealR over 3 yearsOn the Jenkins side does the "trigger remote build" need to be enabled?
-
NealR over 3 yearsAlso this only seems to work with the Docker installation of Jenkins, is that correct?
-
VonC over 3 years@NealR Yes, you would need to enable "trigger remote build". This GitHub action uses an URL to contact your Jenkins, so Docker or not, as long as your Jenkins is reachable from GitHub, it will be called by GitHub on Pull Request on master.
-
NealR over 3 yearssorry - tutorials for this are incredibly... sparse. Will this work without the Docker install? If so could you provide instructions on how to configure this to work on the Jenkins side?
-
VonC over 3 years@NealR There is no docker involved, (the jenkins2.caceis.group.gca uses a docker Jenkins as an example of a local Jekins, to show the screenshot where you need to generate a token, which will be used by the GitHub Action in its Jenkins "
url:
" parameter -
VonC over 3 years@NealR Following a tutorial like docs.github.com/en/free-pro-team@latest/actions/quickstart, you need to create a file triggerJenkinsBuild.yml (or any other name you want) in the folder
.github/workflows
in your GitHub repository: that file will combine the two sections I mention in my answer, in order to trigger Jenkins builds on pull requests onmaster
. -
NealR over 3 yearsLet us continue this discussion in chat.
-
VonC over 3 years@NealR I have edited the answer with the main conclusions from our discussion. Don't hesitate to add (in that discussion) the kind of typos other users of that GitHub Action need to pay attention to.
-
Kyle Stewart almost 3 yearsThank you, this was helpful, Seems to be working for me now
-
Omisha gupta almost 3 yearsGlad I was of help!