Jenkins Pipeline Plugin传递构建在管道中启动的作业之间的作业参数

时间:2016-06-07 06:40:51

标签: jenkins groovy jenkins-pipeline

如果在Jenkins管道插件中如何将参数(参数化构建)从JobA传递到JobB,那么会感激不尽的完整代码示例吗?

我正在使用下面的脚本,无法从文档中找出如何从JobA中访问JobB中的构建步骤shell脚本中的参数:

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:没有这样的属性:类的CVS_TAG:groovy.lang.Binding

并且无法在$CVS_TAG中的构建步骤shell脚本中访问JobB

由于

根据您的回复,我也尝试过这个失败:

建立工作:' JobA',参数:[[$ class:' StringParameterValue',name:' test_param',value:' working&# 39;]]

env.test_param = test_param

echo $ {test_param}

错误总是:

groovy.lang.MissingPropertyException:没有这样的属性:test_param for class:groovy.lang.Binding at groovy.lang.Binding.getVariable(Binding.java:63)

1 个答案:

答案 0 :(得分:3)

上游JobA:

//do something
env.CVS_TAG = 'test'
build job: 'JobB'

下游JobB:

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
}
相关问题