如何在詹金斯(Jenkins)成功构建后触发参数化构建? [英] How to trigger parameterized build on successful build in Jenkins?
问题描述
我有三个管道项目,即项目a,项目b和项目c. project-c需要一个参数.成功完成project-a或project-b后,我想使用参数触发一个project-c的构建.
I have three pipeline projects, project-a, project-b and project-c. project-c takes a parameter. On successful completion of either project-a or project-b I want to trigger a build of project-c with a parameter.
我可以在管道中使用以下代码在project-a和project-b中进行此操作:
I can do this in project-a and project-b with this code in the pipeline:
stage('trigger-project-c') {
def job = build job: 'project-c', parameters: [[$class: 'StringParameterValue', name: 'MY_PARAM', value: 'somevalue']]
}
但这需要两名执行者.我希望project-a或project-b在使用参数运行project-c之前完全完成.
But this requires two executors. I want project-a or project-b to completely finish before running project-c with parameters.
推荐答案
您的管道最有可能是这样的:
Your pipeline most likely looks like this:
node {
stage('build') {
// sh "make"
}
// ...
stage('trigger-project-c') {
def job = build job: 'project-c', parameters: [[$class: 'StringParameterValue', name: 'MY_PARAM', value: 'somevalue']]
}
}
通过将所有内容包装在node
闭包内,可以内联触发下游作业project-c
,而无需暂停上游作业/释放执行程序.
By wrapping everything inside the node
closure, the downstream job project-c
is triggered inline, without the upstream job being paused / releasing an executor.
因此,长时间不执行任何操作的内容不应包装在node
步骤中,以免阻塞执行程序.一个非常相似的情况是使用input
步骤等待用户反馈.
Therefore, things that do essentially nothing for a long time should not be wrapped within a node
step, in order to not block an executor. A very similar case is when using the input
step to wait for user feedback.
相反,您的管道应该看起来像如下所示-可以说-最佳做法(因为您不会阻止执行程序):
Instead, your pipeline should look e.g. as follows, which is - so to say - the best practice (as you don't block your executor):
stage('build') {
node {
// sh "make"
}
}
// or
node {
stage('build') {
// sh "make"
}
stage('unit') {
// sh "make"
}
} // node
// note: the following code is _not_ wrapped inside a `node` step
stage('trigger-project-c') {
def job = build job: 'project-c', parameters: [[$class: 'StringParameterValue', name: 'MY_PARAM', value: 'somevalue']]
}
无需包装node中的> build
步骤,即,阻止它的执行程序.对于其他步骤(如sh
),管道执行将触发错误,并提醒您不能在node
分配范围之外运行.
There is no need to wrap the build
step within a node
, i.e., block an executor for it. For other steps (like sh
), pipeline execution would trigger an error and remind you that it cannot be run outside of a node
allocation.
这篇关于如何在詹金斯(Jenkins)成功构建后触发参数化构建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!