Is there a way to set the agent label dynamically and not as plain string?
The job has 2 stages:
- First stage - Runs on a "master" agent, always. At the end of this stage I will know on which agent should the 2nd stage run.
- Second stage - should run on the agent decided in the first stage.
My (not working) attempt looks like this:
pipeline {
agent { label 'master' }
stages {
stage('Stage1') {
steps {
script {
env.node_name = "my_node_label"
}
echo "node_name: ${env.node_name}"
}
}
stage('Stage2') {
agent { label "${env.node_name}" }
steps {
echo "node_name: ${env.node_name}"
}
}
}
}
The first echo works fine and "my_node_label" is printed.
The second stage fails to run on an agent labeled "my_node_label" and the console prints:
There are no nodes with the label ‘null’
Maybe it can help - if I just put "${env}" in the label field I can see that this is a java class as it prints:
There are no nodes with the label ‘org.jenkinsci.plugins.workflow.cps.EnvActionImpl@79c0ce06’
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…