Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkinsfile variable used in two separate stages

I have a pipeline job that uses two separate nodes (one for build, one for test), and I'd like to share a variable between two of these blocks of code in my Jenkinsfile. I assume it's possible, but I'm very new to groovy and the Jenkinsfile concept. Here is the relevant code so far:

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 "rev" variable in the build stage, but then concatenate it to the SDK_VERSION variable in the Test stage. My error is:

groovy.lang.MissingPropertyException: No such property: rev for class: groovy.lang.Binding
like image 984
user3270760 Avatar asked Feb 03 '17 16:02

user3270760


People also ask

Can we have multiple steps in a stage in Jenkins?

Jenkins Pipeline allows you to compose multiple steps in an easy way that can help you model any sort of automation process. Think of a "step" like a single command which performs a single action. When a step succeeds it moves onto the next step. When a step fails to execute correctly the Pipeline will fail.

Can we use different nodes for each stage in Jenkins?

You can also mix and match node { stage {..}} and stage { node {..}} within your pipeline codes. By design (in Jenkins, as well as in the concept of continuous delivery), stages do not execute in parallel. Only the steps within a single stage are executed in parallel.


1 Answers

Just define the variable before your node block:

def rev = ''
node('build') {
  stage('Checkout') {
    checkout scm
  }
  stage('Build') {
    bat(script: 'build')
    rev = readFile('result')
  }
}
like image 153
mkobit Avatar answered Oct 03 '22 20:10

mkobit