【问题标题】:Jenkins Pipeline Plugin pass build Job parameters between jobs started in a pipelineJenkins Pipeline Plugin 在管道中启动的作业之间传递构建作业参数
【发布时间】:2016-06-07 06:40:50
【问题描述】:

如果有一个体面的完整代码示例,说明如何在 Jenkins Pipeline 插件中将参数(参数化构建)从 JobA 传递到 JobB,将不胜感激?

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

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

谢谢

根据您的回复,我也尝试过,但没有成功:

构建作业:'JobA',参数:[[$class:'StringParameterValue',名称:'test_param',值:'working']]

env.test_param=test_param

回声 ${test_param}

错误总是:

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

【问题讨论】:

  • 这个答案可能会给你一个线索:stackoverflow.com/questions/37079913/…
  • 您是否在 Build JobB 中启用了“此项目已参数化”?另请注意,您可以通过这种方式访问​​参数 ${CVS_TAG}。
  • 感谢您的回复,但我无法正常工作?任何使用构建作业的工作脚本示例?
  • 任何想法我做错了什么?这似乎是使用管道时最重要的功能,并且文档/示例的方式很少说明如何使用它?

标签: jenkins groovy jenkins-pipeline


【解决方案1】:

上游作业A:

//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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-07
    • 1970-01-01
    • 2018-06-27
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多