Jenkinsfile variable used in two separate steps

I have a pipeline job that uses two separate nodes (one for assembly, one for test), and I would like to share a variable between these two blocks of code in my Jenkinsfile . I assume this is possible, but I am very new to the groovy and Jenkinsfile . Here is the relevant code:

 node('build') { stage('Checkout') { checkout scm } stage('Build') { bat(script: 'build') def rev = readFile('result') } } node('test') { stage('Test') { def SDK_VERSION = "5.0.0001.${rev}" bat "test.cmd ${env.BUILD_URL} ${SDK_VERSION}" archiveArtifacts artifacts: 'artifacts/**/*.xml' junit 'artifacts/**/*.xml' } } 

I want to assign the variable "rev" at the build stage, but then combine it with the variable SDK_VERSION at the test stage. My mistake:

 groovy.lang.MissingPropertyException: No such property: rev for class: groovy.lang.Binding 
+6
source share
1 answer

Just define the variable before the node block:

 def rev = '' node('build') { stage('Checkout') { checkout scm } stage('Build') { bat(script: 'build') rev = readFile('result') } } 
+8
source

Source: https://habr.com/ru/post/1014684/


All Articles