管道作业 - 向上游传递参数? [英] Pipeline jobs - pass parameters upstream?
问题描述
TL;DR:显然,在 Jenkins 管道作业中,您可以轻松地向下游传递参数.我想知道的是您是否可以将它们传递到上游.
TL;DR: Obviously in a Jenkins pipeline job you can easily pass parameters downstream. What I want to know is if you can pass them upstream.
我们有三份工作;job_one
、job_two
和 job_three
.这些通常单独运行,因为只需要一个阶段,但在越来越频繁的情况下,我们希望能够背靠背运行所有三个阶段.
We have three jobs; job_one
, job_two
, and job_three
. These are frequently run separately as only one stage is needed, but in increasingly more-frequent cases we'd like to be able to run all three back to back.
第一个和第二个依赖于您可以提前定义的参数,但第三个需要从第二个作业生成的参数(在 job_two 运行之前其结构未知的文件名).
The first and second rely on parameters you can define ahead of time, but the third needs a parameter that is generated from the second job (a file name whose structure is unknown until job_two runs).
我已经构建了 umbrella
,它为每个作业调用类似以下内容.在这种情况下,PARAM1
被填充,因为伞运行为使用参数构建".
I have built umbrella
, which calls something like the following for each job. In this case, PARAM1
is populated because umbrella runs as "Build with parameters".
build job: 'job_one', parameters: [[$class: 'StringParameterValue', name: 'PARAM1', value: "$PARAM1"]]
一切都好,花花公子,我可以在 job_one
中使用 PARAM1
就好了.
All fine and dandy, I can then use PARAM1
in job_one
just fine.
对于job_three
,我需要参数filename
.这是在 job_two
中生成的,因此据我所知是无法访问的,因为 job_three 不知道 job_two 在做什么.
For job_three
I need the parameter filename
. This is generated within job_two
, and therefore from what I can tell is inaccessible because job_three has no idea what job_two is doing.
在理想情况下,我只需让 job_two 将 filename
传递给伞式作业,后者会将其反馈给 job_three.因此,如何将生成的 filename
传递回伞形作业?
In an ideal world I would just have job_two pass the filename
to the umbrella job, which would feed it back into job_three. Therefore, how can I pass the generated filename
back up to the umbrella job?
我正在描绘一个类似这样的最终脚本;
I'm picturing a final script something like this;
node('on-demand-t2small'){
stage ('Build 1') {
build job: 'job_one', parameters: [[$class: 'StringParameterValue', name: 'PARAM1', value: "$PARMA1"]]
}
stage ('Build 2') {
build job: 'job_two', parameters: [[$class: 'StringParameterValue', name: 'PARAM2', value: "$PARMA2"]]
//somehow get the filename parameter out of job_two here so that I can move it to job three...
}
stage ('Build 3') {
build job: 'job_three', parameters: [[$class: 'StringParameterValue', name: 'filename', value: "$filename"]]
} }
补充说明:
我知道第一个问题是为什么不让 job_two 触发 job_three?我不能这样设置系统有两个原因;
Additional Notes:
I recognize that the first question will be "why not have job_two trigger job_three? I can't set the system up this way for two reasons;
- job_two 需要能够在不触发 job_three 的情况下运行,并且三个不能总是需要二的输入才能运行.
- 我曾讨论过让保护伞启动两个,然后在两个中设置一个子句,只有当它是由保护伞启动时才会触发三个,但据我所知,这将限制保护伞工作中的反馈;您不会知道两个失败是因为两个失败,还是因为三个(作为两个的一部分)失败.如果我对这个假设有误,请告诉我.
我曾考虑将参数设置为环境变量,但我认为这是特定于节点的,我不能保证两个作业都会在同一个节点上运行,所以这似乎不是解决方案.
I had thought about setting the parameter as an environment variable but I believe that's node-specific and I can't guarantee both jobs will run on the same node so that seemed to not be the solution.
Umbrella 是一个用 groovy 编写的管道作业,其他三个可能是管道或自由式作业,如果这很重要的话.
Umbrella is a pipeline job written in groovy, the other three may be pipeline or freestyle jobs, if that matters.
如果可能的话,我希望能提供详细的答案,我对 Groovy、Jenkins 和一般编码仍然很陌生.
I would appreciate detailed answers where possible, I'm still new to Groovy, Jenkins, and coding in general.
推荐答案
应该就这么简单:
stage ('Build 3') {
res = build job: 'job_three', parameters: [[$class: 'StringParameterValue', name: 'filename', value: "$filename"]]
echo "$res.buildVariables.filename"
}
假设你在 job_three 中做到了
Assuming that in job_three you do
env.filename = "col new file name"
这篇关于管道作业 - 向上游传递参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!