'Jenkins - trigger pipeline on multiple upstream change with declarative Jenkinsfile

I'm trying to to get Jenkins upstream trigger working with multiple projects, looks like it is supported according to Other available triggers and it does work when multiple "project/branch" ares specified literally, but it does not work when I put in the code with branch matching.

Working code - with the code below, the build is triggered by success build of either proj1/dev or proj2/dev.

triggers {
   upstream(
        ‘proj1/dev,proj2/dev’, thresholdhudson.model.Result.SUCCESS
)

}

Not working code - with the following code, the build is not triggered, although no syntax error from Jenkins.

triggers {
  upstream(
        upstreamProjects: ‘”proj1/“ + env.BRANCH_NAME.replaceAll("/", "%2F”),”proj2/“ + env.BRANCH_NAME.replaceAll("/", "%2F")',
 threshold: hudson.model.Result.SUCCESS
)

}

Any help is appreciated.



Solution 1:[1]

You have the entire thing surrounded with single quotes. That means you are setting it to the exact text inside the single quotes, rather than setting it with the variables. And you aren't quite building the string right, with commas and quoting incorrect. It should just be:

triggers {
  upstream(
        upstreamProjects: 'proj1/' + env.BRANCH_NAME.replaceAll('/', '%2F') + ', proj2/' + env.BRANCH_NAME.replaceAll('/', '%2F'),
 threshold: hudson.model.Result.SUCCESS
)

I also prefer single quotes if you aren't interpolating variables, as I have done above.

Another option as a single string with interpolated variables. This is a little easier to read:

triggers {
  upstream(
        upstreamProjects: "proj1/${env.BRANCH_NAME.replaceAll('/', '%2F')},proj2/${env.BRANCH_NAME.replaceAll('/', '%2F')}",
 threshold: hudson.model.Result.SUCCESS
)

Hopefully all your smart quotes are not actually smart quotes in the code, or those won't work. I hope that is just a copy/paste error getting the code into your post.

Solution 2:[2]

Do we know if we can add the blocking option to the trigger too? I want the job to wait if the upstream job is already running and if someone or code change triggered the current job.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rob Hales
Solution 2 Kidane