-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
306 lines (274 loc) · 8.74 KB
/
Jenkinsfile
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!'groovy'
import groovy.transform.Field
import groovy.transform.stc.ClosureParams
import groovy.transform.stc.SimpleType
@Field
String jdk = 'openjdk-17'
@Field
String idea = '2023.2'
@Field
String ideaSHA256 = 'b1a5c267ca86850764b0541bee0c27af7d2082e55516e95a0c8d30539571735c'
String projectName = env.JOB_NAME.substring(0, env.JOB_NAME.indexOf("/")) // depends on name and location of multibranch pipeline in jenkins
boolean isRelease = env.BRANCH_NAME=="master"
Map<String, ?> recordIssuesDefaults = [
failOnError : true,
enabledForFailure : true,
ignoreFailedBuilds : false,
skipPublishingChecks: true,
qualityGates : [[threshold: 1, type: 'TOTAL', unstable: true]],
]
properties([
gitLabConnection(env.GITLAB_CONNECTION),
buildDiscarder(
logRotator(
numToKeepStr : isRelease ? '1000' : '30',
artifactNumToKeepStr : isRelease ? '100' : '2',
))
])
boolean tryCompleted = false
try
{
Map<String, Closure<?>> parallelBranches = [:]
parallelBranches["Main"] = {
nodeCheckoutAndDelete { scmResult ->
def buildTag = makeBuildTag(scmResult)
mainImage(imageName("Main")).inside(dockerRunDefaults()) {
ant 'clean jenkins' +
' "-Dbuild.revision=${BUILD_NUMBER}"' +
' "-Dbuild.tag=' + buildTag + '"' +
' -Dbuild.status=' + (isRelease?'release':'integration') +
' -Dinstrument.verify=true'
}
recordIssues(
*: recordIssuesDefaults,
tools: [
java(),
],
)
junit(
allowEmptyResults: false,
testResults: 'build/testresults/**/*.xml',
skipPublishingChecks: true
)
recordCoverage(
id: 'coverage-java',
name: 'Coverage Java',
tools: [[pattern: 'build/report.xml']],
enabledForFailure: true,
skipPublishingChecks: true,
sourceDirectories: [[path: 'src']]
)
if (isRelease || env.BRANCH_NAME.contains("archiveSuccessArtifacts"))
archiveArtifacts fingerprint: true, artifacts: 'build/success/*'
plot(
csvFileName: 'plots.csv',
exclZero: false,
keepRecords: false,
group: 'Sizes',
title: 'exedio-cope-builder.jar',
numBuilds: '150',
style: 'line',
useDescr: false,
propertiesSeries: [
[ file: 'build/exedio-cope-builder.jar-plot.properties', label: 'exedio-cope-builder.jar' ],
[ file: 'build/exedio-cope-builder-src.zip-plot.properties', label: 'exedio-cope-builder-src.zip' ],
],
)
}
}
parallelBranches["Forensic"] = {
nodeCheckoutAndDelete {
discoverGitReferenceBuild()
gitDiffStat()
if (isRelease || env.BRANCH_NAME.contains("forensic")) mineRepository()
recordIssues(
*: recordIssuesDefaults,
qualityGates: [[threshold: 1, type: 'TOTAL_HIGH', unstable: true]],
tools: [
taskScanner(
excludePattern:
'.git/**,lib/**,' +
// binary file types
'**/*.jar,**/*.zip,**/*.tgz,**/*.jpg,**/*.jpeg,**/*.gif,**/*.png,**/*.tif,**/*.webp,**/*.pdf,**/*.eot,**/*.ttf,**/*.woff,**/*.woff2,**/keystore,**/*.ico,**/*.xls,**/*.kdbx,**/*.bcmap,**/*.dat,**/*.cur,**/*.otf,**/*.zargo,**/*.gz',
// causes build to become unstable, concatenation prevents matching this line
highTags: 'FIX' + 'ME',
// does not cause build to become unstable
normalTags: 'TODO',
ignoreCase: true),
],
)
}
}
parallelBranches["Idea"] = {
nodeCheckoutAndDelete {
def ideaImage = docker.build(
imageName('Idea'),
'--build-arg JDK=' + jdk + ' ' +
'--build-arg IDEA=' + idea + ' ' +
'--build-arg IDEA_SHA256=' + ideaSHA256 + ' ' +
'conf/idea')
ideaImage.inside(dockerRunDefaults()) {
ant "src -Dskip.instrument=true"
shSilent "/opt/idea/bin/inspect.sh " + env.WORKSPACE + " 'Project Default' idea-inspection-output"
}
archiveArtifacts 'idea-inspection-output/**'
// replace project dir to prevent UnsupportedOperationException - will not be exposed in artifacts
shSilent "find idea-inspection-output -name '*.xml' | " +
"xargs --no-run-if-empty sed --in-place -- 's=\\\$PROJECT_DIR\\\$=" + env.WORKSPACE + "=g'"
recordIssues(
*: recordIssuesDefaults,
tools: [ideaInspection(pattern: 'idea-inspection-output/**')],
)
}
}
parallelBranches["Ivy"] = {
def cache = 'jenkins-build-survivor-' + projectName + "-Ivy"
lockNodeCheckoutAndDelete(cache) {
mainImage(imageName('Ivy')).inside(
dockerRunDefaults('bridge') +
"--mount type=volume,src=" + cache + ",target=/var/jenkins-build-survivor") {
ant "-buildfile ivy" +
" -Divy.user.home=/var/jenkins-build-survivor"
}
archiveArtifacts 'ivy/artifacts/report/**'
assertGitUnchanged()
// There should be an assertIvyExtends for each <conf name="abc" extends="def" /> in ivy/ivy.xml.
assertIvyExtends("test", "runtime")
assertIvyExtends("ide", "runtime")
assertIvyExtends("ide", "test")
assertIvyExtends("ide", "jsr305")
assertIvyExtends("ide", "ant")
assertIvyExtends("ide", "instrument")
}
}
parallel parallelBranches
tryCompleted = true
}
finally
{
if (!tryCompleted)
currentBuild.result = 'FAILURE'
// workaround for Mailer plugin: set result status explicitly to SUCCESS if empty, otherwise no mail will be triggered if a build gets successful after a previous unsuccessful build
if (currentBuild.result == null)
currentBuild.result = 'SUCCESS'
node('email') {
step(
[$class : 'Mailer',
recipients : emailextrecipients([isRelease ? culprits() : developers(), requestor()]),
notifyEveryUnstableBuild: true])
}
// https://docs.gitlab.com/ee/api/commits.html#post-the-build-status-to-a-commit
updateGitlabCommitStatus state: currentBuild.resultIsBetterOrEqualTo("SUCCESS") ? "success" : "failed"
}
// ------------------- LIBRARY ----------------------------
// The code below is meant to be equal across all projects.
void lockNodeCheckoutAndDelete(String resource, Closure body)
{
lock(resource) {
nodeCheckoutAndDelete(body)
}
}
void nodeCheckoutAndDelete(@ClosureParams(value = SimpleType, options = ["Map<String, String>"]) Closure body)
{
node('GitCloneExedio && docker') {
env.JENKINS_OWNER = shStdout("id --user") + ':' + shStdout("id --group")
try
{
deleteDir()
def scmResult = checkout scm
updateGitlabCommitStatus state: 'running'
body.call(scmResult)
}
finally
{
deleteDir()
}
}
}
String jobNameAndBuildNumber()
{
env.JOB_NAME.replace("/", "-").replace(" ", "_") + "-" + env.BUILD_NUMBER
}
def mainImage(String imageName)
{
return docker.build(
imageName,
'--build-arg JDK=' + jdk + ' ' +
'--build-arg JENKINS_OWNER=' + env.JENKINS_OWNER + ' ' +
'conf/main')
}
String imageName(String pipelineBranch, String subImage = '')
{
String isoToday = new Date().format("yyyyMMdd")
String name = 'exedio-jenkins:' + jobNameAndBuildNumber() + '-' + pipelineBranch + '-' + isoToday
if (!subImage.isBlank()) name += '-' + subImage
return name
}
static String dockerRunDefaults(String network = 'none', String hostname = '')
{
return "--cap-drop all " +
"--security-opt no-new-privileges " +
"--network " + network + " " +
(hostname != '' ? "--network-alias " + hostname + " " : "") +
"--dns-opt timeout:1 " + // seconds; default is 5
"--dns-opt attempts:1 " // default is 2
}
String makeBuildTag(Map<String, String> scmResult)
{
String treeHash = shStdout "git cat-file -p " + scmResult.GIT_COMMIT + " | grep '^tree ' | sed -e 's/^tree //'"
return 'build ' +
env.BRANCH_NAME + ' ' +
env.BUILD_NUMBER + ' ' +
new Date().format("yyyy-MM-dd") + ' ' +
scmResult.GIT_COMMIT + ' ' +
treeHash
}
void assertIvyExtends(String extendingConf, String parentConf)
{
int status = shStatus(
"LC_ALL=C" +
" diff --recursive lib/" + parentConf + " lib/" + extendingConf +
" | grep --invert-match '^Only in lib/" + extendingConf + ": '" +
" > ivy/artifacts/ivyExtends" + extendingConf + ".txt")
if (status != 0 && status != 1) // https://www.man7.org/linux/man-pages/man1/diff.1.html
{
error 'FAILURE because diff had trouble'
}
String result = readFile "ivy/artifacts/ivyExtends" + extendingConf + ".txt"
if (result != '')
{
error 'FAILURE because ivy conf "' + extendingConf + '" does not just add jar-files to "' + parentConf + '":\n' +
result
}
}
void shSilent(String script)
{
try
{
sh script
}
catch (Exception ignored)
{
currentBuild.result = 'FAILURE'
}
}
int shStatus(String script)
{
return (int) sh(script: script, returnStatus: true)
}
String shStdout(String script)
{
return ((String) sh(script: script, returnStdout: true)).trim()
}
void ant(String script)
{
shSilent 'java -jar lib/ant/ant-launcher.jar -noinput ' + script
}
void assertGitUnchanged()
{
String gitStatus = shStdout "git status --porcelain --untracked-files=normal"
if (gitStatus != '')
{
error 'FAILURE because fetching dependencies produces git diff:\n' + gitStatus
}
}