user2023861
user2023861

Reputation: 8208

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

I'm trying to run a jenkins file with multiple agents in it, but I'm running into errors. Here's a snippet of my jenkins file:

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

I'm getting these errors:

I can't find any examples in the documentation of how to refer to a previously declared agent. I see how to declare the agent in each individual stage, but I'd end up with many repeated declarations in my file.

Upvotes: 2

Views: 10877

Answers (1)

Mishi.Srivastava
Mishi.Srivastava

Reputation: 390

You need to specify agent as none for overall pipeline, then you can specify agent for each stage explicitly as shown in below example. Populate the details as and what required.

pipeline {
    agent none
    stages {
        
        stage ('Stage-1') {
            agent { label 'agent-1' }
            steps {
                script {
                }
            }
        }
        
        stage ('Stage-2') {
            agent { label 'agent-2' }
            steps {
                script {
                }
            }
        }
    }
}

Refer link for further details - https://jenkins.io/doc/book/pipeline/jenkinsfile/#using-multiple-agents

Upvotes: 5

Related Questions