def flag_prod1 = true
def flag_prod2 = false
def prods = [
'prod1',
'prod2'
]
pipeline
{
agent { label "j" }
stages
{
stage('Trigger')
{
steps
{
script
{
prods.each
{ prd ->
stage("stage $prd")
{
if (flag_${prd})
{
echo "Do something with $prd..."
}
}
}
}
}
}
}
}
The if
statement does not work: if (flag_${prd})
.
What should be the syntax of the if statement where part of the variable name needs to be taken from the other variable name?
CodePudding user response:
why not to declare known variable with all possible flags like this:
def prods = [
'prod1',
'prod2'
]
def flags = [
prod1: true,
prod1: false,
]
prods.each{prd->
if( flags[prd] ){
//do something
}
}
or you could try to define all in one:
def prods = [
prod1: [
flag: true
],
prod2: [
flag: false
]
]
for(prd in prods){
if( prd.flag ){
//do smth
}
}
CodePudding user response:
This works (using iterator key/value, and toBoolean()
conversion):
def jobs = [
'prod1': true,
'prod2': false
]
pipeline
{
agent { label "j" }
stages
{
stage('Trigger')
{
steps
{
script
{
jobs.each
{
if ("$it.value".toBoolean())
{
stage("Stage $it.key")
{
echo "Do something..."
}
}
}
}
}
}
}
}