patternMinor
simulate post step for dynamically generated Jenkins pipeline stages
Viewed 0 times
generateddynamicallyjenkinsstagespoststepforsimulatepipeline
Problem
The following simplified Jenkinsfile dynamically generates sequentially executed stages; however, I cannot create a
I would like to be able to use the commented-out
If at all possible, I'd like to use the DSL as much as possible and avoid scripted pipelines.
Suggestions?
post step for these dynamically stages, like so:pipeline {
agent none
stages {
stage('Gen Stages') {
steps {
script {
def stageNames = ["st1", "st2", "st3"]
stageNames.each { stageName ->
createStage(stageName)
}
}
}
}
}
post {
always {
echo "post > always"
}
success {
echo "post > success"
}
}
}
def createStage(String stageName) {
stage(stageName) {
echo "Stage: ${stageName}"
}
// I want to uncomment and use code below - or something effectively simiarl:
// post {
// always {
// echo "${stageName} > post > always"
// }
// success {
// echo "${stageName} > success > always"
// }
// }
}I would like to be able to use the commented-out
post {} stage, or something effectively similar.If at all possible, I'd like to use the DSL as much as possible and avoid scripted pipelines.
Suggestions?
Solution
You can't use
post because post is part of the declarative DSL, not scripted, and you're defining your stages in a script block. So I'm afraid that as long as you're using a script block to define your stages, you're going to have to stick with the scripted method, which is the groovy try { ... } catch { ... } finally { ... } construct. For example:def createStage(String stageName) {
stage(stageName) {
try {
echo "Stage: ${stageName}"
echo "${stageName} > this is executed only on success"
finally {
echo "${stageName} > this is always executed"
}
}
}Code Snippets
def createStage(String stageName) {
stage(stageName) {
try {
echo "Stage: ${stageName}"
echo "${stageName} > this is executed only on success"
finally {
echo "${stageName} > this is always executed"
}
}
}Context
StackExchange DevOps Q#11769, answer score: 2
Revisions (0)
No revisions yet.