We added the check to each stage and marked the build as ABORTED to prevent the build form going GREEN.
@NonCPS
def call() {
script {
restartedFromStage = currentBuild.getBuildCauses().any
{ cause -> cause._class == 'org.jenkinsci.plugins.pipeline.modeldefinition.causes.RestartDeclarativePipelineCause' }
if (restartedFromStage)
{ error 'Restarting build from a stage is disabled. Please rerun build from start.' currentBuild.result = 'ABORTED' return false }
else
{ return true }
}
}
in each stage we check
when {
expression
{ return call() }
steps {
...
It might be worthwhile to be able to disable restarting individual stages as well as the whole Pipeline.