3

I have following Jenkinsfile, where I want to run the stage on 3 nodes. I have used && operator as per this answer.

pipeline{
agent {
    label 'webserver && serverex && composeserver'
}
stages{
stage('run this on 3 nodes'){
      steps{
         script{
         sh 'echo $HOSTNAME'
       }
   }}
}}

However I'm gettting following error.

There are no nodes with the label ‘webserver&&serverex&&composeserver’

The above stage is an example, I have multiple scripts under the same stage.

I can achieve this using parallel but it will be repetitive as I need to run it on 3 nodes.

May I know what is wrong with AND (&&) operator? Was it removed from Jenkins as it looks like it is working before as per the answer mentioned in the link?

smc
  • 2,175
  • 3
  • 17
  • 30

1 Answers1

6

I think you misinterpreted that answer. You can only reference a single node within node([...]), the && allow you to declare multiple labels which jenkins will look for when selecting an appropriate node.

For example when you have agent A with labels maven windows selenium and agent B with maven linux docker, agent C with maven gcc linux, node('maven') would select node A, B or C depending on executor aviability, which could be a problem when you have mixed in some linux-only sh step, thus you can use node('maven && linux') to get jenkins to choose one of the latter 2 agents that both contain those labels.

If you want to run the same stuff on multiple nodes, you could loop over an array of node labels.

def labels = ['label1', 'label2']

for (label in labels) {
  node(label) {
    stage('Running on ' + label){
      // Do stuff
    }
  }
}
Dominik Gebhart
  • 2,980
  • 1
  • 16
  • 28