Last active
December 26, 2023 12:41
-
-
Save tamasgal/a05afafbf696d202d694f94f78317f80 to your computer and use it in GitHub Desktop.
Looping through a list of docker images and execute their stages in parallel. More info here: https://stackoverflow.com/questions/49782267/running-multiple-docker-containers-from-a-single-jenkinsfile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def docker_images = ["python:2.7.14", "python:3.5.4", "python:3.6.2"] | |
def get_stages(docker_image) { | |
stages = { | |
docker.image(docker_image).inside { | |
stage("${docker_image}") { | |
echo 'Running in ${docker_image}' | |
} | |
stage("Stage A") { | |
switch (docker_image) { | |
case "python:2.7.14": | |
sh 'exit 123' // for python 2.7.14 we force an error for fun | |
break | |
default: | |
sh 'sleep 10' // for any other docker image, we sleep 10s | |
} | |
sh 'echo this is stage A' // this is executed for all | |
} | |
stage("Stage B") { | |
sh 'sleep 5' | |
sh 'echo this is stage B' | |
} | |
stage("Stage C") { | |
sh 'sleep 8' | |
sh 'echo this is stage C' | |
} | |
} | |
} | |
return stages | |
} | |
node('master') { | |
def stages = [:] | |
for (int i = 0; i < docker_images.size(); i++) { | |
def docker_image = docker_images[i] | |
stages[docker_image] = get_stages(docker_image) | |
} | |
parallel stages | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment