jenkinsjenkins-pipelinejenkins-agent

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


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?


Solution

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