Trigger previous stages in Jenkins Blue Ocean if user click on restart stage button

We use ephemeral build environment, which are Docker agent containers in Nomad that are destroyed after every Jenkins job run. Occasionally, developers attempt to restart a failed stage in Jenkins Blue Ocean, but this process fails automatically because previous stages are not triggered on the newly created Docker agent container. Is it possible to configure Jenkins to run previous stages or the entire pipeline from the beginning regardless of which stage the developer tries to restart?

Yes, it is possible to configure Jenkins to run previous stages or the entire pipeline from the beginning regardless of which stage the developer tries to restart. One way to do this is by adding a “rebuild” button to your pipeline’s build history in Blue Ocean. This button allows developers to easily rebuild the pipeline from any stage, including previous stages that were not triggered due to the use of ephemeral build environments.

To add the “rebuild” button, you can use the Rebuilder plugin for Jenkins. This plugin adds a “rebuild” action to your build history in Blue Ocean, allowing developers to restart a failed stage or rebuild the entire pipeline from any previous stage. To install the plugin, go to the Jenkins plugin manager and search for “Rebuilder.”

Once the plugin is installed, you can configure the “rebuild” action by adding it to your pipeline’s post-build actions in your pipeline script. Here is an example of how to add the “rebuild” action:

pipeline {
    agent {
        label 'docker'
    }
    stages {
        // your stages here
    }
    post {
        always {
            rebuild()
        }
    }
}

With the “rebuild” action configured, developers can now easily rebuild the pipeline from any stage, ensuring that all previous stages are triggered on the newly created Docker agent container.