1

How can I pass the Build Number of my Jenkins Pipeline to a job. I have a pipeline which builds jobs in the following order-

a --> b --> c --> d

I have to pass the build number of Pipeline itself to job c. This job will use the passed parameter Build number in a Shell Script during Pre Steps.

My pipeline has an environment variable BUILD_NUMBER.

Is there a way to access the environment variable of Pipeline inside job c?

What I have tried

1. Using withEnv to override the environment variable but it does not work. I am doing this-

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

And then accessing in my Pre Step shell script of job c as follows-

${SERVER_BUILD_NUMBER}

2. Passing a "StringParameter" param2 to job c. This job is now receiving 2 parameters- a string value param1 and the string value of pipeline BUILD_NUMBER param2. I am getting following errors which I believe is due to wrong way of passing parameters-

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)

I have checked the option The project is parameterized in pipeline. My parameter passing syntax is-

   build job: 'server', parameters: [[string(name: 'param1', value: param1)], [string(name: 'param2', value: '${env.BUILD_NUMBER}')]]
mlsngh
  • 23
  • 1
  • 6
  • Ad1. its dead road. Ad2. Can you please provide build job ivocation + destination job config? I think its somethong wrong with invocation – tworec Dec 21 '17 at 12:31
  • Please check the answers to the following [question](https://stackoverflow.com/questions/45324806/how-to-set-a-variable-inside-jenkins-shell-command). They might help you. – Ioan-Alexandru Tataru Feb 21 '18 at 07:51

1 Answers1

1

Ad1. it is dead end

Ad2. This is what you want to do. Just try to pass parameters correctly

Here is example of job with two string params invocation: https://jenkins.io/doc/pipeline/examples/#jobs-in-parallel

See also this SO question: How can I trigger another job from a jenkins pipeline (jenkinsfile) with GitHub Org Plugin?

tworec
  • 4,409
  • 2
  • 29
  • 34
  • I am new to Jenkins and its terms. I need to pass the `BUILD_NUMBER` environment variable in my pipeline script when I am building job c. Is it giving some clarity? – mlsngh Dec 20 '17 at 18:01
  • I tried [this](https://support.cloudbees.com/hc/en-us/articles/221400287-How-to-pass-parameter-to-downstream-job-in-Pipeline-job-) link, added $class: 'StringParameterValue' to my array list and it worked. I did not try the first link given by you but I think it should work as well, will update accordingly. Thanks – mlsngh Dec 22 '17 at 09:36
  • Horray! So please accept answer if it was helpful. +1 also will be nice – tworec Dec 22 '17 at 14:11