<p>You can't use <code>post</code> because post is part of the declarative DSL, not scripted, and you're defining your stages in a <code>script</code> block.  So I'm afraid that as long as you're using a <code>script</code> block to define your stages, you're going to have to stick with the scripted method, which is the groovy <code>try { ... } catch { ... } finally { ... }</code> construct.  For example:</p>

<pre><code>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"
        }
    }
}
</code></pre>

CC BY-SA 4.0
2020-06-05T18:35:13.127Z
2020-06-05T18:35:13.127Z
jayhendren
<p>The following simplified Jenkinsfile dynamically generates sequentially executed stages; however, I cannot create a <code>post</code> step for these dynamically stages, like so:</p>

<pre><code>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"
    //     }
    // }
}
</code></pre>

<p>I would like to be able to use the commented-out <code>post {}</code> stage, or something effectively similar.</p>

<p>If at all possible, I'd like to use the DSL as much as possible and avoid scripted pipelines.</p>

<p>Suggestions?</p>

<jenkins><jenkins-pipeline><jenkinsfile>
simulate post step for dynamically generated Jenkins pipeline stages
Trevor
# simulate post step for dynamically generated Jenkins pipeline stages

(Asked by: username_0 on 2020-06-04)

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 ->
                        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?

**Tags:** jenkins, jenkins-pipeline, jenkinsfile


---

## 1 answers

### Answer by username_1 on 2020-06-05. Score: 2 | Accepted answer

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"
        }
    }
}


---

