Nahshon paz Nahshon paz - 12 days ago 9
Groovy Question

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.

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' "

Answer

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 
            }}