forked from coreos/fedora-coreos-pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
571 lines (510 loc) · 23.5 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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
import org.yaml.snakeyaml.Yaml;
def utils, streams, official, official_jenkins, developer_prefix, src_config_url, src_config_ref, s3_bucket
node {
checkout scm
utils = load("utils.groovy")
streams = load("streams.groovy")
pod = readFile(file: "manifests/pod.yaml")
// just autodetect if we're in the official prod Jenkins or not
official_jenkins = (env.JENKINS_URL == 'https://jenkins-fedora-coreos.apps.ocp.ci.centos.org/')
def official_job = (env.JOB_NAME == 'fedora-coreos/fedora-coreos-fedora-coreos-pipeline')
official = (official_jenkins && official_job)
if (official) {
echo "Running in official (prod) mode."
} else {
echo "Running in developer mode on ${env.JENKINS_URL}."
}
developer_prefix = utils.get_pipeline_annotation('developer-prefix')
src_config_url = utils.get_pipeline_annotation('source-config-url')
src_config_ref = utils.get_pipeline_annotation('source-config-ref')
s3_bucket = utils.get_pipeline_annotation('s3-bucket')
gcp_gs_bucket = utils.get_pipeline_annotation('gcp-gs-bucket')
// sanity check that a valid prefix is provided if in devel mode and drop
// the trailing '-' in the devel prefix
if (!official) {
assert developer_prefix.length() > 0 : "Missing developer prefix"
assert developer_prefix.endsWith("-") : "Missing trailing dash in developer prefix"
developer_prefix = developer_prefix[0..-2]
}
}
// Share with the Fedora testing account so we can test it afterwards
FEDORA_AWS_TESTING_USER_ID = "013116697141"
def coreos_assembler_image
if (official) {
coreos_assembler_image = "coreos-assembler:master"
} else {
coreos_assembler_image = "${developer_prefix}-coreos-assembler:master"
}
properties([
pipelineTriggers([]),
parameters([
choice(name: 'STREAM',
// list devel first so that it's the default choice
choices: (streams.development + streams.production + streams.mechanical),
description: 'Fedora CoreOS stream to build'),
string(name: 'VERSION',
description: 'Override default versioning mechanism',
defaultValue: '',
trim: true),
booleanParam(name: 'FORCE',
defaultValue: false,
description: 'Whether to force a rebuild'),
booleanParam(name: 'MINIMAL',
defaultValue: (official ? false : true),
description: 'Whether to only build the OSTree and qemu images'),
booleanParam(name: 'ALLOW_KOLA_UPGRADE_FAILURE',
defaultValue: false,
description: "Don't error out if upgrade tests fail (temporary)"),
// use a string here because passing booleans via `oc start-build -e`
// is non-trivial
choice(name: 'AWS_REPLICATION',
choices: (['false', 'true']),
description: 'Force AWS AMI replication for non-production'),
string(name: 'COREOS_ASSEMBLER_IMAGE',
description: 'Override coreos-assembler image to use',
defaultValue: "${coreos_assembler_image}",
trim: true),
]),
buildDiscarder(logRotator(
numToKeepStr: '60',
artifactNumToKeepStr: '3'
)),
durabilityHint('PERFORMANCE_OPTIMIZED')
])
// Note the supermin VM just uses 2G. The really hungry part is xz, which
// without lots of memory takes lots of time. For now we just hardcode these
// here; we can look into making them configurable through the template if
// developers really need to tweak them (note that in the default minimal devel
// workflow, only the qemu image is built).
def cosa_memory_request_mb
if (official) {
cosa_memory_request_mb = 6.5 * 1024
} else {
cosa_memory_request_mb = 2.5 * 1024
}
cosa_memory_request_mb = cosa_memory_request_mb as Integer
// substitute the right COSA image and mem request into the pod definition before spawning it
pod = pod.replace("COREOS_ASSEMBLER_MEMORY_REQUEST", "${cosa_memory_request_mb}Mi")
pod = pod.replace("COREOS_ASSEMBLER_IMAGE", params.COREOS_ASSEMBLER_IMAGE)
def podYaml = readYaml(text: pod);
// And re-serialize; I couldn't figure out how to dump to a string
// in a way allowed by the Groovy sandbox. Tempting to just tell people
// to disable that.
node {
def tmpPath = "${WORKSPACE}/pod.yaml";
sh("rm -vf ${tmpPath}")
writeYaml(file: tmpPath, data: podYaml);
pod = readFile(file: tmpPath);
sh("rm -vf ${tmpPath}")
}
echo "Final podspec: ${pod}"
// use a unique label to force Kubernetes to provision a separate pod per run
def pod_label = "cosa-${UUID.randomUUID().toString()}"
echo "Waiting for build-${params.STREAM} lock"
currentBuild.description = "[${params.STREAM}] Waiting"
lock(resource: "build-${params.STREAM}") {
currentBuild.description = "[${params.STREAM}] Running"
podTemplate(cloud: 'openshift', label: pod_label, yaml: pod) {
node(pod_label) { container('coreos-assembler') {
// declare this early so we can use it in Slack
def newBuildID
try { timeout(time: 240, unit: 'MINUTES') {
// Clone the automation repo, which contains helper scripts. In the
// future, we'll probably want this either part of the cosa image, or
// in a derivative of cosa for pipeline needs.
utils.shwrap("""
git clone --depth=1 https://github.com/coreos/fedora-coreos-releng-automation /var/tmp/fcos-releng
""")
// this is defined IFF we *should* and we *can* upload to S3
def s3_stream_dir
if (s3_bucket && utils.path_exists("\${AWS_FCOS_BUILDS_BOT_CONFIG}")) {
if (official) {
// see bucket layout in https://github.com/coreos/fedora-coreos-tracker/issues/189
s3_stream_dir = "${s3_bucket}/prod/streams/${params.STREAM}"
} else {
// One prefix = one pipeline = one stream; the deploy script is geared
// towards testing a specific combination of (cosa, pipeline, fcos config),
// not a full duplication of all the prod streams. One can always instantiate
// a second prefix to test a separate combination if more than 1 concurrent
// devel pipeline is needed.
s3_stream_dir = "${s3_bucket}/devel/streams/${developer_prefix}"
}
}
def developer_builddir = "/srv/devel/${developer_prefix}/build"
def basearch = utils.shwrap_capture("cosa basearch")
stage('Init') {
def ref = params.STREAM
if (src_config_ref != "") {
assert !official : "Asked to override ref in official mode"
ref = src_config_ref
}
// for now, just use the PVC to keep cache.qcow2 in a stream-specific dir
def cache_img
if (official) {
cache_img = "/srv/prod/${params.STREAM}/cache.qcow2"
} else {
cache_img = "/srv/devel/${developer_prefix}/cache.qcow2"
}
utils.shwrap("""
cosa init --force --branch ${ref} ${src_config_url}
mkdir -p \$(dirname ${cache_img})
ln -s ${cache_img} cache/cache.qcow2
""")
// If the cache img is larger than 7G, then nuke it. Otherwise
// it'll just keep growing and we'll hit ENOSPC. It'll get rebuilt.
utils.shwrap("""
if [ -f ${cache_img} ] && [ \$(du ${cache_img} | cut -f1) -gt \$((1024*1024*7)) ]; then
rm -vf ${cache_img}
fi
""")
}
def parent_version = ""
def parent_commit = ""
stage('Fetch') {
if (s3_stream_dir) {
utils.aws_s3_cp_allow_noent("s3://${s3_stream_dir}/releases.json", "tmp/releases.json")
if (utils.path_exists("tmp/releases.json")) {
def releases = readJSON file: "tmp/releases.json"
// check if there's a previous release we should use as parent
if (releases["releases"].size() > 0) {
def commit_obj = releases["releases"][-1]["commits"].find{ commit -> commit["architecture"] == basearch }
parent_commit = commit_obj["checksum"]
parent_version = releases["releases"][-1]["version"]
}
}
utils.shwrap("""
export AWS_CONFIG_FILE=\${AWS_FCOS_BUILDS_BOT_CONFIG}
cosa buildprep s3://${s3_stream_dir}/builds
""")
if (parent_version != "") {
// also fetch the parent version; this is used by cosa to do the diff
utils.shwrap("""
export AWS_CONFIG_FILE=\${AWS_FCOS_BUILDS_BOT_CONFIG}
cosa buildprep s3://${s3_stream_dir}/builds --build ${parent_version}
""")
}
} else if (!official && utils.path_exists(developer_builddir)) {
utils.shwrap("""
cosa buildprep ${developer_builddir}
""")
}
utils.shwrap("""
cosa fetch --strict
""")
}
def prevBuildID = null
if (utils.path_exists("builds/latest")) {
prevBuildID = utils.shwrap_capture("readlink builds/latest")
}
stage('Build') {
def parent_arg = ""
if (parent_version != "") {
parent_arg = "--parent-build ${parent_version}"
}
def force = params.FORCE ? "--force" : ""
def version = ""
if (params.VERSION) {
version = "--version ${params.VERSION}"
} else if (official) {
def new_version = utils.shwrap_capture("/var/tmp/fcos-releng/scripts/versionary.py")
version = "--version ${new_version}"
}
utils.shwrap("""
cosa build ostree --strict --skip-prune ${force} ${version} ${parent_arg}
""")
}
def meta_json
def buildID = utils.shwrap_capture("readlink builds/latest")
if (prevBuildID == buildID) {
currentBuild.result = 'SUCCESS'
currentBuild.description = "[${params.STREAM}] 💤 (no new build)"
return
} else {
newBuildID = buildID
currentBuild.description = "[${params.STREAM}] ⚡ ${newBuildID}"
meta_json = "builds/${newBuildID}/${basearch}/meta.json"
// and insert the parent info into meta.json so we can display it in
// the release browser and for sanity checking
if (parent_commit && parent_version) {
def meta = readJSON file: meta_json
meta["fedora-coreos.parent-version"] = parent_version
meta["fedora-coreos.parent-commit"] = parent_commit
writeJSON file: meta_json, json: meta
}
}
if (official && s3_stream_dir && utils.path_exists("/etc/fedora-messaging-cfg/fedmsg.toml")) {
stage('Sign OSTree') {
utils.shwrap("""
export AWS_CONFIG_FILE=\${AWS_FCOS_BUILDS_BOT_CONFIG}
cosa sign robosignatory --s3 ${s3_stream_dir}/builds \
--extra-fedmsg-keys stream=${params.STREAM} \
--ostree --gpgkeypath /etc/pki/rpm-gpg \
--fedmsg-conf /etc/fedora-messaging-cfg/fedmsg.toml
""")
}
}
stage('Build QEMU') {
utils.shwrap("""
cosa buildextend-qemu
""")
}
stage('Kola:QEMU basic') {
utils.shwrap("""
cosa kola run --basic-qemu-scenarios --no-test-exit-error
tar -cf - tmp/kola/ | xz -c9 > kola-run-basic.tar.xz
""")
archiveArtifacts "kola-run-basic.tar.xz"
}
if (!utils.checkKolaSuccess("tmp/kola", currentBuild)) {
return
}
stage('Kola:QEMU') {
// leave 512M for overhead; VMs are 1G each
def parallel = ((cosa_memory_request_mb - 512) / 1024) as Integer
utils.shwrap("""
cosa kola run --parallel ${parallel} --no-test-exit-error
tar -cf - tmp/kola/ | xz -c9 > kola-run.tar.xz
""")
archiveArtifacts "kola-run.tar.xz"
}
if (!utils.checkKolaSuccess("tmp/kola", currentBuild)) {
return
}
stage('Kola:QEMU upgrade') {
utils.shwrap("""
cosa kola --upgrades --no-test-exit-error
tar -cf - tmp/kola-upgrade | xz -c9 > kola-run-upgrade.tar.xz
""")
archiveArtifacts "kola-run-upgrade.tar.xz"
}
if (!params.ALLOW_KOLA_UPGRADE_FAILURE && !utils.checkKolaSuccess("tmp/kola-upgrade", currentBuild)) {
return
}
if (!params.MINIMAL) {
stage("Metal") {
parallel metal: {
utils.shwrap("""
cosa buildextend-metal
""")
}, metal4k: {
utils.shwrap("""
cosa buildextend-metal4k
""")
}
}
stage('Build Live') {
utils.shwrap("""
cosa buildextend-live
""")
}
stage('Test Live ISO') {
// compress the metal and metal4k images now so we're testing
// installs with the image format we ship
// lower to make sure we don't go over and account for overhead
def xz_memlimit = cosa_memory_request_mb - 512
utils.shwrap("""
export XZ_DEFAULTS=--memlimit=${xz_memlimit}Mi
cosa compress --compressor xz --artifact metal --artifact metal4k
""")
try {
parallel metal: {
utils.shwrap("kola testiso -S --output-dir tmp/kola-metal")
}, metal4k: {
utils.shwrap("kola testiso -SP --qemu-native-4k --output-dir tmp/kola-metal4k")
}
} catch (Throwable e) {
archiveArtifacts "builds/latest/**/*.iso"
throw e
} finally {
utils.shwrap("tar -cf - tmp/kola-metal/ | xz -c9 > ${env.WORKSPACE}/kola-testiso-metal.tar.xz")
utils.shwrap("tar -cf - tmp/kola-metal4k/ | xz -c9 > ${env.WORKSPACE}/kola-testiso-metal4k.tar.xz")
archiveArtifacts allowEmptyArchive: true, artifacts: 'kola-testiso*.tar.xz'
}
}
// parallel build these artifacts
def pbuilds = [:]
["Aliyun", "AWS", "Azure", "DigitalOcean", "Exoscale", "GCP", "IBMCloud", "OpenStack", "VMware", "Vultr"].each {
pbuilds[it] = {
def cmd = it.toLowerCase()
utils.shwrap("""
cosa buildextend-${cmd}
""")
}
}
parallel pbuilds
// Key off of s3_stream_dir: i.e. if we're configured to upload artifacts
// to S3, we also take that to mean we should upload an AMI. We could
// split this into two separate developer knobs in the future.
if (s3_stream_dir) {
stage('Upload AWS') {
def suffix = official ? "" : "--name-suffix ${developer_prefix}"
// XXX: hardcode us-east-1 for now
// XXX: use the temporary 'ami-import' subpath for now; once we
// also publish vmdks, we could make this more efficient by
// uploading first, and then pointing ore at our uploaded vmdk
utils.shwrap("""
export AWS_CONFIG_FILE=\${AWS_FCOS_BUILDS_BOT_CONFIG}
cosa buildextend-aws ${suffix} \
--upload \
--build=${newBuildID} \
--region=us-east-1 \
--bucket s3://${s3_bucket}/ami-import \
--grant-user ${FEDORA_AWS_TESTING_USER_ID}
""")
}
}
// If there is a config for GCP then we'll upload our image to GCP
if (utils.path_exists("\${GCP_IMAGE_UPLOAD_CONFIG}")) {
stage('Upload GCP') {
utils.shwrap("""
# pick up the project to use from the config
gcp_project=\$(jq -r .project_id \${GCP_IMAGE_UPLOAD_CONFIG})
# collect today's date for the description
today=\$(date +%Y-%m-%d)
# NOTE: Add --deprecated to create image in deprecated state.
# We undeprecate in the release pipeline with promote-image.
cosa buildextend-gcp \
--log-level=INFO \
--build=${newBuildID} \
--upload \
--create-image=true \
--deprecated \
--family fedora-coreos-${params.STREAM} \
--license fedora-coreos-${params.STREAM} \
--license "https://compute.googleapis.com/compute/v1/projects/vm-options/global/licenses/enable-vmx" \
--project=\${gcp_project} \
--bucket gs://${gcp_gs_bucket}/image-import \
--json \${GCP_IMAGE_UPLOAD_CONFIG} \
--description=\"Fedora, Fedora CoreOS ${params.STREAM}, ${newBuildID}, ${basearch} published on \$today\"
""")
}
}
}
stage('Archive') {
// lower to make sure we don't go over and account for overhead
def xz_memlimit = cosa_memory_request_mb - 512
utils.shwrap("""
export XZ_DEFAULTS=--memlimit=${xz_memlimit}Mi
cosa compress --compressor xz
""")
// Run the coreos-meta-translator against the most recent build,
// which will generate a release.json from the meta.json files
utils.shwrap("""
/var/tmp/fcos-releng/coreos-meta-translator/trans.py --workdir .
""")
if (s3_stream_dir) {
// just upload as public-read for now, but see discussions in
// https://github.com/coreos/fedora-coreos-tracker/issues/189
utils.shwrap("""
export AWS_CONFIG_FILE=\${AWS_FCOS_BUILDS_BOT_CONFIG}
cosa buildupload s3 --acl=public-read ${s3_stream_dir}/builds
""")
} else if (!official) {
// In devel mode without an S3 server, just archive into the PVC
// itself. Otherwise there'd be no other way to retrieve the
// artifacts. But note we only keep one build at a time.
utils.shwrap("""
rm -rf ${developer_builddir}
mkdir -p ${developer_builddir}
cp -aT builds ${developer_builddir}
""")
}
}
// These steps interact with Fedora Infrastructure/Releng for
// signing of artifacts and importing of OSTree commits. They
// must be run after the archive stage because the artifacts
// are pulled from their S3 locations.
if (official && s3_stream_dir && utils.path_exists("/etc/fedora-messaging-cfg/fedmsg.toml")) {
stage('Sign Images') {
utils.shwrap("""
export AWS_CONFIG_FILE=\${AWS_FCOS_BUILDS_BOT_CONFIG}
cosa sign robosignatory --s3 ${s3_stream_dir}/builds \
--extra-fedmsg-keys stream=${params.STREAM} \
--images --gpgkeypath /etc/pki/rpm-gpg \
--fedmsg-conf /etc/fedora-messaging-cfg/fedmsg.toml
""")
}
stage("OSTree Import: Compose Repo") {
utils.shwrap("""
/var/tmp/fcos-releng/coreos-ostree-importer/send-ostree-import-request.py \
--build=${newBuildID} --s3=${s3_stream_dir} --repo=compose \
--fedmsg-conf=/etc/fedora-messaging-cfg/fedmsg.toml
""")
}
}
// Now that the metadata is uploaded go ahead and kick off some tests
if (!params.MINIMAL && s3_stream_dir &&
utils.path_exists("\${AWS_FCOS_KOLA_BOT_CONFIG}")) {
stage('Kola:AWS') {
// We consider the AWS kola tests to be a followup job, so we use `wait: false` here.
build job: 'kola-aws', wait: false, parameters: [
string(name: 'STREAM', value: params.STREAM),
string(name: 'VERSION', value: newBuildID),
string(name: 'S3_STREAM_DIR', value: s3_stream_dir)
]
}
}
// Now that the metadata is uploaded go ahead and kick off some tests
if (!params.MINIMAL && s3_stream_dir &&
utils.path_exists("\${GCP_IMAGE_UPLOAD_CONFIG}")) {
stage('Kola:GCP') {
// We consider the GCP kola tests to be a followup job, so we use `wait: false` here.
build job: 'kola-gcp', wait: false, parameters: [
string(name: 'STREAM', value: params.STREAM),
string(name: 'VERSION', value: newBuildID),
string(name: 'S3_STREAM_DIR', value: s3_stream_dir)
]
}
}
// For now, we auto-release all non-production streams builds. That
// way, we can e.g. test testing-devel AMIs easily.
//
// Since we are only running this stage for non-production (i.e. mechanical
// and development) builds we'll default to not doing AWS AMI replication.
// That can be overridden by the user setting the AWS_REPLICATION parameter
// to true, overriding the default (false).
if (official && !(params.STREAM in streams.production)) {
stage('Publish') {
// use jnlp container in our pod, which has `oc` in it already
container('jnlp') {
utils.shwrap("""
oc start-build --wait fedora-coreos-pipeline-release \
-e STREAM=${params.STREAM} \
-e VERSION=${newBuildID} \
-e AWS_REPLICATION=${params.AWS_REPLICATION}
""")
}
}
}
currentBuild.result = 'SUCCESS'
// main timeout and try {} finish here
}} catch (e) {
currentBuild.result = 'FAILURE'
throw e
} finally {
def color
def message = "[${params.STREAM}] <${env.BUILD_URL}|${env.BUILD_NUMBER}>"
if (currentBuild.result == 'SUCCESS') {
if (!newBuildID) {
// SUCCESS, but no new builds? Must've been a no-op
return
}
message = ":fcos: :sparkles: ${message} - SUCCESS"
color = 'good';
} else {
message = ":fcos: :trashfire: ${message} - FAILURE"
color = 'danger';
}
if (newBuildID) {
message = "${message} (${newBuildID})"
}
try {
if (official) {
slackSend(color: color, message: message)
}
} finally {
echo message
}
}
}}
}}