如何在我的 Jenkins 文件中声明多个代理,然后在后续阶段引用它们?

How can I declare multiple agents in my Jenkins file and then refer to them in subsequent stages?

我正在尝试 运行 一个包含多个代理的 jenkins 文件,但我 运行 遇到了错误。这是我的詹金斯文件的片段:

pipeline {
    agent {
        docker {
            label 'agentAAA'
            ...
        }
        node {
            label 'agentBBB'
            ...
        }
    }
    ...
    stages {
        stage('to run on AAA') {
            agent {
                label 'agentAAA'
            }
            ...
        }
        stage('to run on BBB') {
            agent {
                label 'agentBBB'
            }
            ...
        }
        stage('to run on BBB') {
            agent {
                label 'agentBBB'
            }
            ...
        }

我遇到了这些错误:

我在文档中找不到任何有关如何引用先前声明的代理的示例。我看到了如何在每个单独的阶段声明代理,但我最终会在我的文件中重复声明很多。

您需要为整个管道将代理指定为 none,然后您可以为每个阶段显式指定代理,如下例所示。根据需要填写详细信息。

pipeline {
    agent none
    stages {

stage ('Stage-1'){
   agent {label 'agent-1'}
    steps{
        script{
        }
    }
   }   

stage ('Stage-2'){
   agent {label 'agent-2'}
    steps{
        script{
        }
    }
    }
}
}

参考 link 了解更多详情 - https://jenkins.io/doc/book/pipeline/jenkinsfile/#using-multiple-agents