Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 369
FrontSide Avatar asked Apr 10 '17 10:04

FrontSide


People also ask

How do I add multiple labels in Jenkins?

For example: agent { label 'my-label1 && my-label2' } or agent { label 'my-label1 || my-label2' } .

What is an agent specified while writing the Jenkins file declarative pipeline?

The agent section specifies where the entire Pipeline, or a specific stage, will execute in the Jenkins environment depending on where the agent section is placed. The section must be defined at the top-level inside the pipeline block, but stage-level usage is optional.

How do I specify agent in Jenkins pipeline script?

The “agent” section configures on which nodes the pipeline can be run. Specifying “agent any” means that Jenkins will run the job on any of the available nodes. An example of its usage could be: pipeline { agent any ... }

Can we have multiple stages in Jenkins pipeline?

Jenkins Pipeline allows you to compose multiple steps in an easy way that can help you model any sort of automation process. Think of a "step" like a single command which performs a single action. When a step succeeds it moves onto the next step. When a step fails to execute correctly the Pipeline will fail.


2 Answers

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 } 
like image 65
Arcin B Avatar answered Sep 19 '22 15:09

Arcin B


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'                 }             }         }     } } 
like image 37
mcalcote Avatar answered Sep 18 '22 15:09

mcalcote