forked from apache/beam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon_job_properties.groovy
367 lines (331 loc) · 13.1 KB
/
common_job_properties.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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Contains functions that help build Jenkins projects. Functions typically set
// common properties that are shared among all Jenkins projects.
// Code in this directory should conform to the Groovy style guide.
// http://groovy-lang.org/style-guide.html
class common_job_properties {
static void setSCM(def context, String repositoryName) {
context.scm {
git {
remote {
// Double quotes here mean ${repositoryName} is interpolated.
github("apache/${repositoryName}")
// Single quotes here mean that ${ghprbPullId} is not interpolated and instead passed
// through to Jenkins where it refers to the environment variable.
refspec('+refs/heads/*:refs/remotes/origin/* ' +
'+refs/pull/${ghprbPullId}/*:refs/remotes/origin/pr/${ghprbPullId}/*')
}
branch('${sha1}')
extensions {
cleanAfterCheckout()
}
}
}
}
// Sets common top-level job properties for website repository jobs.
static void setTopLevelWebsiteJobProperties(def context,
String branch = 'asf-site') {
setTopLevelJobProperties(
context,
'beam-site',
branch,
'beam',
30)
}
// Sets common top-level job properties for main repository jobs.
static void setTopLevelMainJobProperties(def context,
String branch = 'master',
int timeout = 100,
String jenkinsExecutorLabel = 'beam') {
setTopLevelJobProperties(
context,
'beam',
branch,
jenkinsExecutorLabel,
timeout)
}
// Sets common top-level job properties. Accessed through one of the above
// methods to protect jobs from internal details of param defaults.
private static void setTopLevelJobProperties(def context,
String repositoryName,
String defaultBranch,
String jenkinsExecutorLabel,
int defaultTimeout) {
// GitHub project.
context.properties {
githubProjectUrl('https://github.com/apache/' + repositoryName + '/')
}
// Set JDK version.
context.jdk('JDK 1.8 (latest)')
// Restrict this project to run only on Jenkins executors as specified
context.label(jenkinsExecutorLabel)
// Discard old builds. Build records are only kept up to this number of days.
context.logRotator {
daysToKeep(14)
}
// Source code management.
setSCM(context, repositoryName)
context.parameters {
// This is a recommended setup if you want to run the job manually. The
// ${sha1} parameter needs to be provided, and defaults to the main branch.
stringParam(
'sha1',
defaultBranch,
'Commit id or refname (eg: origin/pr/9/head) you want to build.')
}
context.wrappers {
// Abort the build if it's stuck for more minutes than specified.
timeout {
absolute(defaultTimeout)
abortBuild()
}
// Set SPARK_LOCAL_IP for spark tests.
environmentVariables {
env('SPARK_LOCAL_IP', '127.0.0.1')
}
credentialsBinding {
string("COVERALLS_REPO_TOKEN", "beam-coveralls-token")
}
}
}
// Sets the pull request build trigger. Accessed through precommit methods
// below to insulate callers from internal parameter defaults.
private static void setPullRequestBuildTrigger(context,
String commitStatusContext,
String successComment = '--none--',
String prTriggerPhrase = '') {
context.triggers {
githubPullRequest {
admins(['asfbot'])
useGitHubHooks()
orgWhitelist(['apache'])
allowMembersOfWhitelistedOrgsAsAdmin()
permitAll()
// prTriggerPhrase is the argument which gets set when we want to allow
// post-commit builds to run against pending pull requests. This block
// overrides the default trigger phrase with the new one. Setting this
// will disable automatic invocation of this build; the phrase will be
// required to start it.
if (prTriggerPhrase) {
triggerPhrase(prTriggerPhrase)
onlyTriggerPhrase()
}
extensions {
commitStatus {
// This is the name that will show up in the GitHub pull request UI
// for this Jenkins project.
delegate.context("Jenkins: " + commitStatusContext)
}
// Comment messages after build completes.
buildStatus {
completedStatus('SUCCESS', successComment)
completedStatus('FAILURE', '--none--')
completedStatus('ERROR', '--none--')
}
}
}
}
}
// Sets common config for Maven jobs.
static void setMavenConfig(context, String mavenInstallation='Maven 3.3.3') {
context.mavenInstallation(mavenInstallation)
context.mavenOpts('-Dorg.slf4j.simpleLogger.showDateTime=true')
context.mavenOpts('-Dorg.slf4j.simpleLogger.dateTimeFormat=yyyy-MM-dd\\\'T\\\'HH:mm:ss.SSS')
// The -XX:+TieredCompilation -XX:TieredStopAtLevel=1 JVM options enable
// tiered compilation to make the JVM startup times faster during the tests.
context.mavenOpts('-XX:+TieredCompilation')
context.mavenOpts('-XX:TieredStopAtLevel=1')
context.rootPOM('pom.xml')
// Use a repository local to the workspace for better isolation of jobs.
context.localRepository(LocalRepositoryLocation.LOCAL_TO_WORKSPACE)
// Disable archiving the built artifacts by default, as this is slow and flaky.
// We can usually recreate them easily, and we can also opt-in individual jobs
// to artifact archiving.
if (context.metaClass.respondsTo(context, 'archivingDisabled', boolean)) {
context.archivingDisabled(true)
}
}
// Sets common config for PreCommit jobs.
static void setPreCommit(context,
String commitStatusName,
String successComment = '--none--') {
// Set pull request build trigger.
setPullRequestBuildTrigger(context, commitStatusName, successComment)
}
// Enable triggering postcommit runs against pull requests. Users can comment the trigger phrase
// specified in the postcommit job and have the job run against their PR to run
// tests not in the presubmit suite for additional confidence.
static void enablePhraseTriggeringFromPullRequest(context,
String commitStatusName,
String prTriggerPhrase) {
setPullRequestBuildTrigger(
context,
commitStatusName,
'--none--',
prTriggerPhrase)
}
// Sets common config for PostCommit jobs.
static void setPostCommit(context,
String buildSchedule = '0 */6 * * *',
boolean triggerEveryPush = true,
String notifyAddress = '[email protected]',
boolean emailIndividuals = true) {
// Set build triggers
context.triggers {
// By default runs every 6 hours.
cron(buildSchedule)
if (triggerEveryPush) {
githubPush()
}
}
context.publishers {
// Notify an email address for each failed build (defaults to commits@).
mailer(notifyAddress, false, emailIndividuals)
}
}
static def mapToArgString(LinkedHashMap<String, String> inputArgs) {
List argList = []
inputArgs.each({
// FYI: Replacement only works with double quotes.
key, value -> argList.add("--$key=$value")
})
return argList.join(' ')
}
// Configures the argument list for performance tests, adding the standard
// performance test job arguments.
private static def genPerformanceArgs(def argMap) {
LinkedHashMap<String, String> standardArgs = [
project: 'apache-beam-testing',
dpb_log_level: 'INFO',
maven_binary: '/home/jenkins/tools/maven/latest/bin/mvn',
bigquery_table: 'beam_performance.pkb_results',
// Publishes results with official tag, for use in dashboards.
official: 'true'
]
// Note: in case of key collision, keys present in ArgMap win.
LinkedHashMap<String, String> joinedArgs = standardArgs.plus(argMap)
return mapToArgString(joinedArgs)
}
// Adds the standard performance test job steps.
static def buildPerformanceTest(def context, def argMap) {
def pkbArgs = genPerformanceArgs(argMap)
context.steps {
// Clean up environment.
shell('rm -rf PerfKitBenchmarker')
// Clone appropriate perfkit branch
shell('git clone https://github.com/GoogleCloudPlatform/PerfKitBenchmarker.git')
// Install Perfkit benchmark requirements.
shell('pip install --user -r PerfKitBenchmarker/requirements.txt')
// Install job requirements for Python SDK.
shell('pip install --user -e sdks/python/[gcp,test]')
// Launch performance test.
shell("python PerfKitBenchmarker/pkb.py $pkbArgs")
}
}
/**
* Sets properties for all jobs which are run by a pipeline top-level (maven) job.
* @param context The delegate from the top level of a MavenJob.
* @param jobTimeout How long (in minutes) to wait for the job to finish.
* @param descriptor A short string identifying the job, e.g. "Java Unit Test".
*/
static def setPipelineJobProperties(def context, int jobTimeout, String descriptor) {
context.parameters {
stringParam(
'ghprbGhRepository',
'N/A',
'Repository name for use by ghprb plugin.')
stringParam(
'ghprbActualCommit',
'N/A',
'Commit ID for use by ghprb plugin.')
stringParam(
'ghprbPullId',
'N/A',
'PR # for use by ghprb plugin.')
}
// Set JDK version.
context.jdk('JDK 1.8 (latest)')
// Restrict this project to run only on Jenkins executors as specified
context.label('beam')
// Execute concurrent builds if necessary.
context.concurrentBuild()
context.wrappers {
timeout {
absolute(jobTimeout)
abortBuild()
}
credentialsBinding {
string("COVERALLS_REPO_TOKEN", "beam-coveralls-token")
}
downstreamCommitStatus {
delegate.context("Jenkins: ${descriptor}")
triggeredStatus("${descriptor} Pending")
startedStatus("Running ${descriptor}")
statusUrl()
completedStatus('SUCCESS', "${descriptor} Passed")
completedStatus('FAILURE', "${descriptor} Failed")
completedStatus('ERROR', "Error Executing ${descriptor}")
}
// Set SPARK_LOCAL_IP for spark tests.
environmentVariables {
env('SPARK_LOCAL_IP', '127.0.0.1')
}
}
// Set Maven parameters.
setMavenConfig(context)
}
/**
* Sets job properties common to pipeline jobs which are responsible for being the root of a
* build tree. Downstream jobs should pull artifacts from these jobs.
* @param context The delegate from the top level of a MavenJob.
*/
static def setPipelineBuildJobProperties(def context) {
context.properties {
githubProjectUrl('https://github.com/apache/beam/')
}
context.parameters {
stringParam(
'sha1',
'master',
'Commit id or refname (e.g. origin/pr/9/head) you want to build.')
}
// Source code management.
setSCM(context, 'beam')
}
/**
* Sets common job parameters for jobs which consume artifacts built for them by an upstream job.
* @param context The delegate from the top level of a MavenJob.
* @param jobName The job from which to copy artifacts.
*/
static def setPipelineDownstreamJobProperties(def context, String jobName) {
context.parameters {
stringParam(
'buildNum',
'N/A',
"Build number of ${jobName} to copy from.")
}
context.preBuildSteps {
copyArtifacts(jobName) {
buildSelector {
buildNumber('${buildNum}')
}
}
}
}
}