forked from cloudbees/jenkins-scripts
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathProperlyStopRunningPipelines.groovy
103 lines (94 loc) · 3.76 KB
/
ProperlyStopRunningPipelines.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
Author: Alex Taylor
Since: July 2018
Description: This script stop all or a series of running pipeline jobs
reset or became out of synch.
Parameters: None
Scope: Cloudbees Jenkins Platform
*/
/* This script can be used to kill off all running jobs, the latest build of a job, a specific job number, or jobs
which have been running for a certain amount of time. This is all based on a couple of specific settings which are marked
with comments. This script will guaranteed take 1.5 minutes to run because we want to ensure that each build command has the
time needed to run*/
import groovy.time.*;
// A couple of example map definitions:
//Used to kill all of the active jobs
//def jobMap = []
//Used to clean up specific builds. Put "buildNum: 0" if you want to stop all of the running builds for that job
/*def jobMap = [
//[name:'$JOB_NAME', buildNum: $BUILD_NUM2],
//[name:'$JOB_NAME2', buildNum: $BUILD_NUM2],
//[name:'$JOB_NAME3', buildNum: $BUILD_NUM3]
]*/
def jobMap = []
//Created for if the map is empty to add all the jobs
if(jobMap.isEmpty()){
Jenkins.instance.getAllItems(org.jenkinsci.plugins.workflow.job.WorkflowJob).each{
job -> job.builds.findAll{it.isBuilding()}.each{
build ->
println("Adding: "+ job.fullName+ " build number " + build.getNumber().toInteger())
jobMap.add([ name: job.fullName, buildNum: build.getNumber().toInteger()])
}
}
}
use(TimeCategory) {
def delay = 2.days;//Put in a Custom date here to kill anything older
def refDate = (new Date()- delay).time
// NO MODIFICATION
Jenkins.instance.getAllItems(org.jenkinsci.plugins.workflow.job.WorkflowJob).each{
job -> job.builds.byTimestamp(refDate, new Date().time).each{
build ->
println("Adding: "+ job.fullName+ " build number " + build.getNumber().toInteger())
jobMap.add([ name: job.fullName, buildNum: build.getNumber().toInteger()])
}
}
}
println "Removing the running builds for the following jobs: "
for(int i=0; i< jobMap.size(); i++)
{
def currentName = jobMap.get(i).name
def currentItem = Jenkins.instance.getItemByFullName(currentName)
if (currentItem.isBuilding()){
currentItem.builds.each{
build ->
if (build.isInProgress()&& jobMap.get(i).buildNum.equals(0)&& jobMap.get(i).name.equals(currentName)){
println("Adding: "+ currentName+ " build number " + build.getNumber().toInteger())
jobMap.add([ name: currentName, buildNum: build.getNumber().toInteger()])
}
}
//Calling the same as the `X` in the UI
def currentBuild = jobMap.get(i).buildNum
if(currentBuild){
println("Stopping " + currentName + " Build Number "+ currentBuild);
Jenkins.instance.getItemByFullName(currentName).getBuildByNumber(currentBuild).doStop();
}
}
}
Thread.sleep(30000)
//Calling the same as the Terminate running build command in the console log
for(int i=0; i< jobMap.size(); i++)
{
def currentName = jobMap.get(i).name
def currentItem = Jenkins.instance.getItemByFullName(currentName)
if (currentItem.isBuilding()){
def currentBuild = jobMap.get(i).buildNum
if(currentBuild){
println("Terminating " + currentName + " Build Number "+ currentBuild);
Jenkins.instance.getItemByFullName(currentName).getBuildByNumber(currentBuild).doTerm();
}
}
}
Thread.sleep(30000)
//Calling the same as the Kill running build command in the console log
for(int i=0; i< jobMap.size(); i++)
{
def currentName = jobMap.get(i).name
def currentItem = Jenkins.instance.getItemByFullName(currentName)
if (currentItem.isBuilding()){
def currentBuild = jobMap.get(i).buildNum
if(currentBuild){
println("Killing " + currentName + " Build Number "+ currentBuild);
Jenkins.instance.getItemByFullName(currentName).getBuildByNumber(currentBuild).doKill();
}
}
}