cberk1
cberk1

Reputation: 45

Executing a Declarative Pipeline Stage on Multiple/all Agents

I have a project which features tests which only run on Linux and other tests which only run on Windows.

I have a Linux agent and a Windows agent. I need to check out the project (and its multiple dependencies) on both agents before I can run the tests on their respective agents.

What's the best method of doing this without repeating code?

I've managed to get a matrix block working like so:

 stage('SCM Checkout Matrix')
        {
            matrix {
                axes {
                    axes {
                        name 'AGENT'
                        values 'builtin', 'jenkins-uk-win7'
                    }
                }

                stages {
                    stage("SCM Checkout") {
                        options {
                            lock( 'synchronous-matrix' )
                        } 
                        agent {label "${AGENT}"}
                        steps {
                            <many git checkouts in here>
                           
                        }                        
                    }
                
                }
            }

But this seems hideously complicated and also makes the dashboard horribly messy: enter image description here

I've also tried defining multiple agent labels with the && operator:

agent(label 'builtin && jenkins-uk-win7')

as described here but it complains that there isn't an agent called 'builtin && jenkins-uk-win7'.

I've studied this post where people appear to be having similar problems to me regarding use of &&.

Surely this is a pretty elementary thing to do? Am I overlooking something painfully obvious?

Upvotes: 0

Views: 71

Answers (1)

Iterokun
Iterokun

Reputation: 2550

Yes, the declarative syntax for what you are trying to do is verbose. And the pipeline-stage-view plugin doesn't help there. Give the pipeline-graph-view plugin a try, and if you still find it cumbersome your only other option is to use a scripted pipeline:

def agents = ['builtin', 'jenkins-uk-win7']
def stages = agents.collectEntries { agent -> [
    agent,
    {
        node(agent) {
            stage("$agent Checkout") {
                echo agent
                // whatever else you want to do
            }
            // other stages
        }
    }
]}

parallel(stages)

Upvotes: 1

Related Questions