9

We currently generate a lot of Jenkins jobs on a per Git branch basis using Jenkins job DSL; the multi-branch pipeline plugin looks like an interesting way to potentially get first-class job generation support using Jenkinsfiles and reduce the amount of Job DSL we maintain.

For example we have libwidget-server and widget-server develop branch projects. When the libwidget-server build finishes then the widget-server job is triggered (for the develop branch). This applies to other branches too.

This makes use of the Build after other projects are built to trigger upon completion of an upstream build (e.g. libwidget-server causes widget-server to be built).

It seems that the multi-branch pipeline plugin lacks the Build after other projects are built setting - how would we accomplish the above in the multi-branch pipeline build?

Friedrich 'Fred' Clausen
  • 3,321
  • 8
  • 39
  • 70

2 Answers2

15

You should add the branch name to your upstream job (assuming you are using a multi-branch pipeline for the upstream job too).

Suppose you have a folder with two jobs, both multi-branch pipeline jobs: jobA and jobB; jobB should trigger after jobA's master.

You can add this code snippet to jobB's Jenkinsfile:

properties([
  pipelineTriggers([
    upstream(
      threshold: 'SUCCESS',
      upstreamProjects: '../jobA/master'
    )
  ])
])

(Mind that any branch of jobB here will trigger after jobA's master!)

  • The upstreams in our case are not multi-branch pipelines but I'll do some tests with that. I also still need to look at @cscutcher's suggestion above. – Friedrich 'Fred' Clausen Sep 13 '16 at 01:30
  • It works perfectly well for multibranch jobs, but seems to be unsupported for bitbucket branch source plug-in. – luka5z Oct 21 '16 at 14:10
  • You can refer to other jobs from within multibranch job using absolute or relative paths `"/t/multibranch-job/master, ../multibranch-job/master, test, feature%252Ftest"` – luka5z Oct 21 '16 at 14:53
  • When i try this method I am getting `org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticField hudson.model.Result UNSTABLE` exception. Is there a way around it? Or should I ask the admin to whitelist the API (Which is tedious)? – Codebender Jan 02 '17 at 05:52
  • @luka5z how does that look? properties([ pipelineTriggers([ upstream( threshold: hudson.model.Result.SUCCESS, upstreamProjects: '"/t/multibranch-job/master, ../multibranch-job/master, test, feature%252Ftest" ) ]) ]) – Japster24 May 09 '17 at 15:18
  • 1
    @Codebender a bit late, but I believe the updated answer should solve your exception? – Jo Vandeginste Jul 16 '17 at 12:00
5

I'm currently trying to get this to work for our deployment. The closest I've got is adding the following to the downstream Jenkinsfile;

properties([
    pipelineTriggers([
        triggers: [
            [
                $class: 'jenkins.triggers.ReverseBuildTrigger',
                upstreamProjects: "some_project", result: hudson.model.Result.SUCCESS
            ]
        ]
    ]),
])

That at least gets Jenkins to acknowledge that it should be triggering when 'some_project' get's built i.e it appears in the "View Configuration" page.

However so far builds of 'some_project' still don't trigger the downstream project as expected.

That being said maybe you'll have more luck. Let me know if it works for you.

(Someone else has asked a similar question here -> Jenkins: Trigger Multi-branch pipeline on upstream change )

cscutcher
  • 400
  • 2
  • 8
  • 4
    This worked for me when I replaced `result:` with `threshold:`. I also specified a matching branch of a multi-branch pipeline with `"some_project/" + env.BRANCH_NAME.replaceAll("/", "%2F")` – Ross Lagerwall Nov 22 '16 at 11:52
  • @cscutcher, @Ross Lagerwall, using declarative `triggers { upstream() }` is not working for me to chain 2 multibranch pipeline jobs, using Jenkins 2.249.2, Pipeline: Multibranch v2.22, Pipeline v2.6, Pipeline: Job v2.4. I read somewhere that the upstream job needs to be specified as a "path", as @Jo Vandeginste suggests above, but it's not working for me either as a job name ("common_folder/mb_project/branch") or path ("../../other_mb_project/branch")... – timblaktu Feb 03 '21 at 18:22