-
Notifications
You must be signed in to change notification settings - Fork 4
/
misc-console-scripts.groovy
173 lines (130 loc) · 5.37 KB
/
misc-console-scripts.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* Change maven settings provider from FilePath to Default
*/
import groovy.xml.*;
import hudson.model.*;
import hudson.maven.*;
import jenkins.model.*;
import jenkins.maven.*;
import hudson.*;
def count = 0
Jenkins.instance.getAllItems(FreeStyleProject.class).findAll{job -> !job.isDisabled() }.each{
job ->
count++
//println("\nBegin Processing ${job.name} (${job.class}")
job.builders.findAll{builder -> builder instanceof hudson.tasks.Maven && builder.settings instanceof jenkins.mvn.FilePathSettingsProvider}.each { builder ->
println "Project ${job.name}"
println("Settings: ${builder.settings}, path: ${builder.settings.path}")
builder.settings = new jenkins.mvn.DefaultSettingsProvider()
}
}
count
/**
* Set Subversion credentialsId for jobs that use a particular URL and don't currently have one set
*/
import groovy.xml.*;
import hudson.model.*;
import hudson.maven.*;
import jenkins.model.*;
import jenkins.maven.*;
import hudson.*;
def selectFrom = Jenkins.instance.allItems
def credentialsId = 'some-cred-id-string'
def svnHostname = 'svn.hostname.for.your.company'
def count = 0
Jenkins.instance.getAllItems(AbstractProject).findAll{job -> !job.isDisabled() && job.scm instanceof hudson.scm.SubversionSCM }.each{ job ->
job.scm.locations.findAll { loc ->
try { loc.SVNURL.host.startsWith(svnHostname) && (loc.credentialsId == null || loc.credentialsId == '') } catch (Exception x) { return false }
}.each{ loc ->
println "JOB ${job.name}: [cred(${loc.credentialsId})] ${loc.SVNURL}"
// Uncomment both lines when output looks right.
//loc.credentialsId = null
//loc.credentialsId = credentialsId
count++
}
}
count
/**
* Find original build in the pipeline that caused this build to be triggered
* (build is the current build)
*/
def prevRun = build
def firstRun = prevRun
while (prevRun != null) {
firstRun = prevRun
def cause = prevRun.getCause(Cause.UpstreamCause)
prevRun = cause?.upstreamRun
println "Found run: ${firstRun}"
}
//Print out overall pipeline duration (may be useful in email templates)
def duration = build.startTimeInMillis + build.duration - firstRun.startTimeInMillis
println "Duration: ${hudson.Util.getTimeSpanString(duration)}"
/**
* Find all gradle jobs (using gradle step or calling gradle on cmd-line)
*/
jenkins.model.Jenkins.getActiveInstance().getAllItems(Project.class).findAll{job -> job.builders.find{builder -> builder instanceof hudson.plugins.gradle.Gradle} }.each{job ->
println "Found gradle job ${job.fullName}: ${job}"
}
jenkins.model.Jenkins.getActiveInstance().getAllItems(Project.class).findAll{job -> job.builders.find{builder -> builder instanceof hudson.tasks.CommandInterpreter && builder.command.indexOf('gradle') >= 0} }.each{job ->
println "Found gradle job (cmd line) ${job.fullName}: ${job}"
}
true
/**
* Generate a cleanup script to remove orphaned "builds" subdirectories in disabled jobs.
* Results can be pasted into a shell. Threshold is in the if statement.
* This script assumes that all jenkins builds have already been properly deleted for the affected jobs, and it
* suggests that you blindly remove any subdirectories named "builds" (which are sometimes orphaned)
*/
Jenkins.instance.getAllItems(Job).findAll{job -> job.isDisabled()}.each{job ->
// println "Job ${job.fullName}"
result = "du -s /data/jenkins/jobs/${job.fullName}".execute().text
parts = result.tokenize()
if (Integer.parseInt(parts[0]) > 10000) {
println "df /data ; find /data/jenkins/jobs/${job.fullName} -name builds -exec rm -rf {} \\; ; df /data # ${parts[0]}"
}
}.size()
/**
* Print out Test Count summaries for a whole folder, based on looking at the latest completed build of each job
* supports Pipeline jobs
*/
def summarize(def startsWith, def verbose = false) {
def jobs = 0
def jobsWithResults = 0
def total = 0
def failed = 0
def skipped = 0
jenkins.model.Jenkins.getActiveInstance().getAllItems(Job).findAll{job -> !job.isDisabled() && job.fullName.startsWith(startsWith)}.each{job ->
if (verbose) { print "${job.fullName} => " }
jobs++
def build = job.getLastCompletedBuild()
if (!build) {
return
}
if (build.respondsTo('getTestResultAction') && build.getTestResultAction()) {
total += build.getTestResultAction().getTotalCount()
failed += build.getTestResultAction().getFailCount()
skipped += build.getTestResultAction().getSkipCount()
jobsWithResults++
if (verbose) {
println "${build.getTestResultAction().getTotalCount()}: ${build.getTestResultAction().getFailCount()}, ${build.getTestResultAction().getSkipCount()}"
}
} else if (build.getActions()) {
//println build.getActions()
build.getActions(hudson.tasks.junit.TestResultAction).each {action ->
total += action.getTotalCount()
failed += action.getFailCount()
skipped += action.getSkipCount()
jobsWithResults++
if (verbose) {
println "${action.getTotalCount()}: ${action.getFailCount()}/${action.getSkipCount()}"
}
}
} else {
if (verbose) {
println "NOTHING"
}
}
}
println "TOTAL FOR ${startsWith}:: ${jobs} Jobs (${jobsWithResults} with Test Results), Tests: ${total}: ${failed} Failed, ${skipped} Skipped"
}
summarize('FOLDER_NAME')