问题
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' "
回答1:
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}") {
回答2:
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
}}
来源:https://stackoverflow.com/questions/40827434/jenkins-pipeline-in-stage1-read-number-from-file-in-stage2-within-node-n