Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-54250

Disable restarting stages in jenkinsfile optionally

    Details

    • Sprint:
      Declarative backlog
    • Similar Issues:

      Description

      I'd like to switch off the restart stage option.

      My pipelines currently don't work correctly after restarting a stage (I check out git only at first stage...)

      like that: 

      options

      {     disableResume()     *disableRestart()* }

        Attachments

          Issue Links

            Activity

            Hide
            hemp Chris Hemp added a comment -

            Previously I had the legacy UI hiding the "Restart from Stage" button with this css style (using the `simple-theme-plugin`):  

            /* Hide Restart from Stage */
            a[href$="restart"] {display: none;} 

            This css style hiding solution was not an option in the Blue Ocean UI though.  This helps, thanks Jonathan Sokolowski !

            Show
            hemp Chris Hemp added a comment - Previously I had the legacy UI hiding the "Restart from Stage" button with this css style (using the `simple-theme-plugin`):   /* Hide Restart from Stage */ a[href$= "restart" ] {display: none;} This css style hiding solution was not an option in the Blue Ocean UI though.  This helps, thanks Jonathan Sokolowski !
            Hide
            shamil Alex Simenduev added a comment -

            Jonathan Sokolowski thanks for the script. Question, where do I put this shared step inside my pipeline? Do I must put it in each stage?

            Show
            shamil Alex Simenduev added a comment - Jonathan Sokolowski thanks for the script. Question, where do I put this shared step inside my pipeline? Do I must put it in each stage?
            Hide
            chrismaes Chris Maes added a comment -

            Jonathan Sokolowski thanks for the script, which works fine, but this scripts generates a 

            expected to call pipeline.disableRestartFromStage but wound up catching script; see: https://jenkins.io/redirect/pipeline-cps-method-mismatches/

            error. Is there any way to avoid this error?

            Show
            chrismaes Chris Maes added a comment - Jonathan Sokolowski thanks for the script, which works fine, but this scripts generates a  expected to call pipeline.disableRestartFromStage but wound up catching script; see: https://jenkins.io/redirect/pipeline-cps-method-mismatches/ error. Is there any way to avoid this error?
            Hide
            jorhett Jo Rhett added a comment -

            Alex Simenduev add it to a pipeline library as documented at https://jenkins.io/doc/book/pipeline/shared-libraries/#defining-custom-steps

            Thanks Jonathan Sokolowski but I wanted to point out this doesn't solve the situation for any stage which has a when section and is also a required build step. (GitHub accepts "I didn't do this stage" as pass )

            Stage "Validate changed files" skipped due to this build restarting at stage "Run downstream jobs"
            [Pipeline] }
            [Pipeline] // stage
            [Pipeline] stage
            [Pipeline] { (Run downstream jobs)
            Stage "Run downstream jobs" skipped due to when conditional
            

            Build shows green, let's deploy! (facepalm)

            This really needs a global option.

            Show
            jorhett Jo Rhett added a comment - Alex Simenduev add it to a pipeline library as documented at  https://jenkins.io/doc/book/pipeline/shared-libraries/#defining-custom-steps Thanks Jonathan Sokolowski but I wanted to point out this doesn't solve the situation for any stage which has a  when section and is also a required build step. (GitHub accepts "I didn't do this stage" as pass ) Stage "Validate changed files" skipped due to this build restarting at stage "Run downstream jobs" [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (Run downstream jobs) Stage "Run downstream jobs" skipped due to when conditional Build shows green, let's deploy! (facepalm) This really needs a global option.
            Hide
            suganyaravikumar Suganya Ravikumar added a comment - - edited

            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 {
            ...

            Show
            suganyaravikumar Suganya Ravikumar added a comment - - edited 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 { ...

              People

              • Assignee:
                abayer Andrew Bayer
                Reporter:
                hoppej Jan Hoppe
              • Votes:
                15 Vote for this issue
                Watchers:
                20 Start watching this issue

                Dates

                • Created:
                  Updated: