I want to build some Packages using several Docker-Images in a Jenkins-Pipeline . The Pipeline below works .. but I want to implement the Stage(s) is a more "Loop"-ish way ....
pipeline {
agent { label 'docker' }
stages {
stage ('Running on ubuntu:20.04') {
agent {
docker {
reuseNode true
label 'docker'
image 'ubuntu:20.04'
}
}
steps {
sh 'uname -a && bash --version'
}
}
stage ('Running on ubuntu:22.04') {
agent {
docker {
reuseNode true
label 'docker'
image 'ubuntu:22.04'
}
}
steps {
sh 'uname -a && bash --version'
}
}
}
}
An Ideas how to implement that ? ..
Initially I tried using something like this
pipeline {
agent { label 'docker' }
stages {
stage ('Loop-Stage') {
steps {
script {
// Loop trough List (*works*)
for (item in ['ubuntu:20.04', 'ubuntu:22.04']) {
// Create new stage (*works*)
stage ('Running on ${item}') {
// Set agent (*fails*)
agent {
docker {
reuseNode true
label 'docker'
image "${item}"
}
}
steps {
sh 'uname -a && bash --version'
}
}
}
}
}
}
}
}
But this does now work. The Error seems to indicate a mixture between scripted&declarative Syntax .. you get the following error:
java.lang.NoSuchMethodError: No such DSL method 'agent' found among steps [ArtifactoryGradleBuild, MavenDescriptorStep, addInteractivePromotion, ansiColor, archive, artifactoryBuildTrigger, artifactoryDistributeBuild, artifactoryDownload, artifactoryEditProps, artifactoryGoPublish, ar
How can I apply a the corresponding "agent" to the dynamically generated Stage ?