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

一笑奈何 提交于 2019-12-08 22:53:37

问题


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.


回答1:


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
}



回答2:


This syntax appears to work for me:

agent { label 'linux && java' }



回答3:


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'
                }
            }
        }
    }
}



回答4:


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

agent { label 'small-or-medium' }


来源:https://stackoverflow.com/questions/43321026/can-i-define-multiple-agent-labels-in-a-declarative-jenkins-pipeline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!