How to use multiple labels to select a node in a Jenkins Pipeline script?
Asked Answered
G

1

6

Intro:

We are currently running a Jenkins master with multiple slave nodes, each of which is currently tagged with a single label (e.g., linux, windows, ...)

In our scripted-pipeline scripts (which are defined in a shared library), we currently use snippets like the following:

node ("linux") {
    // do something on a linux node
}

or

node ("windows") {
    // do something on a windows node
}

Yet, as our testing environment grows, we now have multiple different Linux environments, some of which have or do not have certain capabilities (e.g., some may be able to run service X and some may not).

I would like to label my slaves now with multiple lables, indicating their capabilities, for example:

  • Slave 1: linux, serviceX, serviceY
  • Slave 2: linux, serviceX, serviceZ

If I now need a Linux slave that is able to run service X, I wanted to do the following (according to this):

node ("linux" && "serviceX") {
    // do something on a linux node that is able to run service X
}

Yet, this fails. Sometime, also a windows slave gets selected, which is not what I want to achieve.


Question: How can i define multiple labels (and-combined) based on which a node gets selected in a Jenkins scripted pipepline script?

Group answered 14/5, 2018 at 10:14 Comment(0)
W
14

The && needs to be part of the string, not the logical Groovy operator.

E.g.:

node ("linux && serviceX") {
    // do something on a linux node that is able to run service X
}
Wizard answered 14/5, 2018 at 10:20 Comment(5)
Could you add an example?Meador
node( 'Docker || docker-containers' )Index
&& does not work, here wrong answer is marked correct. JCompetence is correct you must use double pipe operator: node( 'Docker || docker-containers' )Jolty
@NikolaiEhrhardt For what was asked for in the question (the node has to be Linux AND has to offer serviceX AND has to offer serviceY), it has to be &&. With a || you would get either something that has the label Docker OR docker-containers, but may be missing the other. Kind of a weird thing to specify, indicates that whoever was in charge of maintaining the node cluster failed to stick to a single naming convention.Wizard
understood that, "&&" selects node with both labels.Jolty

© 2022 - 2024 — McMap. All rights reserved.