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

Disable restarting stages in jenkinsfile optionally

    • Declarative backlog
    • 2.2121.vd87fb_6536d1e

      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()
      }

          [JENKINS-54250] Disable restarting stages in jenkinsfile optionally

          Andrew Bayer added a comment -

          It might be worthwhile to be able to disable restarting individual stages as well as the whole Pipeline.

          Andrew Bayer added a comment - It might be worthwhile to be able to disable restarting individual stages as well as the whole Pipeline.

          Jan Hoppe added a comment -

          That's right, disabling restart for all or a single stage would be great! 

          Jan Hoppe added a comment - That's right, disabling restart for all or a single stage would be great! 

          Chris Maes added a comment -

          the restart stage button is very confusing for many people because way more visible than the global restart (for the whole pipeline)... so disabling that would be nice.

          Chris Maes added a comment - the  restart stage button is very confusing for many people because way more visible than the global restart (for the whole pipeline)... so disabling that would be nice.

          Edward Maxwell-Lyte added a comment - - edited

          Our company would also like this feature to ensure partial pipelines can't be run.

          Edward Maxwell-Lyte added a comment - - edited Our company would also like this feature to ensure partial pipelines can't be run.

          Chris Hemp added a comment - - edited

          Our pipelines are very similar to this where restarting at a stage is confusing and rebuilds are the only option.

          Pipeline stage skeleton: 

          • Stage 1:  Checkout with a package manager install : yarn install, mvn install, gradlew build --refresh-dependencies
          • Stages [1 or *]: tests
          • Stages [about 5]: CI/CD pipeline

           

          Chris Hemp added a comment - - edited Our pipelines are very similar to this where restarting at a stage is confusing and rebuilds are the only option. Pipeline stage skeleton:  Stage 1:  Checkout with a package manager install : yarn install, mvn install, gradlew build --refresh-dependencies Stages [1 or *] : tests Stages [about 5] : CI/CD pipeline  

          Running in a swarm, where each job could be run in a different build agent, then this restart is useless from our use case: if the swarm chooses another build agent without the previous stages already run, then the executed one fails because of the absence of build artefacts (i.e.). If this build from stage is required, I'd enforce running within the same build agent

          Manuel de la Peña added a comment - Running in a swarm, where each job could be run in a different build agent, then this restart is useless from our use case: if the swarm chooses another build agent without the previous stages already run, then the executed one fails because of the absence of build artefacts (i.e.). If this build from stage is required, I'd enforce running within the same build agent

          +1 Within k8s environment, using kubernetes-plugin, and running dynamic agents makes having this option obsolete. Also took some time, and still the case, to explain people in our company, how to properly restart the job, every time, that you should actually use "replay" in this case instead.

          Sergiy Tupchiy added a comment - +1 Within k8s environment, using kubernetes-plugin, and running dynamic agents makes having this option obsolete. Also took some time, and still the case, to explain people in our company, how to properly restart the job, every time, that you should actually use "replay" in this case instead.

          Jonathan Sokolowski added a comment - - edited

          I've written a shared pipeline script to achieve this as a workaround:

          #!/usr/bin/env groovy
          
          @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.'
                  }
              }
          }
          

          Jonathan Sokolowski added a comment - - edited I've written a shared pipeline script to achieve this as a workaround: #!/usr/bin/env groovy @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.' } } }

          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 jsok !

          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 jsok !

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

          Alex Simenduev added a comment - jsok thanks for the script. Question, where do I put this shared step inside my pipeline? Do I must put it in each stage?

          Chris Maes added a comment -

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

          Chris Maes added a comment - jsok 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?

          Jo Rhett added a comment -

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

          Thanks jsok 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.

          Jo Rhett added a comment - shamil add it to a pipeline library as documented at  https://jenkins.io/doc/book/pipeline/shared-libraries/#defining-custom-steps Thanks jsok 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.

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

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

          Ian Boudreaux added a comment -

          My team is having this issue as well. We are using the Blue Ocean UI and the pipelines in our Jenkins setup have dependent stages where Stage A must run before Stage B. Because of this, the "restart from stage" option is dangerous and should not be used. We've resorted to logic that tells the user "this is not a supported workflow" when clicked, but it would be less confusing if we could disable it entirely. Especially since the "restart from stage" button is more prevalent than the "Rerun pipeline" button.

          Is there any target date for when this feature will be available in classic and Blue Ocean UIs?

          Ian Boudreaux added a comment - My team is having this issue as well. We are using the Blue Ocean UI and the pipelines in our Jenkins setup have dependent stages where Stage A must run before Stage B. Because of this, the "restart from stage" option is dangerous and should not be used. We've resorted to logic that tells the user "this is not a supported workflow" when clicked, but it would be less confusing if we could disable it entirely. Especially since the "restart from stage" button is more prevalent than the "Rerun pipeline" button. Is there any target date for when this feature will be available in classic and Blue Ocean UIs?

          Mark Waite added a comment -

          There is no target date when this feature will be available ianboudreaux

          Mark Waite added a comment - There is no target date when this feature will be available ianboudreaux

          James Nord added a comment - should be possible to tweak https://github.com/jenkinsci/pipeline-model-definition-plugin/blob/15978cd172a8d34e2cd6bf8a6bfeaa5514d44dad/pipeline-model-definition/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/actions/RestartDeclarativePipelineAction.java#L99 to disable this at the job level via a property.

          Ivan Pavlovic added a comment -

          Hi. I would like to contribute to this issue, and i have even started looking into it. However, i have never before contributed to Jenkins project, and if possible, i could use some guidance, like in bullet points. Is that possible hoppej? Is this a good place to discuss implementation details?

          Ivan Pavlovic added a comment - Hi. I would like to contribute to this issue, and i have even started looking into it. However, i have never before contributed to Jenkins project, and if possible, i could use some guidance, like in bullet points. Is that possible hoppej ? Is this a good place to discuss implementation details?

          Mark Waite added a comment -

          ipdev a discussion in this issue will be seen by those who are watching the issue and may be seen by a few others. If you'd like a wider discussion forum, the Jenkins developers mailing list is a good place to discuss development questions.

          If you'd like a general introduction to Jenkins plugin development, you might review the DevOps World 2021 workshop, "Contributing to Open Source", and the five part video series that came out of that workshop.

          Mark Waite added a comment - ipdev a discussion in this issue will be seen by those who are watching the issue and may be seen by a few others. If you'd like a wider discussion forum, the Jenkins developers mailing list is a good place to discuss development questions. If you'd like a general introduction to Jenkins plugin development, you might review the DevOps World 2021 workshop, "Contributing to Open Source" , and the five part video series that came out of that workshop.

          Ivan Pavlovic added a comment -

          markewaite thank you very much (and sorry for the late reply). I will be reaching out to Jenkins developers via the mailing list. Looking forward to my first contribution to Jenkins project!

          Ivan Pavlovic added a comment - markewaite thank you very much (and sorry for the late reply). I will be reaching out to Jenkins developers via the mailing list. Looking forward to my first contribution to Jenkins project!

          Ivan Pavlovic added a comment -

          Hi markewaite . I've subscribed to the developers email group, wrote an email but got no response. Anything else i can do?

          Ivan Pavlovic added a comment - Hi markewaite . I've subscribed to the developers email group, wrote an email but got no response. Anything else i can do?

          Mark Waite added a comment -

          ipdev sorry that you didn't get a response. You can refresh the question by replying to it on the list.

          Mark Waite added a comment - ipdev sorry that you didn't get a response. You can refresh the question by replying to it on the list.

          Ivan Pavlovic added a comment -

          Hi markewaite . I did as proposed, but still no answer. I will start creating PRs, as i would like to contribute by fixing the issue. Hopefully, i will get some communication going there. Thanks for helping out so far!

          Ivan Pavlovic added a comment - Hi markewaite . I did as proposed, but still no answer. I will start creating PRs, as i would like to contribute by fixing the issue. Hopefully, i will get some communication going there. Thanks for helping out so far!

          Mike Klein added a comment -

          Hi! New to this thread but also need this solution for the reasons stated by the OP. Any word on this?

          Mike Klein added a comment - Hi! New to this thread but also need this solution for the reasons stated by the OP. Any word on this?

          Ivan Pavlovic added a comment -

          mkleinaip PRs are open. However, not merged yet

          Ivan Pavlovic added a comment - mkleinaip PRs are open. However, not merged yet

          Mike Klein added a comment -

          ipdev, thank you for the update.

          Mike Klein added a comment - ipdev , thank you for the update.

          m l added a comment - - edited

          This issue can be closed since PR #560 is merged.

          m l added a comment - - edited This issue can be closed since PR #560 is merged.

          Ivan Pavlovic added a comment -

          mrlov Thanks for updating the ticket!

          Ivan Pavlovic added a comment - mrlov Thanks for updating the ticket!

          Hari Dara added a comment -

          Even after the disableResume option is used, I see that the build state is still periodically serialized. Considering that there is no need to resume, what is the use of saving the state periodically? Shouldn't that be completely avoided?

          Hari Dara added a comment - Even after the disableResume option is used, I see that the build state is still periodically serialized. Considering that there is no need to resume, what is the use of saving the state periodically? Shouldn't that be completely avoided?

            ipdev Ivan Pavlovic
            hoppej Jan Hoppe
            Votes:
            38 Vote for this issue
            Watchers:
            43 Start watching this issue

              Created:
              Updated:
              Resolved: