0

In one stage of my declarative jenkins pipeline codes, it executes a bash script(sh '''./a.sh''', script "a.sh" is maintained outsides) - in that script, the value of "jarVersion" is injected in ${WORKSPACE}/.jarVersion (echo "jarVersion=${jarVersion}" > ${WORKSPACE}/.jarVersion). At later stage, we need get the value of jarVersion. We use load "${WORKSPACE}/.jarVersion" and ${jarVersion} to get the value. It works when we do so in pipeline script.

However, when we set this pipeline as a shared library (put it in /vars/testSuite.groovy) and call it in another pipeline script. It can not recognize var ${jarVersion}.

Please advise how to solve the issue. A common question is: how to transfer a value in a script from stage A to stage B?

    stage('getJarVersion'){
        steps{
            script{
                load "${WORKSPACE}/.jarVersion"
                currentBuild.description = "jarVersion:${jarVersion}"
            }
        }
    }

I expected it could work as it is in pipeline scripts.

But it shows:

groovy.lang.MissingPropertyException: No such property: jarVersion for class: testSuite
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:458)
    at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.getProperty(DefaultInvoker.java:34)
    at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
    at testSuite.call(/jenkins/jobs/TestSuite1/builds/11/libs/pipelineUtilities/vars/testSuite.groovy:84)
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Phoenix
  • 95
  • 3
  • 10

1 Answers1

0

With the stages under the same groovy file, you have to declare the object out of the stage blocks and before the node block. So for each stage, you can define the value inside the variable:

Pipeline {

def my_var

 stage('stage1'){
 ---------
 }

 stage('stage2'){
 ---------
}

}

If you are defining a stage per file, you have to create the closures with the input object and to pass it in the call from the parent groovy file:

test.groovy:

def call(def my_obj, String my_string) {


   stage('my_stage') {
     println(my_obj)
   }
}

parent_test.groovy

test(obj_value,string_value)
Daniel Majano
  • 976
  • 2
  • 10
  • 18