Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Loops in Jenkinsfiles
// Related to https://issues.jenkins-ci.org/browse/JENKINS-26481
abcs = ['a', 'b', 'c']
node('master') {
stage('Test 1: loop of echo statements') {
echo_all(abcs)
}
stage('Test 2: loop of sh commands') {
loop_of_sh(abcs)
}
stage('Test 3: loop with preceding SH') {
loop_with_preceding_sh(abcs)
}
stage('Test 4: traditional for loop') {
traditional_int_for_loop(abcs)
}
}
@NonCPS // has to be NonCPS or the build breaks on the call to .each
def echo_all(list) {
list.each { item ->
echo "Hello ${item}"
}
}
// outputs all items as expected
@NonCPS
def loop_of_sh(list) {
list.each { item ->
sh "echo Hello ${item}"
}
}
// outputs only the first item
@NonCPS
def loop_with_preceding_sh(list) {
sh "echo Going to echo a list"
list.each { item ->
sh "echo Hello ${item}"
}
}
// outputs only the "Going to echo a list" bit
//No NonCPS required
def traditional_int_for_loop(list) {
sh "echo Going to echo a list"
for (int i = 0; i < list.size(); i++) {
sh "echo Hello ${list[i]}"
}
}
// echoes everything as expected
@thiyagaraj

This comment has been minimized.

Copy link

commented Sep 25, 2017

thank you! 👍

@timlevett

This comment has been minimized.

Copy link

commented Oct 6, 2017

Yes! Exactly what I was looking for. Thanks

@Reesy

This comment has been minimized.

Copy link

commented Jan 10, 2018

This has been extremely helpful, thank you!

@tamasgal

This comment has been minimized.

Copy link

commented Apr 12, 2018

Thanks ;)

@prideloki

This comment has been minimized.

Copy link

commented Apr 18, 2018

👍

@aliasmee

This comment has been minimized.

Copy link

commented May 23, 2018

Thanks ^_^

@cropse

This comment has been minimized.

Copy link

commented Oct 31, 2018

This is absolutely helpful, Thanks.

@juandrn

This comment has been minimized.

Copy link

commented May 25, 2019

For me works with the last foreach and between script braces in pipeline:
Jenkisn version Jenkins ver. 2.164.3

SERVERDIRS = [ "%SERVERLINUXDIR%" , "%SERVERLINUXARMDIR%" ]

pipeline{
    environment {
		SERVERLINUXDIR		="Linux"
		SERVERLINUXARMDIR	="Linux-ARM"
    }
    stages	{
        stage ('Debug') {
            steps	{
				script{
					for (int i = 0; i < SERVERDIRS.size(); i++) {
						bat "echo Test Var ${SERVERDIRS[i]}"
					}
			      }
            }
    }
}
@nickboldt

This comment has been minimized.

Copy link

commented Jun 6, 2019

.each also works in declarative pipeline like this, using a String[] rather than a List:

def platforms = "linux-x64,darwin-x64,linux-arm"
platforms.split(',').each {
	sh "echo Something something ${it} ..."
}
@vikepic

This comment has been minimized.

Copy link

commented Jun 14, 2019

Thanks! 👍

@v1k-t0r

This comment has been minimized.

Copy link

commented Jul 11, 2019

+1

@abgm

This comment has been minimized.

Copy link

commented Jul 26, 2019

Thanks!!
fast connections check for different endpoints :)

node('master') {	
	stagesWithTry([
		'https://google.com/'
		,'https://github.com'
		,'https://releases.hashicorp.com/'
		,'https://kubernetes-charts.storage.googleapis.com'
		,'https://gcsweb.istio.io/gcs/istio-release/releases'
	])
}

def stagesWithTry(list){
	for (int i = 0; i < list.size(); i++) {
		try {	
			stage(list[i]){
					sh "curl --connect-timeout 15 -v -L ${list[i]}"
			} 
		} catch (Exception e) {
			echo "Stage failed, but we continue"  
		}
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.