In a declarative jenkins pipeline - can I set the agent label dynamically?

前端 未结 6 1022
-上瘾入骨i
-上瘾入骨i 2020-12-01 14:18

Is there a way to set the agent label dynamically and not as plain string?

The job has 2 stages:

  1. First stage - Runs on a \"master\" agent, always. At t
6条回答
  •  渐次进展
    2020-12-01 14:32

    it might be something about the context of the script block.

    this works, using a label of 'docker' in second stage:

    def hotLabel = 'docker'
    
    pipeline {
        agent { label 'master' }
        stages {
            stage('Stage1') {
                steps {
                    echo "node_name: ${hotLabel}"
                }
            }
    
            stage('Stage2') {
                agent { label "${hotLabel}" }
                steps {
                    echo "node_name: ${hotLabel}"
                }
            }
        }
    }
    

    this does not (gets the same There are no nodes with the label ‘null’ error):

    def hotLabel = null
    
    pipeline {
        agent { label 'master' }
        stages {
            stage('Stage1') {
                steps {
                    script {
                        hotLabel = "docker"
                    }
                }
            }
    
            stage('Stage2') {
                agent { label "${hotLabel}" }
                steps {
                    echo "node_name: ${hotLabel}"
                }
            }
        }
    }
    

提交回复
热议问题