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

Disable restarting stages in jenkinsfile optionally

    XMLWordPrintable

Details

    • Declarative backlog
    • 2.2121.vd87fb_6536d1e

    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

            abayer Andrew Bayer added a comment -

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

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

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

            hoppej Jan Hoppe added a comment - That's right, disabling restart for all or a single stage would be great! 
            chrismaes 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.

            chrismaes 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.
            edwardmlyte Edward Maxwell-Lyte added a comment - - edited

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

            edwardmlyte Edward Maxwell-Lyte added a comment - - edited Our company would also like this feature to ensure partial pipelines can't be run.
            hemp 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

             

            hemp 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

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

            stupchiy 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.
            jsok 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.'
                    }
                }
            }
            
            jsok 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.' } } }
            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 jsok !

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

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

            chrismaes 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?
            jorhett 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.

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

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

            ianboudreaux 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?
            markewaite Mark Waite added a comment -

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

            markewaite Mark Waite added a comment - There is no target date when this feature will be available ianboudreaux
            teilo 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.
            ipdev 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?

            ipdev 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?
            markewaite 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.

            markewaite 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.
            ipdev 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!

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

            ipdev 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?
            markewaite 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.

            markewaite 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.
            ipdev 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!

            ipdev 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!
            mkleinaip 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?

            mkleinaip 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?
            ipdev Ivan Pavlovic added a comment -

            mkleinaip PRs are open. However, not merged yet

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

            ipdev, thank you for the update.

            mkleinaip Mike Klein added a comment - ipdev , thank you for the update.
            mrlov m l added a comment - - edited

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

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

            mrlov Thanks for updating the ticket!

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

            People

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

              Dates

                Created:
                Updated:
                Resolved: