Repository navigation
Expand file tree
/
Copy pathgenerate_cloudbuild.py
More file actions
491 lines (460 loc) · 15.6 KB
/
Copy pathgenerate_cloudbuild.py
File metadata and controls
491 lines (460 loc) · 15.6 KB
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
import os
import sys
MAIN_TEMPLATE = """options:
logging: GCS_ONLY
machineType: 'E2_HIGHCPU_32'
env:
- DOCKER_CLI_EXPERIMENTAL=enabled
logsBucket: 'gs://cloud-sdk-docker-build-logs'
steps:
# Pin to qemu-v10.2.3-68 for stability and security
- name: 'tonistiigi/binfmt@sha256:400a4873b838d1b89194d982c45e5fb3cda4593fbfd7e08a02e76b03b21166f0'
args:
- '--install'
- 'arm64'
- name: 'gcr.io/cloud-builders/docker'
id: multi_arch_step1
args:
- 'buildx'
- 'create'
- '--name'
- 'mybuilder'
- name: 'gcr.io/cloud-builders/docker'
id: multi_arch_step2
args:
- 'buildx'
- 'use'
- 'mybuilder'
waitFor: ['multi_arch_step1']
- name: 'gcr.io/cloud-builders/docker'
id: multi_arch_step3
args:
- 'buildx'
- 'inspect'
- '--bootstrap'
waitFor: ['multi_arch_step2']
{SCANNINGSTEPS}
{MULTIARCH_BUILDSTEPS}
{TAG_AMD64_STEPS}{EMULATOR_TEST_STEPS}
{DOCKER_LOGIN_STEP}
{DOCKER_PUSHSTEPS}
{IMAGES_SECTION}
{SECRETS_SECTION}
substitutions:
_ALPINE_REGISTRY: 'docker.io/library'
timeout: 7200s"""
GCRIO_PROJECT='google.com/cloudsdktool'
DOCKERHUB_PREFIX='google'
GCR_PREFIXES = [
('us-docker.pkg.dev', 'gcr.io'),
('us-docker.pkg.dev', 'us.gcr.io'),
('europe-docker.pkg.dev', 'eu.gcr.io'),
('asia-docker.pkg.dev', 'asia.gcr.io'),
]
SCANNING_PREFIXES = [('us-docker.pkg.dev', 'scanning')]
OLD_NAME='cloud-sdk'
REBRAND_NAME='google-cloud-cli'
IMAGES=['alpine', 'debian_slim', 'default', 'debian_component_based', 'emulators', 'stable']
MULTI_ARCH=['debian_slim', 'debian_component_based', 'alpine', 'emulators', 'stable', 'default']
SCANNING_IMAGES=['all_components']
LABEL_FOR_IMAGE={
'alpine': 'alpine',
'debian_slim': 'slim',
'default': '',
'debian_component_based': 'debian_component_based',
'emulators': 'emulators',
'stable': 'stable',
'all_components': 'all_components'
}
def generate(is_test=False, is_rebuild=False):
"""Returns a Cloud Build config.
Args:
is_test: When true, builds the images locally for testing without pushing.
is_rebuild: When true, the config rebuilds an already released version: it
pushes only the floating and dated tags to Artifact Registry. The version
tags stay on the release image, and Docker Hub is skipped.
Raises:
ValueError: Both is_test and is_rebuild are true.
"""
if is_test and is_rebuild:
raise ValueError('is_test and is_rebuild are mutually exclusive.')
include_version_tags = not is_rebuild
def MakeScanningTags(label):
t = []
maybe_hyphen = '-' if label != 'latest' else ''
for gcr_prefix, gcr_suffix in SCANNING_PREFIXES:
t.append(
"'{gcrprefix}/{gcrio_project}/{gcrio_suffix}/{rebrand_name}:{label}'"
.format(
gcrprefix=gcr_prefix,
gcrio_project=GCRIO_PROJECT,
gcrio_suffix=gcr_suffix,
rebrand_name=REBRAND_NAME,
label=label,
)
)
if include_version_tags:
t.append(
"'{gcr_prefix}/{gcrio_project}/{gcrio_suffix}/{rebrand_name}:$TAG_NAME{maybe_hyphen}{label}'"
.format(
gcr_prefix=gcr_prefix,
gcrio_project=GCRIO_PROJECT,
gcrio_suffix=gcr_suffix,
rebrand_name=REBRAND_NAME,
maybe_hyphen=maybe_hyphen,
label=label,
)
)
t.append(
"'{gcr_prefix}/{gcrio_project}/{gcrio_suffix}/{rebrand_name}:$TAG_NAME{maybe_hyphen}{label}-$_DATE'"
.format(
gcr_prefix=gcr_prefix,
gcrio_project=GCRIO_PROJECT,
gcrio_suffix=gcr_suffix,
rebrand_name=REBRAND_NAME,
maybe_hyphen=maybe_hyphen,
label=label,
)
)
return t
def MakeGcrTags(
label_without_tag,
label_with_tag,
maybe_hyphen,
include_old_name=True,
include_rebrand_name=True,
):
t = []
for gcr_prefix, gcr_suffix in GCR_PREFIXES:
if include_old_name:
t.append(
"'{gcrprefix}/{gcrio_project}/{gcrio_suffix}/{old_name}:{label}'"
.format(
gcrprefix=gcr_prefix,
gcrio_project=GCRIO_PROJECT,
gcrio_suffix=gcr_suffix,
old_name=OLD_NAME,
label=label_without_tag,
)
)
if include_version_tags:
t.append(
"'{gcr_prefix}/{gcrio_project}/{gcrio_suffix}/{old_name}:$TAG_NAME{maybe_hyphen}{label}'"
.format(
gcr_prefix=gcr_prefix,
gcrio_project=GCRIO_PROJECT,
old_name=OLD_NAME,
gcrio_suffix=gcr_suffix,
maybe_hyphen=maybe_hyphen,
label=label_with_tag,
)
)
t.append(
"'{gcr_prefix}/{gcrio_project}/{gcrio_suffix}/{old_name}:$TAG_NAME{maybe_hyphen}{label}-$_DATE'"
.format(
gcr_prefix=gcr_prefix,
gcrio_project=GCRIO_PROJECT,
old_name=OLD_NAME,
gcrio_suffix=gcr_suffix,
maybe_hyphen=maybe_hyphen,
label=label_with_tag,
)
)
if include_rebrand_name:
t.append(
"'{gcrprefix}/{gcrio_project}/{gcrio_suffix}/{rebrand_name}:{label}'"
.format(
gcrprefix=gcr_prefix,
gcrio_project=GCRIO_PROJECT,
gcrio_suffix=gcr_suffix,
rebrand_name=REBRAND_NAME,
label=label_without_tag,
)
)
if include_version_tags:
t.append(
"'{gcr_prefix}/{gcrio_project}/{gcrio_suffix}/{rebrand_name}:$TAG_NAME{maybe_hyphen}{label}'"
.format(
gcr_prefix=gcr_prefix,
gcrio_project=GCRIO_PROJECT,
rebrand_name=REBRAND_NAME,
gcrio_suffix=gcr_suffix,
maybe_hyphen=maybe_hyphen,
label=label_with_tag,
)
)
t.append(
"'{gcr_prefix}/{gcrio_project}/{gcrio_suffix}/{rebrand_name}:$TAG_NAME{maybe_hyphen}{label}-$_DATE'"
.format(
gcr_prefix=gcr_prefix,
gcrio_project=GCRIO_PROJECT,
rebrand_name=REBRAND_NAME,
gcrio_suffix=gcr_suffix,
maybe_hyphen=maybe_hyphen,
label=label_with_tag,
)
)
return t
# Make all the tags and save them
tags = {}
multi_arch_tags = {}
for i in IMAGES:
tags[i] = []
if i in MULTI_ARCH:
multi_arch_tags[i] = []
label_name = LABEL_FOR_IMAGE[i]
label_without_tag = label_name
label_with_tag = label_name
maybe_hyphen = '-'
if i == 'default':
label_without_tag = 'latest'
maybe_hyphen = ''
# Make dockerhub tags for i. Rebuilds don't go to Docker Hub.
if not is_rebuild:
tags[i].append(
"'{dockerhub_prefix}/{old_name}:{label}'".format(
dockerhub_prefix=DOCKERHUB_PREFIX,
old_name=OLD_NAME,
label=label_without_tag,
)
)
tags[i].append(
"'{dockerhub_prefix}/{old_name}:$TAG_NAME{maybe_hyphen}{label}'"
.format(
dockerhub_prefix=DOCKERHUB_PREFIX,
old_name=OLD_NAME,
maybe_hyphen=maybe_hyphen,
label=label_with_tag,
)
)
# Make gcr tags for i
if i not in MULTI_ARCH:
tags[i].extend(
MakeGcrTags(label_without_tag, label_with_tag, maybe_hyphen)
)
else:
# old gcr tags go into tags
tags[i].extend(
MakeGcrTags(
label_without_tag,
label_with_tag,
maybe_hyphen,
include_rebrand_name=False,
)
)
# new gcr tags go into multiarch tags
multi_arch_tags[i].extend(
MakeGcrTags(
label_without_tag,
label_with_tag,
maybe_hyphen,
include_old_name=False,
)
)
# Make scanning tags and save them
scanning_tags = {}
for i in SCANNING_IMAGES:
scanning_tags[i] = []
label_name = LABEL_FOR_IMAGE[i]
if i == 'default':
label_name = 'latest'
scanning_tags[i].extend(MakeScanningTags(label_name))
scanning_steps = ''
for i in SCANNING_IMAGES:
image_directory = '{}/'.format(i)
if i == 'default':
image_directory = '.'
scanning_step = """- name: 'gcr.io/cloud-builders/docker'
id: scanning_{image_name}
args: ['build', '--target', 'prod', '--build-arg', 'CLOUD_SDK_VERSION=$_CLI_VERSION', {scanning_tags}, '{image_directory}']
waitFor: ['-']"""
output_scanning_step = scanning_step.format(
image_name=i,
scanning_tags=', '.join(
["'-t', {}".format(t) for t in scanning_tags[i]]
),
image_directory=image_directory,
)
if len(scanning_steps) > 0:
scanning_steps += '\n'
scanning_steps += output_scanning_step
tag_amd64_steps = ''
for i in IMAGES:
image_directory = '{}/'.format(i)
if i == 'default':
image_directory = '.'
if not is_test:
# Pull the dated tag, not the floating tag, so concurrent builds cannot
# swap the image mid-run.
primary_tag = next(
t for t in multi_arch_tags[i] if t.endswith("-$_DATE'")
).strip("'")
tag_cmds = ' && '.join([
'docker tag {} {}'.format(primary_tag, t.strip("'")) for t in tags[i]
])
step_template = """- name: 'gcr.io/cloud-builders/docker'
id: tag_amd64_{image_name}
entrypoint: 'bash'
args: ['-c', 'docker pull {primary_tag} && {tag_cmds}']
waitFor: ['multi_arch_{image_name}']"""
output_step = step_template.format(
image_name=i,
primary_tag=primary_tag,
tag_cmds=tag_cmds,
)
else:
target_flag = "'--target', 'prod', " if i == 'alpine' else ''
alpine_registry_flag = (
"'--build-arg', 'ALPINE_REGISTRY=$_ALPINE_REGISTRY', "
if i == 'alpine'
else ''
)
step_template = """- name: 'gcr.io/cloud-builders/docker'
id: tag_amd64_{image_name}
args: ['buildx', 'build', {target_flag}'--build-arg', 'CLOUD_SDK_VERSION=$_CLI_VERSION', {alpine_registry_flag}'--platform', 'linux/amd64', '--load', {tags}, '{image_directory}']
waitFor: ['multi_arch_{image_name}']"""
output_step = step_template.format(
image_name=i,
target_flag=target_flag,
alpine_registry_flag=alpine_registry_flag,
tags=', '.join(["'-t', {}".format(t) for t in tags[i]]),
image_directory=image_directory,
)
if len(tag_amd64_steps) > 0:
tag_amd64_steps += '\n'
tag_amd64_steps += output_step
multi_arch_build_steps = ''
push_flag = ", '--push'" if not is_test else ''
for i in MULTI_ARCH:
image_directory = '{}/'.format(i)
if i == 'default':
image_directory = '.'
target_flag = "'--target', 'prod', " if i == 'alpine' else ''
alpine_registry_flag = (
"'--build-arg', 'ALPINE_REGISTRY=$_ALPINE_REGISTRY', "
if i == 'alpine'
else ''
)
multi_arch_build_step = """- name: 'gcr.io/cloud-builders/docker'
id: multi_arch_{image_name}
args: ['buildx', 'build', {target_flag}'--build-arg', 'CLOUD_SDK_VERSION=$_CLI_VERSION', {alpine_registry_flag}'--platform', 'linux/arm64,linux/amd64', {tags}, '{image_directory}'{push_flag}]
waitFor: ['multi_arch_step3']"""
output_build_step = multi_arch_build_step.format(
image_name=i,
target_flag=target_flag,
alpine_registry_flag=alpine_registry_flag,
tags=', '.join(["'-t', {}".format(t) for t in multi_arch_tags[i]]),
image_directory=image_directory,
push_flag=push_flag,
)
if len(multi_arch_build_steps) > 0:
multi_arch_build_steps += '\n'
multi_arch_build_steps += output_build_step
docker_push_steps = ''
if not is_test:
for i in IMAGES:
push_step = """- name: 'gcr.io/cloud-builders/docker'
args: ['push', {tag}]
waitFor: ['dockersecret', '{build_step}']"""
for tag in tags[i]:
if tag.startswith("'google/cloud-sdk"):
if len(docker_push_steps) > 0:
docker_push_steps += '\n'
docker_push_steps += push_step.format(tag=tag, build_step='tag_amd64_' + i)
docker_login_step = ''
if not is_test and not is_rebuild:
docker_login_step = """- name: 'gcr.io/cloud-builders/docker'
id: dockersecret
entrypoint: 'bash'
args: ['-c', 'docker login --username=$_USERNAME --password=$$PASSWORD']
secretEnv: ['PASSWORD']"""
all_gcr_io_tags_for_images = ''
images_section = ''
if not is_test:
all_images_tags = []
for i in IMAGES:
all_images_tags.extend(
[t for t in tags[i] if not t.startswith("'google/cloud-sdk")]
)
for i in SCANNING_IMAGES:
all_images_tags.extend(t for t in scanning_tags[i])
for tag in sorted(all_images_tags):
if len(all_gcr_io_tags_for_images) > 0:
all_gcr_io_tags_for_images += '\n'
all_gcr_io_tags_for_images += '- {}'.format(tag)
images_section = 'images:\n' + all_gcr_io_tags_for_images
secrets_section = ''
if not is_test and not is_rebuild:
secrets_section = """secrets:
- kmsKeyName: projects/google.com:cloudsdktool/locations/global/keyRings/docker/cryptoKeys/dockerhub-password
secretEnv:
PASSWORD: |
CiQA9btlfpg/kWmwXQvrNXtkVpu2tDdD2VOi1FYd3mmjCUGaK4YSNwC8sn1MepjracHAg8VAQEWm
s26BTGccqD1NTS83DGFdY9moRGhSPm4WJKCg2tTQKYeTfdqUjjM="""
emulator_test_steps = ''
if is_test:
emulator_test_steps = """
- name: 'google/cloud-sdk:emulators'
id: test_emulators
entrypoint: 'bash'
args: ['./emulators/test-all-emulators.sh']
waitFor: ['tag_amd64_emulators']
- name: 'google/cloud-sdk:latest'
id: test_latest
entrypoint: 'bash'
args: ['./test-latest.sh']
waitFor: ['tag_amd64_default']
- name: 'google/cloud-sdk:debian_component_based'
id: test_debian_component_based
entrypoint: 'bash'
args: ['-c', 'cd debian_component_based && ./test-components.sh']
waitFor: ['tag_amd64_debian_component_based']
- name: 'google/cloud-sdk:alpine'
id: test_alpine_gocloud
entrypoint: 'bash'
args: ['./test-gocloud.sh']
waitFor: ['tag_amd64_alpine']"""
return MAIN_TEMPLATE.format(
SCANNINGSTEPS=scanning_steps,
MULTIARCH_BUILDSTEPS=multi_arch_build_steps,
TAG_AMD64_STEPS=tag_amd64_steps,
EMULATOR_TEST_STEPS=emulator_test_steps,
DOCKER_LOGIN_STEP=docker_login_step,
DOCKER_PUSHSTEPS=docker_push_steps,
IMAGES_SECTION=images_section,
SECRETS_SECTION=secrets_section,
)
def main():
workspace_dir = os.environ.get('BUILD_WORKSPACE_DIRECTORY')
if workspace_dir:
yaml_file = os.path.join(
workspace_dir,
'third_party/gcloud_cli/external/cloud_sdk_docker/cloudbuild.yaml',
)
test_yaml_file = os.path.join(
workspace_dir,
'third_party/gcloud_cli/external/cloud_sdk_docker/cloudbuild_test.yaml',
)
rebuild_yaml_file = os.path.join(
workspace_dir,
'third_party/gcloud_cli/external/cloud_sdk_docker/cloudbuild_rebuild.yaml',
)
# Generate cloudbuild.yaml
with open(yaml_file, 'w') as f:
f.write(generate(is_test=False) + '\n')
# Generate cloudbuild_rebuild.yaml
with open(rebuild_yaml_file, 'w') as f:
f.write(generate(is_rebuild=True) + '\n')
# Generate cloudbuild_test.yaml
with open(test_yaml_file, 'w') as f:
f.write(
'# IT IS A TEST\n' + generate(is_test=True) + '\n'
)
print('Successfully updated all cloudbuild yaml files.')
else:
is_test = 'is_test' in sys.argv
if is_test:
print('# IT IS A TEST')
print(generate(is_test=is_test, is_rebuild='is_rebuild' in sys.argv))
if __name__ == '__main__':
main()