0
votes

I'm not a Jenkins guru so please be patient. :-)

I have a pipeline, something nearly as simple as this:

def hash = ''
node {
      stage('Checkout') {
         …
      }

      stage('Build') {
         …
      }

      stage('Tests') {
         …
      }
}

stage('Ask deploy') {
    input 'Deploy?'
}

node {
      stage('Deploy') {
      }
}

I want to set the value of the hash variable in the first node and read it in the next if the manual input is positive. Is this possible and safe? Is this the correct approach?

Note that there are multiple executors and manual input involved. In the Jenkins docs it is hinted for a node that:

As soon as an executor is free on a node, the steps will run.

This means that the two nodes may run in different executors, correct? Do they still share the same global variables? Thanks in advance for any clarifications!

2

2 Answers

0
votes

If you have multiple slaves in Jenkins, the pipeline will be launch in one of this slaves. Every slave is different.

Every stage in you pipeline will be launch in the same slave so if you have the variable "hash" at the first line of your pipeline you wouldn't have problem to read it in all your pipeline but if you have to access to this variable value from a different build you can not access.

If you need a global variable to read it in different builds you can define a global variable using the Global Variables String Parameter Plugin

0
votes

The hash variable is global and its value is available in the different executors which seems logical to me. So it looks like what I do is OK and it will work this way unless I miss something.

Here is how I've verified that (details skipped for brevity):

I've created a similar pipeline and killed the executor which ran the first node:

def gitHash;

node {
  withCredentials(...) {
      //Step 1:
      //Check out from the SCM
      stage('Prepare') {
        echo "Checking out the project from source control.."
        scmInfo = checkout scm
        gitHash = scmInfo.GIT_COMMIT
        echo "Project checked out, the GIT hash of the last commit is: ${gitHash}"
      }
  }
}

stage('Ask deploy') {
    input 'Deploy?'
}

node {
    withCredentials(...) {
      stage('Deploy') {
        echo "TODO, hash ${gitHash}"
      }
    }
}

The output from Jenkins is the following (details skipped):

Obtained Jenkinsfile from 7adc4bb98524b31de93e0c1ae16bf967ca3df47c
Running on jnlp-13775fa128a47 in /root/workspace/...
[Pipeline] {
[Pipeline] withCredentials
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Prepare)
[Pipeline] echo
Project checked out, the GIT hash of the last commit is: 7adc4bb98524b31de93e0c1ae16bf967ca3df47c
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage

[Pipeline] stage
[Pipeline] { (Ask deploy)
[Pipeline] input
Deploy?
Proceed or Abort
Approved by admin
[Pipeline] }
[Pipeline] // stage
[Pipeline] node
Running on jnlp-1383bdf520c9d in /root/workspace/...
[Pipeline] {
[Pipeline] withCredentials
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Deploy)
[Pipeline] echo
TODO, hash 7adc4bb98524b31de93e0c1ae16bf967ca3df47c
[Pipeline] End of Pipeline
Finished: SUCCESS

As seen the first node runs on executor jnlp-13775fa128a47 the second is on jnlp-1383bdf520c9d but the value of the globally scoped variable can be read there.