问题描述
如果有一个体面的完整代码示例,说明如何在 Jenkins Pipeline 插件中将参数(参数化构建)从 JobA
传递到 JobB
,将不胜感激?
Would be grateful for a decent full code example of how to pass parameters (parameterized build) from JobA
to JobB
in Jenkins Pipeline plugin?
我正在使用如下脚本,无法从文档中了解如何从 JobA
访问参数,例如 JobB
中的构建步骤 shell 脚本:
I am using a script like below and can not figure from the docs how to access parameters from JobA
in say a build step shell script in JobB
:
build job: 'JobA', parameters: [[$class: 'StringParameterValue', name: 'CVS_TAG', value: 'test']]
build job: 'JobB', parameters: [[$class: 'StringParameterValue', name: 'CVS_TAG', value: 'test']]
echo env.CVS_TAG
上面给出了一个错误:
groovy.lang.MissingPropertyException: No such property: CVS_TAG for class: groovy.lang.Binding
并且无法在 JobB
的构建步骤 shell 脚本中访问 $CVS_TAG
.
And can not access $CVS_TAG
in a build step shell script in JobB
.
谢谢
根据您的回复,我也尝试过,但没有成功:
Per you responses I have also tried this unsuccessfully:
构建作业:'JobA',参数:[[$class:'StringParameterValue',名称:'test_param',值:'working']]
build job: 'JobA', parameters: [[$class: 'StringParameterValue', name: 'test_param', value: 'working']]
env.test_param=test_param
env.test_param=test_param
回声 ${test_param}
echo ${test_param}
错误总是:
groovy.lang.MissingPropertyException:没有这样的属性:类的test_param:groovy.lang.Binding at groovy.lang.Binding.getVariable(Binding.java:63)
groovy.lang.MissingPropertyException: No such property: test_param for class: groovy.lang.Binding at groovy.lang.Binding.getVariable(Binding.java:63)
推荐答案
上游JobA:
//do something
env.CVS_TAG = 'test'
build job: 'JobB'
下游作业B:
import hudson.EnvVars
import org.jenkinsci.plugins.workflow.cps.EnvActionImpl
import hudson.model.Cause
def upstreamEnv = new EnvVars()
node {
//if the current build is running by another we begin to getting variables
def upstreamCause = currentBuild.rawBuild.getCause(Cause$UpstreamCause)
if (upstreamCause) {
def upstreamJobName = upstreamCause.properties.upstreamProject
def upstreamBuild = Jenkins.instance
.getItemByFullName(upstreamJobName)
.getLastBuild()
upstreamEnv = upstreamBuild.getAction(EnvActionImpl).getEnvironment()
}
def CVS_TAG = upstreamEnv.CVS_TAG
echo CVS_TAG
}
这篇关于Jenkins Pipeline Plugin 在管道中启动的作业之间传递构建作业参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!