Jenkins pipeline: in stage1, read number from file, in stage2, **within** node name, use that number

To continue this:
Jenkins pipeline stages – passing whole file

I’m now stuck with having to set a number as part of a node name, where this number is contained within a file on another node.

  • How to retrieve current workspace using Jenkins Pipeline Groovy script?
  • How to get the list of changed file in SVN from Jenkins
  • jGit constructor error
  • Jenkins job running system Groovy script how to respond to user kill
  • How do I discover the additional causes of my Jenkins build?
  • How to fork a background process in Jenkins? Setting BUILD_ID and using nohup seems to be non working
  • E.g, if a properties file contains the number 6, I have to run a job on a node named DEV-SME-6.

    Is there a way for me to: in stage1, read the number from a file on the master node, something like this:

    Env_number = readFile 'file_containing_an_integer_between_2_and_7.txt'
    

    then in stage2, within the node name, use that number to specify part of the node name like this:

    node('DEV-SME-$Env_number')
    

    ??

    This doesn’t work with unstash (trying to unstash before specifiying a node name in a stage gives me an error: FilePath.hudson is missing

    My current code, after trying some stuff, is something like this:

    stage "first stage"
       node ('master'){
          try{
             env.Env_number = sh ' echo DEV$(cat file_containing_an_integer_between_2_and_7.txt '
          catch(error)
             echo "failed to set variable Env_number "
                }}
    
    stage "second stage"
       node('${Env_number}') {
          try{
             command1
             command2 
                }}
    

    This gives me a “no nodes with the label ‘null’ ”

  • Jenkins Slave Doesn't Use Same Ruby Version As On Local Terminal
  • Jenkins Pipeline based on groovy : unable to push into git => Permission denied (publickey)
  • Jenkins Git plugin: git describe cannot describe anything
  • How to access a remote Git repository in Jenkins using aliases?
  • How to automate a job in Jenkins?
  • Create git tag from within Jenkins pipeline script
  • 2 Solutions collect form web for “Jenkins pipeline: in stage1, read number from file, in stage2, **within** node name, use that number”

    You cannot use stash outside of a node, as it is meant to copy “stashed” files from the orchestrating Master to the node that runs the code.

    For these things you should use the env variable which will be serialized through the execution. Use it for information that should survive across different nodes.

    You’ve made one mistake in the code above.

    stage "second stage"
       node("${Env_number}") {
    

    Should be:”

    stage "second stage"
       node("${env.Env_number}") {
    

    Success! without sh scripts, with readFile:

    stage "first stage"
       node ('master'){
          try{
             env.Env_number = readFile 'file_containing_an_integer_between_2_and_7.txt'
          catch(error)
             echo "failed to set variable Env_number "
                }}
    
    stage "second stage"
       node("DEV${Env_number}") {
          try{
             command1
             command2 
                }}
    
    Git Baby is a git and github fan, let's start git clone.