simulate post step for dynamically generated Jenkins pipeline stages The following simplified Jenkinsfile dynamically generates sequentially executed stages; however, I cannot create a 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 -&gt;
                        createStage(stageName)
                    }
                }
            }
        }
    }
    post {
        always {
            echo "post &gt; always"
        }
        success {
            echo "post &gt; 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} &gt; post &gt; always"
    //     }
    //     success {
    //         echo "${stageName} &gt; success &gt; 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?
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} &gt; this is executed only on success"
        finally {
            echo "${stageName} &gt; this is always executed"
        }
    }
}