I'm trying to convert my Scripted pipeline to a Declarative Pipeline.
Wondering how to do a simple if-statement inside a steps {}
block.
stage ('Deploy to Docker') {
steps {
parallel (
"instance1" : {
environment {
containerId = sh(script: "docker ps --quiet --filter name=${fullDockerImageName}", returnStdout: true).trim()
}
steps {
if (containerId.isEmpty()) {
docker.image('some/image').run("--name ${fullDockerImageName}")
}
}
}
)
}
}
This causes the following Exception:
WorkflowScript: 201: Expected a step @ line 201, column 29.
if (containerId.isEmpty()) {
Since I'm not allowed to do a simple if(..)
inside a steps {}
block, any idea on how to do this?
It doesn't seem to make sense to make this a full stage with a when {}
, since there are more steps that happens in a simple stage (starting a stopped container if it exists, etc).
What's the best way to do a simple if?