Can I define multiple agent labels in a declarative Jenkins Pipeline?

JenkinsGroovyJenkins Pipeline

Jenkins Problem Overview


I'm using declarative Jenkins pipelines to run some of my build pipelines and was wondering if it is possible to define multiple agent labels.

I have a number of build agents hooked up to my Jenkins and would like for this specific pipeline to be able to be built by various agents that have different labels (but not by ALL agents).

To be more concrete, let's say I have 2 agents with a label 'small', 4 with label 'medium' and 6 with label 'large'. Now I have a pipeline that is very resource-low and I want it to be executed on only a 'small'- or 'medium'-sized agent, but not on a large one as it may cause larger builds to wait in the queue for an unnecessarily long time.

All the examples I've seen so far only use one single label. I tried something like this:

 agent { label 'small, medium' }

But it failed.

I'm using version 2.5 of the Jenkins Pipeline Plugin.

Jenkins Solutions


Solution 1 - Jenkins

You can see the 'Pipeline-syntax' help within your Jenkins installation and see the sample step "node" reference.

You can use exprA||exprB:

node('small||medium') {
    // some block
}

Solution 2 - Jenkins

> EDIT: I misunderstood the question. This answer is only if you know > which specific agent you want to run for each stage.

If you need multiple agents you can declare agent none and then declare the agent at each stage.

https://jenkins.io/doc/book/pipeline/jenkinsfile/#using-multiple-agents

From the docs:

pipeline {
    agent none
    stages {
        stage('Build') {
            agent any
            steps {
                checkout scm
                sh 'make'
                stash includes: '**/target/*.jar', name: 'app' 
            }
        }
        stage('Test on Linux') {
            agent { 
                label 'linux'
            }
            steps {
                unstash 'app' 
                sh 'make check'
            }
            post {
                always {
                    junit '**/target/*.xml'
                }
            }
        }
        stage('Test on Windows') {
            agent {
                label 'windows'
            }
            steps {
                unstash 'app'
                bat 'make check' 
            }
            post {
                always {
                    junit '**/target/*.xml'
                }
            }
        }
    }
}

Solution 3 - Jenkins

This syntax appears to work for me:

agent { label 'linux && java' }

Solution 4 - Jenkins

As described in Jenkins pipeline documentation and by Vadim Kotov one can use operators in label definition.

So in your case if you want to run your jobs on nodes with specific labels, the declarative way goes like this:

agent { label('small || medium') }

Some more examples from jenkins page:

// AND
agent { label('windows && jdk9 )')  }
 
// more complex one
agent { label('postgres && !vm && (linux || freebsd)')  } 

Solution 5 - Jenkins

Create a another label call 'small-or-medium' that has 6 all agents. Then in Jenkinsfile:

agent { label 'small-or-medium' }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFrontSideView Question on Stackoverflow
Solution 1 - JenkinsArcin BView Answer on Stackoverflow
Solution 2 - JenkinsmcalcoteView Answer on Stackoverflow
Solution 3 - Jenkinsgone.skiingView Answer on Stackoverflow
Solution 4 - JenkinsgitrustView Answer on Stackoverflow
Solution 5 - JenkinsBing ShiaoView Answer on Stackoverflow