Jenkins

[Script Console] Jenkins 실행 중이거나 대기 중인 빌드 일괄 멈추기

thxxyj 2023. 9. 23. 14:14
728x90

Jenkins 에서 실행 중인 빌드나 큐에 대기 중인 빌드를 Script Console에서 일괄적으로 중단하는 방법

Script Console : http://{JENKINS_URL}/manage/script

import java.util.ArrayList
import hudson.model.*;
import jenkins.model.Jenkins

  // 1. Remove everything which is currently queued
  def q = Jenkins.instance.queue
  for (queued in Jenkins.instance.queue.items) {
    q.cancel(queued.task)
  }

  // 2. stop all the currently running jobs
  // 2번만 실행해도 큐에 쌓인 빌드까지 중단됨
  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()
        }
      }
    }
  }

 

 

+ queue에 쌓인 빌드 삭제 조건문 추가

import java.util.ArrayList
import hudson.model.*;
import jenkins.model.Jenkins

  // 1. Remove everything which is currently queued
  def q = Jenkins.instance.queue
  for (queued in Jenkins.instance.queue.items) {
    if (queued.task['runId'].contains('멈추고싶은JOB이름') ) {
      q.cancel(queued.task)
    }
    else {  // skip
      println(queued.task)
    }
  }

 

# Ex) println(queued.task) 
ExecutorStepExecution.PlaceholderTask{runId=CI_MAIN_JOB#79060,label=master,context=CpsStepContext[16:node]:Owner[CI_MAIN_JOB/79060:CI_MAIN_JOB #79060],cookie=null,auth=null}

 

 

 

+ Jenkins Log 참고

# doKill() 했을때 Jenkins Log
Hard kill!
Finished: ABORTED

# doStop() 했을때 Jenkins Log
Aborted by USER
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
org.jenkinsci.plugins.workflow.actions.ErrorAction$ErrorId: a0bff3b3-17e8-48dc-9f85-931
Finished: ABORTED

 

https://stackoverflow.com/questions/12305244/cancel-queued-builds-and-aborting-executing-builds-using-groovy-for-jenkins

 

 

 

728x90