2
votes

I'm using Declarative Pipelines 1.3.2 plugin and I want to use the same agent (as in only specifying the agent directive once) in multiple parallel stages:

stage('Parallel Deployment')
{
    agent { dockerfile { label 'docker'; filename 'Dockerfile'; } }
    parallel
    {
        stage('A') { steps { ... } } 
        stage('B') { steps { ... } }
    }
}

However, Jenkins complains:

"agent" is not allowed in stage "Parallel Deployment" as it contains parallel stages

A solution is to duplicate the agent directive for each parallel stage, but this is tedious and leads to lot of duplicated code with many parallel stages:

stage('Parallel Deployment')
{
    parallel
    {
        stage('A') { 
            agent { dockerfile { label 'docker'; filename 'Dockerfile'; } }
            steps { ... } 
        } 
        stage('B') { 
            agent { dockerfile { label 'docker'; filename 'Dockerfile'; } }
            steps { ... } 
        }
    }
}

Is there a more idiomatic solution, or is duplicating agent directive necessary for each of the parallel stages?

1
Are you not able to declare the agent at Pipeline level so all stages run on the same agent ?ben5556
You are totally correct; if this was the answer, I'd accept it now.Sean Pianka
Added it as an answer :)ben5556

1 Answers

-3
votes

Declare the agent at Pipeline level so all stages run on the same agent.