Cancel queued builds and aborting executing builds using Groovy for Jenkins

前端 未结 9 2045
礼貌的吻别
礼貌的吻别 2020-12-01 00:28

For Jenkins using a Groovy System Script, is there a way to easily search the build queue and list of executing builds for some criteria (specifically a parameter that match

相关标签:
9条回答
  • 2020-12-01 01:14

    After some investigation, I came up with this code which works absolutely fine for me. It clears the queue and also aborts all the jobs currently getting executed.

    Prerequisites:

    1. 'All' view contains all the jobs
    2. Use System groovy
    import jenkins.model.Jenkins
    import hudson.*
    import hudson.model.*
    import jenkins.*
    
    // Remove everything which is currently queued
    
    Jenkins.instance.queue.clear()
    def buildingJobs = Jenkins.instance.getAllItems(Job.class).findAll {
        it.isBuilding()
    }
    
    buildingJobs.each {
        def jobName = it.toString()
        def val = jobName.split("\\[|\\]")
    
        // 'Abort jobs' is the name of the job I have created, and I do not want it to abort itself.
    
        if((val[1].trim())!='Abort jobs') {
            def job = Jenkins.instance.getItemByFullName(val[1].trim())
            for (build in job.builds) {
                if (build.isBuilding()) {
                    println(build)
                    build.doStop();
                }
            }
        }
    }
    
    
    0 讨论(0)
  • 2020-12-01 01:15

    I haven't tested it myself, but looking at the API it should be possible in the following way:

    import hudson.model.*
    import jenkins.model.Jenkins
    
    def q = Jenkins.instance.queue
    
    q.items.findAll { it.task.name.startsWith('my') }.each { q.cancel(it.task) }
    

    Relevant API links:

    • http://javadoc.jenkins-ci.org/jenkins/model/Jenkins.html
    • http://javadoc.jenkins-ci.org/hudson/model/Queue.html
    0 讨论(0)
  • 2020-12-01 01:19

    Here is my solution, if you want to run only the newest job of same project from the build queue and cancel other:

    def q = Jenkins.instance.queue
    //Find items in queue that match <project name>
    def queue = q.items.findAll { it.task.name.startsWith('sample_project') }
    //get all jobs id to list
    def queue_list = []
    queue.each { queue_list.add(it.getId()) }
    //sort id's, remove last one - in order to keep the newest job, cancel the rest
    queue_list.sort().take(queue_list.size() - 1).each { q.doCancelItem(it) }
    
    0 讨论(0)
  • 2020-12-01 01:20

    Referencie: https://xanderx.com/post/cancel-all-queued-jenkins-jobs/

    Run this in Manage Jenkins > Script Console:

    Jenkins.instance.queue.clear()
    
    0 讨论(0)
  • 2020-12-01 01:24

    To control Job build queue, you can use this Plugin also: https://wiki.jenkins-ci.org/display/JENKINS/Block+queued+job+plugin

    • To block job when last build of defined target project is in building status
    • To block job when last build of defined target project has result
    0 讨论(0)
  • 2020-12-01 01:25

    I know it's kind of an old question, but Google points me to this one. The scripts shown here only remove the jobs from the queue, and don't stop running builds. The following script, just removes everything from the queue and kills all running builds:

      import java.util.ArrayList
      import hudson.model.*;
      import jenkins.model.Jenkins
    
      // Remove everything which is currently queued
      def q = Jenkins.instance.queue
      for (queued in Jenkins.instance.queue.items) {
        q.cancel(queued.task)
      }
    
      // stop all the currently running jobs
      for (job in Jenkins.instance.items) {
        stopJobs(job)
      }
    
      def stopJobs(job) {
        if (job in com.cloudbees.hudson.plugins.folder.Folder) {
          for (child in job.items) {
            stopJobs(child)
          }    
        } else if (job in org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject) {
          for (child in job.items) {
            stopJobs(child)
          }
        } else if (job in org.jenkinsci.plugins.workflow.job.WorkflowJob) {
    
          if (job.isBuilding()) {
            for (build in job.builds) {
            build.doKill()
            }
          }
        }
      }
    
    0 讨论(0)
提交回复
热议问题