Stan Allosserie
Stan Allosserie

Reputation: 117

Declarative Jenkins pipeline - How to set agent any only when condition is met?

Is it possible to specify agent 'any' only when a condition is met?

Context: I have a parallel pipeline with some common steps. The pipeline runs tests on Linux and Windows on two different agents. There are parameter defining if both, or only Linux, or only Windows should run. I want to use as little agents as possible.

With the following pipeline, the Linux tests are executed on the main agent. The Windows tests on a different agent. So if only Linux needs to run, I only use 1 agent, which is good. But if only Windows needs to run, the main agent will be blocked until the Windows agent finishes.

pipeline {
    agent any    // main agent
    parameters {
        booleanParam( name: 'RUN_WINDOWS' )
        booleanParam( name: 'RUN_LINUX' )
    }
    stages {
        stage('Common step') {
            ...
        }
        stage("Test") {
            parallel {
                stage('Windows') {
                    agent any
                    when {
                        expression { return params.RUN_WINDOWS }
                    }
                    stages {
                        stage('Test Windows') {
                            ...
                        }
                    }
                }
                stage('Linux') {    // no agent specified, so will use the main agent
                    when {
                        expression { return params.RUN_LINUX }
                    }
                    stages {
                        stage('Test Linux') {
                            ...
                        }
                    }
                }
            }
            post {
                ...
            }
        }
    }
}

I'd need something like this for the Windows stage, where it only specifies an agent based on a condition:

if (params.RUN_WINDOWS && params.LINUX) { agent any }

EDIT: I'm looking for a solution without using labels.

Upvotes: 1

Views: 1590

Answers (1)

ycr
ycr

Reputation: 14604

As a workaround, instead of setting any you can set agent labels conditionally. Something like below.

def labels = "Windows"

if (params.RUN_WINDOWS && params.LINUX) { 
  labels = "windows || linux"
}

pipeline {
    agent any    // main agent
    parameters {
        booleanParam( name: 'RUN_WINDOWS' )
        booleanParam( name: 'RUN_LINUX' )
    }
    stages {
        stage('Common step') {
            ...
        }
        stage("Test") {
            parallel {
                stage('Windows') {
                agent {
                   label "${labelSelected}"
                   }
                    when {
                        expression { return params.RUN_WINDOWS }
                    }
                    stages {
                        stage('Test Windows') {
                            ...
                        }
                    }
                }
                stage('Linux') {    // no agent specified, so will use the main agent
                    when {
                        expression { return params.RUN_LINUX }
                    }
                    stages {
                        stage('Test Linux') {
                            ...
                        }
                    }
                }
            }
            post {
                ...
            }
        }
    }
}

Upvotes: 1

Related Questions