Using variables in a declarative pipeline

19 views
Skip to first unread message

Chris Packham

unread,
Sep 4, 2017, 4:23:00 AM9/4/17
to jenkins...@googlegroups.com
Hi,

I've got a declarative pipeline with parallel builds that works but
I'd like to avoid some repetition.

Here's the gist of my pipeline

pipeline {
stages {
stage('Build') {
parallel (
"target1": {
node ('maker') {
sh 'make target1_config'
sh 'make'
}
},
"target2": {
node ('maker') {
sh 'make target2_config'
sh 'make'
}
},
...
)
}
}
}

I'd like to be able to use "make ${something}_config" so that I don't
have to repeat (or track down the one place I haven't) the target
name. It would also be nice if I could define the build steps once and
re-use them inside the nodes.

Any suggestions?

Chris Packham

unread,
Sep 4, 2017, 6:24:34 AM9/4/17
to jenkins...@googlegroups.com
Answering my own question for internet posterity. I can achieve what I
want by using a 'script' step so my Build step above becomes

stage ('Build') {
steps {
script {
def targets = ["target1", "target2", ...]
def buildSteps = [:]
for (x in targets) {
def targets = x // apparently this is needed, something to
do with closures
buildSteps[target] = {
node ('maker') {
checkout scm
sh "make ${target}_config"
sh "make"
}
parallel buildSteps
}
}
}
}
}

I'd still like to hear if there is a better way to achieve this or if
there was a more declarative way of achieving this
(https://issues.jenkins-ci.org/browse/JENKINS-40986) sounds promising.
Reply all
Reply to author
Forward
0 new messages