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

Ability to abort all previous running builds

    XMLWordPrintable

    Details

    • Similar Issues:
    • Released As:
      2.42

      Description

      It is sometimes desirable for a job (such as a branch project) to simply abort any previously running builds as soon as a new build starts. For example, in a branch project for a pull request, you might want to see test results from an earlier commit even after pushing follow-up commits, but most of the time you only care about the results of the PR head, and computer time might be too valuable to waste on the older ones.

      (I think gerrit-trigger does something like this automatically, and I have seen Alex Gray invent the same kind of pattern with JenkinsPy.)

      Merely setting the job to not be concurrent-capable does not suffice, since then newer builds will queue up waiting for the older ones to finish.


      Manual equivalent courtesy of Brandon Squizzato:

      def buildNumber = BUILD_NUMBER as int; if (buildNumber > 1) milestone(buildNumber - 1); milestone(buildNumber)
      

        Attachments

          Issue Links

            Activity

            Show
            stchar Stanislav Ovchar added a comment - Created PR  https://github.com/jenkinsci/pipeline-milestone-step-plugin/pull/18
            Hide
            d1morto Donald Morton added a comment - - edited

            This code caused 100% CPU on my Jenkins server:

            def buildNumber = env.BUILD_NUMBER as int
            if (buildNumber > 1) milestone(buildNumber - 1)
            milestone(buildNumber)
            

            I had restarted Jenkins after a security patch, and 20-30 builds kicked off from Branch Indexing. They all hung on the milestone() step. I finally started trying to kill them all, but then I noticed CPU was maxed out at 100% and it was taking forever. Had to comment out the above code and restart to fix it. 

            Attached a thread dump.

            Show
            d1morto Donald Morton added a comment - - edited This code caused 100% CPU on my Jenkins server: def buildNumber = env.BUILD_NUMBER as int if (buildNumber > 1) milestone(buildNumber - 1) milestone(buildNumber) I had restarted Jenkins after a security patch, and 20-30 builds kicked off from Branch Indexing. They all hung on the milestone() step. I finally started trying to kill them all, but then I noticed CPU was maxed out at 100% and it was taking forever. Had to comment out the above code and restart to fix it.  Attached a thread dump.
            Hide
            kamcknig Kyle McKnight added a comment -

            Would also like to know how to do this in declarative pipeline 

            Show
            kamcknig Kyle McKnight added a comment - Would also like to know how to do this in declarative pipeline 
            Hide
            kamcknig Kyle McKnight added a comment -

            I haven't tried it yet, but Paul Eipper, found this stackoverflow comment https://stackoverflow.com/a/52811034/1272355 for doing this with declarative pipeline

            Show
            kamcknig Kyle McKnight added a comment - I haven't tried it yet, but Paul Eipper , found this stackoverflow comment https://stackoverflow.com/a/52811034/1272355  for doing this with declarative pipeline
            Hide
            kon Kalle Niemitalo added a comment - - edited

            workflow-job-plugin #200 added support for:

            • properties([disableConcurrentBuilds(abortPrevious: true)]) as a step in a scripted pipeline
            • options { disableConcurrentBuilds abortPrevious: true } as a directive in a declarative pipeline

            I have a multibranch declarative pipeline in which I want to abort previous builds of pull requests but allow concurrent builds of branches. This cannot be done with the options directive because, although the value of the abortPrevious parameter can be made conditional, the disableConcurrentBuilds option itself cannot. I tried using the properties step instead but got a message saying it is not allowed:

            org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
            WorkflowScript: 52: Invalid step "properties" used - not allowed in this context - The properties step cannot be used in Declarative Pipelines @ line 52, column 17.
            

            I then moved that into a script step, and that seems to be working fine, but I don't understand why it is even disallowed in the first place.

            The options { when { … } } construct that was suggested in JENKINS-41392 comment 344397 would make things easier.

            Show
            kon Kalle Niemitalo added a comment - - edited workflow-job-plugin #200 added support for: properties( [disableConcurrentBuilds(abortPrevious: true)] ) as a step in a scripted pipeline options { disableConcurrentBuilds abortPrevious: true } as a directive in a declarative pipeline I have a multibranch declarative pipeline in which I want to abort previous builds of pull requests but allow concurrent builds of branches. This cannot be done with the options directive because, although the value of the abortPrevious parameter can be made conditional, the disableConcurrentBuilds option itself cannot. I tried using the properties step instead but got a message saying it is not allowed: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: WorkflowScript: 52: Invalid step "properties" used - not allowed in this context - The properties step cannot be used in Declarative Pipelines @ line 52, column 17. I then moved that into a script step, and that seems to be working fine, but I don't understand why it is even disallowed in the first place. The options { when { … } } construct that was suggested in JENKINS-41392 comment 344397 would make things easier.

              People

              Assignee:
              jglick Jesse Glick
              Reporter:
              jglick Jesse Glick
              Votes:
              31 Vote for this issue
              Watchers:
              42 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: