Run / Deploy my declarative pipeline for all my agents

Greetings community!

I have a declarative pipeline where I’m trying to run Jenkinsfile script for all agents with a specific label.
I have added agent any in the beginning, but I also tried to add label into my agent at beginning of my script, as much as, inside of each stage, which it didn’t work. At the moment it just takes arbitrarily an agent and run the Jenkinsfile script against it.

My intention is to run the whole Jenkinsfile script against an agent, and go to the next till it finishes this loop for all agents.

How can I achieve that?

Is it better to use scripted pipeline instead?

Here is the structure of my Jenkinsfile

pipeline {
environment {
BACKUP_FILE = “${‘backup_’ + env.BUILD_ID + ‘_tar.gz’}”
DEPLOY_FOLDER = “${‘deploy’ + env.BUILD_ID}”
}

agent any

stages {

    stage('PREPARE ENVIRONMENT') {
        when {
            expression {
                ...
            }
        }

        steps {
            sh '''
                ...
            '''
        }
    }

    stage('BUILD') {
        when {
            expression {
                ...
            }
        }

        steps {
            sh '''
                ...
            '''
        }
    }

    stage('TEST') {
        when {
            expression {
                ...
            }
        }

        steps {
            sh '''
                ...
            '''
        }
    }

    stage('DEPLOY') {
        when {
            expression {
                ...
            }
        }

        steps {
            sh '''
                ...
            '''
        }
    }
}

post {
    always {
  	emailext (
  		attachLog: true,
  		compressLog: true,
  		to: 'myemail@email.compressLog',
  		subject: "MyApplication - Build # ${BUILD_NUMBER}",
  		body: "Status: ${currentBuild.currentResult}\nBuild number: ${env.BUILD_NUMBER}\nTo see log details open the attached file or click on the link: ${env.BUILD_URL}"
  	)
    }
}

}

Any help would be greatly appreciated.

Thank you in advance!

Cheers,
Alex

Very short version:

1 Like

Alternative if you are willing to use scripted, you can use something like

def nodes = nodesByLabel label: 'windows'
nodes = nodes.sort()

Map tasks = [:]

for (int i = 0; i < nodes.size(); i++) {
    def label = nodes[i]
    def stageName = "Checkout on ${label}"
    tasks[label] = {
        node(label) {
            stage(stageName) {
                checkout scm
                bat 'ECHO Hello world'
            }
        }
    }
}

timeout(time: 23, unit: 'MINUTES') {
    parallel(tasks)
}

1 Like

Thanks for your reply halkeye.
I was trying to use a solution without using any other plugin.
But I will keep that in mind.
Thank you!

Hi MarkEWaite!

Thank you very much for your reply.

What about I just use like below?

node(‘mylabel’) {
stage(stageName) {
checkout scm
bat ‘ECHO Hello world’
}
}

Is that going to run the entirely script for all my nodes/agents sequentially?

I actually don’t need to run them in parallel, but rather sequentially.

Also, I was not able to find too much documentation on scripted pipeline, mostly about declarative pipelines. If you know some page please post it here.

Thank you very much for your help. Much appreciated!

Alex

No. That will run a single stage on one of the nodes that has the label mylabel. If you want to run on multiple nodes, you’ll either need to use matrix in declarative as described by @halkeye or you’ll need to declare multiple stages, whether sequential or parallel.

If you’re running a current version of declarative Pipeline, you already have the matrix step in your Jenkins installation. No additional plugins are needed.

The Declarative directive generator on your Jenkins installation at /directive-generator/ will guide you to define the matrix directive.

Hi Mark!

It is good to know about that. I will try it out, thank you!

As you can see I’m not much experienced on Jenkins.

What is the best approach to run my entire Jenkinsfile for each agent that has a specific label sequentially? Declarative or Scripted pipelines? Or, both can do the job just fine?

Thank you very much for your reply!

Both can do the job just fine.

1 Like