Cancel queued builds and aborting executing builds using Groovy for Jenkins

前端 未结 9 2046
礼貌的吻别
礼貌的吻别 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:25

    couldn't add as a comment, but as of today with latest jenkins, Andrey's script (nice) requires another import to work. Executing as system Groovy script.

    Jenkins errors and mentions the missing class. I included the url that mentioned the issue:

    //import hudson.model.*
    // per http://stackoverflow.com/questions/17429050/running-groovy-command-from-jenkins-using-groovy-script-plugin
    // requires this now
    import jenkins.model.Jenkins 
    
    def q = Jenkins.instance.queue
    
    q.items.findAll { it.task.name.startsWith('my') }.each { q.cancel(it.task) }
    
    0 讨论(0)
  • 2020-12-01 01:25

    I've expanded upon the snippet by Igor Zilberman so that it also aborts running jobs when there is a job in the queue with the same cause (what you see when you hover over the job in the build queue, only looking at the first line). I’m running this as a job with build step “Execute System Groovy Script”.

    
    import hudson.model.Result
    import jenkins.model.CauseOfInterruption
    import jenkins.model.*;
    
    [ // setup job names here
    'my-jobname-here'   
    ].each {jobName ->  
      def queue = Jenkins.instance.queue  
      def q = queue.items.findAll { it.task.name.equals(jobName) }  
      def r = [:]  
      def projs = jenkins.model.Jenkins.instance.items.findAll { it.name.equals(jobName) }  
    
      projs.each{p ->  
        x = p._getRuns()  
        x.each{id, y ->  
          r.put(id, y)  
        }  
      }  
    
      TreeMap queuedMap = [:]  
      TreeMap executingMap = [:]  
    
      q.each{i->  
        queuedMap.put(i.getId(), i.getCauses()[0].getShortDescription()) //first line  
      }  
      r.each{id, run->  
        def exec = run.getExecutor()  
        if(exec != null){  
          executingMap.put(id, run.getCauses()[0].getShortDescription()) //first line  
        }  
      }  
    
      println("Queued:")  
      queuedMap.each{ k, v -> println "${k}:${v}" }  
      println("Executing:")  
      executingMap.each{ k, v -> println "${k}:${v}" }  
    
      // First, if there is more than one queued entry, cancel all but the highest one.  
      // Afterwards, if there is a queued entry, cancel the running ones  
    
      def queuedNames = queuedMap.values();  
      queuedNames.each{n ->  
        def idsForName = []  
        queuedMap.each{ id, name ->  
          if(name.equals(n)){  
            idsForName.add(id)  
          }  
        }  
        if (idsForName.size() > 1){  
          println("Cancelling queued job: "+n)  
        }  
        // remove all but the latest from queue  
        idsForName.sort().take(idsForName.size() - 1).each { queue.doCancelItem(it) }  
      }  
      executingMap.each{ id, name ->  
        if(queuedMap.values().contains(name)){  
          r.each{rid, run->  
            if (id == rid){  
              def exec = run.getExecutor()  
              if(exec != null){  
                println("Aborting running job: "+id+": "+name)  
                exec.interrupt(Result.ABORTED)  
              }  
            }  
          }  
        }  
      }  
    }  
    return "Done"
    
    0 讨论(0)
  • 2020-12-01 01:29

    Use the jenkins groovy postbuild plugin:
    I think this would be the groovy script:

    import hudson.model.*  
    def q = jenkins.model.Jenkins.getInstance().getQueue()   
    def items = q.getItems()  
    for (i=0;i<items.length;i++){  
      if(items[i].task.getName() == "job_name"){  
      items[i].doCancelQueue()  
    }   
    }  
    
    0 讨论(0)
提交回复
热议问题