詹金斯未能建立下游工作

jenkins fails on building a downstream job

我正在尝试像这样从我当前的工作中触发下游工作

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

目的是等待作业结果并根据该结果失败或成功。 Jenkins 总是失败并显示消息 Waiting for non-job items is not supported 。上面提到的作业没有任何参数,并且像我的其他作业一样使用多分支管道插件定义。

我能想到的是,这种类型的 jenkins 项目不支持作为构建步骤输入,但这似乎违反直觉,并且会被证明是我的障碍。任何人都可以确认是否确实如此?

如果是这样,有人可以提出任何解决方法吗?

谢谢

这看起来像 JENKINS-45443,其中包括评论

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.

但它也提供了解决方法:

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)

这个问题是 JENKINS-29913 的一部分,过去两年打开过:

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).

它指的是RFE(增强请求)JENKINS-37718, based on another (unanswered) Stack Overflow question

实际上,我通过更加注意构建步骤的定义设法解决了这个问题。由于我所有的下游作业都被定义为多分支管道作业,因此它们的结构类似于文件夹,文件夹中的每个项目代表一个单独的作业。因此调用下游作业的正确方法不是 build job: 'my-job', propagate: true, wait: true,而是 build job: "my-job/my-branch-name", propagate: true, wait: true.

此外,与问题无关但与手头的问题相关,请确保您在 jenkins 机器上始终至少有一个空闲的执行程序,因为等待语法将占用一个线程用于等待作业和一个线程对于正在等待的工作,您很容易发现自己处于资源匮乏的情况。

希望对您有所帮助