30

I'm trying to trigger a downstream job from my current job like so

pipeline {
  stages {
    stage('foo') {
      steps{
        build job: 'my-job', propagate: true, wait: true
      }
     }
  }
}

The purpose is to wait on the job result and fail or succeed according to that result. Jenkins is always failing with the message Waiting for non-job items is not supported . The job mentioned above does not have any parameters and is defined like the rest of my jobs, using multibranch pipeline plugin.

All i can think of is that this type of jenkins item is not supported as a build step input, but that seems counterintuitive and would prove to be a blocker to me. Can anyone confirm if this is indeed the case?

If so, can anyone suggest any workarounds?

Thank you

omu_negru
  • 4,642
  • 4
  • 27
  • 38
  • 1
    I am dealing with pretty much the same issue here. Unfortunately the only other material I can find on this topic is another StackOverflow post from April: https://stackoverflow.com/questions/43337070/how-to-invoke-a-jenkins-pipeline-a-in-another-jenkins-pipeline-b – Bram Vandewalle Oct 02 '17 at 12:02

2 Answers2

61

I actually managed to fix this by paying more attention to the definition of the build step. Since all my downstream jobs are defined as multibranch pipeline jobs, their structure is folder-like, with each item in the folder representing a separate job. Thus the correct way to call the downstream jobs was not build job: 'my-job', propagate: true, wait: true, but rather build job: "my-job/my-branch-name", propagate: true, wait: true.

Also, unrelated to the question but related to the issue at hand, make sure you always have at least one more executor free on the jenkins machine, since the wait on syntax will consume one thread for the waiting job and one for the job being waited on, and you can easily find yourself in a resource-starvation type situation.

Hope this helps

omu_negru
  • 4,642
  • 4
  • 27
  • 38
  • 1
    So the bug I mentioned does not apply here? +1 anyway. – VonC Oct 03 '17 at 07:05
  • 3
    How does this work if the downstream multibranch pipeline doesn't have a branch setup yet? I receive this error in that case: `ERROR: No item named my-job/new-branch found`. – colti Oct 11 '18 at 20:11
  • 1
    if the build did not discover any branch yet it's obviously not going to work. So make sure the downstream multibranch job has the branch you're trying to call discovered / built before trying to build it from upstream. – omu_negru Oct 12 '18 at 09:39
  • @colti I had the same issue but this was because my job was on the same level. After referencing the job like this: `../my-job/branch` it worked. – рüффп Jun 18 '20 at 07:37
  • 2
    What would be the correct way to do this generically? I tried `build "my-job/${env.BRANCH_NAME}"` but that fails with a "No item named X found" error even though the branch was already known to the pipeline. – Pieter Jul 13 '20 at 08:23
  • had the same issues with the branch name. found any solution? @Pieter – Nicolas Gehlert Jun 09 '22 at 08:20
  • 1
    From what I can still find, it appears that I ended up running the branch name through a URL encoder: `URLEncoder.encode(env.BRANCH_NAME, 'utf-8')` – Pieter Jun 10 '22 at 09:07
2

This looks like JENKINS-45443 which includes the comment

Pipeline has no support for the upstream/downstream job system, in part due to technical limitations, in part due to the fact that there is no static job configuration that would make this possible except by inspecting recent build metadata.

But it also offer the workaround:

as long as the solution is still ongoing, I include here our workaround. It is based in the rtp (Rich Text Publisher) plugin, that you should have installed to make it work:

At the end of our Jenkinsfile and after triggering the job, we wait it to finish. In that case, build() returns the object used to run the downstream job. We get the info from it.

Warning: getAbsoluteUrl() function is a critical one. Use it at your own risk!

def startedBld = build(
    job: YOUR_DOWNSTREAM_JOB,
    wait: true, // VERY IMPORTANT, otherwise build () does not return expected object
    propagate: true
)

// Publish the started build information in the Build result
def text = '<h2>Downstream jobs</h2>Started job <a href="' + startedBld.rawBuild.getAbsoluteUrl () + '">' + startedBld.rawBuild.toString () + '</a>'
rtp (nullAction: '1',parserName: 'HTML', stableText: text)

This issue is part of JENKINS-29913, opened for the past two years:

Currently DependencyGraph is limited to AbstractProject, making it impossible for Workflows to participate in upstream/downstream relationships (in cases where job chaining is required, for example due to security constraints).

It refers the RFE (Request for Enhancement) JENKINS-37718, based on another (unanswered) Stack Overflow question.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250