在Job中访问Jenkins管道的环境变量

时间:2017-12-20 15:05:44

标签: jenkins

如何将Jenkins Pipeline的Build Number传递给作业。 我有一个管道,按以下顺序构建作业 -

a - > b - > c - > d

我必须将Pipeline本身的内部版本号传递给作业c。 此作业将在预步骤中使用Shell脚本中传递的参数Build编号。

我的管道有一个环境变量BUILD_NUMBER。

有没有办法在作业c中访问Pipeline的环境变量?

我尝试了什么

1 即可。使用withEnv覆盖环境变量,但它不起作用。我这样做 -

withEnv(['SERVER_BUILD_NUMBER=$env.BUILD_NUMBER']) {
   build job: 'app-server-perf', parameters: [string(name: 'appServer_commit_id', value: appServer_commit_id)]
    }

然后按照以下步骤访问作业c的Pre Step shell脚本 -

${SERVER_BUILD_NUMBER}

2 即可。将“StringParameter”参数2传递给作业c。此作业现在接收2个参数 - 字符串值param1和管道BUILD_NUMBER param2的字符串值。我得到以下错误,我认为这是由于错误的传递参数的方式 -

java.lang.IllegalArgumentException: Could not instantiate {job=Shared Types, parameters=[[@string(name=sharedTypes_commit_id,value=develop2.2)], [@string(name=serverPerformance_build_number,value=494)]]} for BuildTriggerStep(job: String, parameters?: ParameterValue{BooleanParameterValue(name: String, value: boolean) | CredentialsParameterValue(name: String, value: String, description: String) | CvsTagsParamValue(name: String, tagName: String) | FileParameterValue(name: String, file: FileItem{}) | GitParameterValue(name: String, value: String) | JiraIssueParameterValue(name: String, value: String) | JiraVersionParameterValue(name: String, version: String) | LabelParameterValue(name: String, label: String, allNodesMatchingLabel: boolean, nodeEligibility: NodeEligibility{AllNodeEligibility() | IgnoreOfflineNodeEligibility() | IgnoreTempOfflineNodeEligibility()}) | ListSubversionTagsParameterValue(name: String, tagsDir: String, tag: String) | NodeParameterValue(name: String, labels: String[], nodeEligibility: NodeEligibility{AllNodeEligibility() | IgnoreOfflineNodeEligibility() | IgnoreTempOfflineNodeEligibility()}) | PromotedBuildParameterValue(name: String, runId: String, description: String) | RunParameterValue(name: String, runId: String, description: String) | StringParameterValue(name: String, value: String) | TextParameterValue(name: String, value: String) | com.michelin.cio.hudson.plugins.passwordparam.PasswordParameterValue~PasswordParameterValue(name: String, value: String, description: String) | hudson.model.PasswordParameterValue~PasswordParameterValue(name: String, value: String, description: String)}[], propagate?: boolean, quietPeriod?: int, wait?: boolean): java.lang.ClassCastException: class org.jenkinsci.plugins.workflow.support.steps.build.BuildTriggerStep.setParameters() expects class hudson.model.ParameterValue but received class java.util.ArrayList
    at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:264)
    at org.jenkinsci.plugins.workflow.steps.StepDescriptor.newInstance(StepDescriptor.java:201)
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeStep(DSL.java:177)
    at org.jenkinsci.plugins.workflow.cps.DSL.invokeMethod(DSL.java:124)
    at org.jenkinsci.plugins.workflow.cps.CpsScript.invokeMethod(CpsScript.java:117)
    at groovy.lang.GroovyObject$invokeMethod$6.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:155)
    at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23)

我已经检查了管道中的选项The project is parameterized。我的参数传递语法是 -

   build job: 'server', parameters: [[string(name: 'param1', value: param1)], [string(name: 'param2', value: '${env.BUILD_NUMBER}')]]

1 个答案:

答案 0 :(得分:0)

的Ad1。这是死路一条

Ad2的。这就是你想要做的。只是尝试正确传递参数

以下是两个字符串参数调用的作业示例: https://jenkins.io/doc/pipeline/examples/#jobs-in-parallel

另见这个问题:How can I trigger another job from a jenkins pipeline (jenkinsfile) with GitHub Org Plugin?