Use CircleCI version 2.1 at the top of your .circleci/config.yml file.
1
version: 2.1
Add the orbs
stanza below your version, invoking the orb:
1
2
orbs:
gcp-gcr: circleci/gcp-gcr@0.16.10
Use gcp-gcr
elements in your existing workflows and jobs.
Log into Google Cloud Plaform, build and push image to GCR, then echo the image digest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
version: '2.1'
orbs:
gcp-gcr: circleci/gcp-gcr@x.y.z
jobs:
build-and-push:
executor: gcp-gcr/default
steps:
- checkout
- gcp-gcr/gcr-auth
- gcp-gcr/build-image:
image: orb-test
no_output_timeout: 20m
registry-url: eu.gcr.io
- gcp-gcr/push-image:
digest-path: /tmp/digest.txt
image: orb-test
registry-url: eu.gcr.io
- run:
command: |
echo "Digest is: $(</tmp/digest.txt)"
workflows:
commit:
jobs:
- build-and-push
Log into Google Cloud Plaform, then build and push image to GCR. Uses a custom executor to reduce the setup time.
1
2
3
4
5
6
7
8
9
10
11
12
13
version: '2.1'
orbs:
gcp-gcr: circleci/gcp-gcr@x.y.z
workflows:
build_and_push_image:
jobs:
- gcp-gcr/build-and-push-image:
context: myContext
executor: my-executor
image: my-image
registry-url: us.gcr.io
setup-remote-docker: true
use-docker-layer-caching: true
Log into Google Cloud Plaform, then build and push image to GCR
1
2
3
4
5
6
7
8
9
10
version: '2.1'
orbs:
gcp-gcr: circleci/gcp-gcr@x.y.z
workflows:
build_and_push_image:
jobs:
- gcp-gcr/build-and-push-image:
context: myContext
image: my-image
registry-url: us.gcr.io
Log into Google Cloud Plaform, then build and push image to Artifact Registry
1
2
3
4
5
6
7
8
9
10
11
version: '2.1'
orbs:
gcp-gcr: circleci/gcp-gcr@x.y.z
workflows:
build_and_push_image:
jobs:
- gcp-gcr/build-and-push-image:
context: myContext
image: my-image
registry-url: us-central1-docker.pkg.dev
repository: my-repo
Log into Google Cloud Plaform, then tag an existing image with "latest"
1
2
3
4
5
6
7
8
9
10
11
12
version: '2.1'
orbs:
gcp-gcr: circleci/gcp-gcr@x.y.z
workflows:
build_and_push_image:
jobs:
- gcp-gcr/tag-image:
context: myContext
image: my-image
registry-url: us.gcr.io
source-tag: mytag1
target-tag: mytag2
Install GCP CLI, if needed, and configure. Adds a tag to an existing image.
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
executor | executor to use for this job | No | default | executor |
gcloud-service-key | The gcloud service key | No | GCLOUD_SERVICE_KEY | env_var_name |
gcloud_components | The list of gcloud components to install. Space separated. See https://cloud.google.com/sdk/docs/components for additional info.
| No | '' | string |
gcloud_version | Version of gcloud CLI to install.
| No | latest | string |
gcp_cred_config_file_path | Output location of OIDC credentials. | No | ~/gcp_cred_config.json | string |
google-compute-region | The Google compute zone to connect with via the gcloud CLI | No | GOOGLE_COMPUTE_REGION | env_var_name |
google-compute-zone | The Google compute zone to connect with via the gcloud CLI | No | GOOGLE_COMPUTE_ZONE | env_var_name |
google-project-id | The Google project ID to connect with via the gcloud CLI | No | GOOGLE_PROJECT_ID | env_var_name |
google_project_number | Name of environment variable storing the Google project number
used to configure OIDC.
| No | GOOGLE_PROJECT_NUMBER | env_var_name |
image | A name for your Docker image | Yes | - | string |
registry-url | The GCR registry URL from ['', us, eu, asia].gcr.io | No | gcr.io | string |
service_account_email | Environment variable containing OIDC service account email. | No | OIDC_SERVICE_ACCOUNT_EMAIL | env_var_name |
source-tag | An existing Docker image tag | Yes | - | string |
target-tag | A new Docker image tag | Yes | - | string |
use_oidc | Set to true to enable OIDC | No | false | boolean |
workload_identity_pool_id | Environment variable containing OIDC configured workload identity pool is stored.
| No | OIDC_WIP_ID | env_var_name |
workload_identity_pool_provider_id | Environment variable containing OIDC configured workload identity pool provider ID is stored.
| No | OIDC_WIP_PROVIDER_ID | env_var_name |
Install GCP CLI, if needed, and configure. Build and push image to repository.
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
attach-workspace | Boolean for whether or not to attach to an existing workspace. Default is false.
| No | false | boolean |
digest-path | (Optional) The path to save the RepoDigest of the pushed image | No | '' | string |
docker-context | Path to the directory containing your build context, defaults to . (working directory)
| No | . | string |
dockerfile | Name of dockerfile to use, defaults to Dockerfile | No | Dockerfile | string |
executor | executor to use for this job | No | default | executor |
extra_build_args | Extra flags to pass to docker build. For examples, see https://docs.docker.com/engine/reference/commandline/build
| No | '' | string |
gcloud-service-key | The gcloud service key | No | GCLOUD_SERVICE_KEY | env_var_name |
gcloud_components | The list of gcloud components to install. Space separated. See https://cloud.google.com/sdk/docs/components for additional info.
| No | '' | string |
gcloud_version | Version of gcloud CLI to install.
| No | latest | string |
gcp_cred_config_file_path | Output location of OIDC credentials. | No | ~/gcp_cred_config.json | string |
google-compute-region | The Google compute zone to connect with via the gcloud CLI | No | GOOGLE_COMPUTE_REGION | env_var_name |
google-compute-zone | The Google compute zone to connect with via the gcloud CLI | No | GOOGLE_COMPUTE_ZONE | env_var_name |
google-project-id | The Google project ID to connect with via the gcloud CLI | No | GOOGLE_PROJECT_ID | env_var_name |
google_project_number | Name of environment variable storing the Google project number
used to configure OIDC.
| No | GOOGLE_PROJECT_NUMBER | env_var_name |
image | A name for your Docker image | Yes | - | string |
no_output_timeout | Pass through a default timeout if your Docker build does not output anything for more than 10 minutes.
| No | 10m | string |
path | Path to the directory containing your Dockerfile, defaults to . (working directory)
| No | . | string |
registry-url | The GCR registry URL from ['', us, eu, asia].gcr.io, or an artifact registry url from [GOOGLE_COMPUTE_REGION, us, eu, asia]-docker.pkg.dev
| No | gcr.io | string |
remote-docker-version | Specify the remote docker version. See: https://circleci.com/docs/2.0/building-docker-images/#docker-version
| No | docker24 | string |
repository | The Artifact Registry requires a HOST-NAME/PROJECT-ID/REPOSITORY/IMAGE format. If pushing to the Artifact Registry, the repository to push the image to
| No | '' | string |
service_account_email | Environment variable containing OIDC service account email. | No | OIDC_SERVICE_ACCOUNT_EMAIL | env_var_name |
setup-remote-docker | Setup and use CircleCI's remote Docker environment for Docker and docker-compose commands? Not required if using the default executor
| No | false | boolean |
tag | A Docker image tag | No | latest | string |
use-docker-layer-caching | Setup docker layer caching for optimized build. Not available if using the default executor.
| No | false | boolean |
use_oidc | Set to true to enable OIDC | No | false | boolean |
workload_identity_pool_id | Environment variable containing OIDC configured workload identity pool is stored.
| No | OIDC_WIP_ID | env_var_name |
workload_identity_pool_provider_id | Environment variable containing OIDC configured workload identity pool provider ID is stored.
| No | OIDC_WIP_PROVIDER_ID | env_var_name |
workspace-root | Workspace root path that is either an absolute path or a path relative to the working directory. Defaults to '.' (the working directory)
| No | . | string |
Build a Docker image
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
attach-workspace | Boolean for whether or not to attach to an existing workspace. Default is false.
| No | false | boolean |
docker-context | Path to the directory containing your build context, defaults to . (working directory)
| No | . | string |
dockerfile | Name of dockerfile to use, defaults to Dockerfile | No | Dockerfile | string |
extra_build_args | Extra flags to pass to docker build. For examples, see https://docs.docker.com/engine/reference/commandline/build
| No | '' | string |
google-project-id | The Google project ID to connect with via the gcloud CLI | No | GOOGLE_PROJECT_ID | env_var_name |
image | A name for your Docker image | Yes | - | string |
no_output_timeout | Pass through a default timeout if your Docker build does not output anything for more than 10 minutes.
| No | 10m | string |
path | Path to the directory containing your Dockerfile, defaults to . (working directory)
| No | . | string |
registry-url | The GCR registry URL from ['', us, eu, asia].gcr.io | No | gcr.io | string |
repository | The Artifact Registry requires a HOST-NAME/PROJECT-ID/REPOSITORY/IMAGE format. If pushing to the Artifact Registry, the repository to push the image to
| No | '' | string |
tag | A Docker image tag | No | latest | string |
workspace-root | Workspace root path that is either an absolute path or a path relative to the working directory. Defaults to '.' (the working directory)
| No | . | string |
Configure Docker to use gcloud as a credential helper.
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
components | The list of gcloud components to install. Space separated. See https://cloud.google.com/sdk/docs/components for additional
| No | '' | string |
gcloud-service-key | The gcloud service key | No | GCLOUD_SERVICE_KEY | env_var_name |
gcp_cred_config_file_path | Output location of OIDC credentials. | No | ~/gcp_cred_config.json | string |
google-compute-region | The Google compute region to connect with via the gcloud CLI
| No | GOOGLE_COMPUTE_REGION | env_var_name |
google-compute-zone | The Google compute zone to connect with via the gcloud CLI
| No | GOOGLE_COMPUTE_ZONE | env_var_name |
google-project-id | Environment variable name for the Google project ID to connect with via the gcloud CLI
| No | GOOGLE_PROJECT_ID | env_var_name |
google_project_number | Name of environment variable storing the Google project number
used to configure OIDC.
| No | GOOGLE_PROJECT_NUMBER | env_var_name |
registry-url | The GCR registry URL from ['', us, eu, asia].gcr.io, or an artifact registry url from [GOOGLE_COMPUTE_REGION, us, eu, asia]-docker.pkg.dev
| No | gcr.io | string |
service_account_email | Environment variable containing OIDC service account email. | No | OIDC_SERVICE_ACCOUNT_EMAIL | env_var_name |
use_oidc | Set to true to enable OIDC | No | false | boolean |
version | The version of the gcloud CLI to install. If left to "latest", the latest version will be installed. Otherwise, provide the full version number as it appears in the URL on this page: https://cloud.google.com/sdk/docs/downloads-versioned-archives
| No | latest | string |
workload_identity_pool_id | Environment variable containing OIDC configured workload identity pool is stored.
| No | OIDC_WIP_ID | env_var_name |
workload_identity_pool_provider_id | Environment variable containing OIDC configured workload identity pool provider ID is stored.
| No | OIDC_WIP_PROVIDER_ID | env_var_name |
Push a container image to the GCR registry
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
digest-path | (Optional) The path to save the RepoDigest of the pushed image | No | '' | string |
google-project-id | The Google project ID to connect with via the gcloud CLI | No | GOOGLE_PROJECT_ID | env_var_name |
image | A name for your docker image | Yes | - | string |
registry-url | The GCR registry URL from ['', us, eu, asia].gcr.io | No | gcr.io | string |
repository | The Artifact Registry requires a HOST-NAME/PROJECT-ID/REPOSITORY/IMAGE format. If pushing to the Artifact Registry, the repository to push the image to
| No | '' | string |
tag | Comma-separated list of docker image tags. The contents of each will be evaluated. | No | latest | string |
Add a tag to an existing published image
PARAMETER | DESCRIPTION | REQUIRED | DEFAULT | TYPE |
---|---|---|---|---|
google-project-id | The Google project ID to connect with via the gcloud CLI | No | GOOGLE_PROJECT_ID | env_var_name |
image | A Docker image name | Yes | - | string |
registry-url | The GCR registry URL from ['', us, eu, asia].gcr.io | No | gcr.io | string |
repository | The Artifact Registry requires a HOST-NAME/PROJECT-ID/REPOSITORY/IMAGE format. If pushing to the Artifact Registry, the repository to push the image to
| No | '' | string |
source-tag | An existing Docker image tag. The contents will be evaluated. | Yes | - | string |
target-tag | A new Docker image tag. The contents will be evaluated. | Yes | - | string |
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
# This code is licensed from CircleCI to the user under the MIT license.
# See here for details: https://circleci.com/developer/orbs/licensing
version: 2.1
description: |
An orb for managing images with Google Container Registry (GCR).
display:
home_url: https://cloud.google.com/container-registry
source_url: https://github.com/CircleCI-Public/gcp-gcr-orb
orbs:
docker: circleci/docker@2.7.1
gcp-cli: circleci/gcp-cli@3.3
commands:
build-image:
description: Build a Docker image
parameters:
attach-workspace:
default: false
description: |
Boolean for whether or not to attach to an existing workspace. Default is false.
type: boolean
docker-context:
default: .
description: |
Path to the directory containing your build context, defaults to . (working directory)
type: string
dockerfile:
default: Dockerfile
description: Name of dockerfile to use, defaults to Dockerfile
type: string
extra_build_args:
default: ""
description: |
Extra flags to pass to docker build. For examples, see https://docs.docker.com/engine/reference/commandline/build
type: string
google-project-id:
default: GOOGLE_PROJECT_ID
description: The Google project ID to connect with via the gcloud CLI
type: env_var_name
image:
description: A name for your Docker image
type: string
no_output_timeout:
default: 10m
description: |
Pass through a default timeout if your Docker build does not output anything for more than 10 minutes.
type: string
path:
default: .
description: |
Path to the directory containing your Dockerfile, defaults to . (working directory)
type: string
registry-url:
default: gcr.io
description: The GCR registry URL from ['', us, eu, asia].gcr.io
type: string
repository:
default: ""
description: |
The Artifact Registry requires a HOST-NAME/PROJECT-ID/REPOSITORY/IMAGE format. If pushing to the Artifact Registry, the repository to push the image to
type: string
tag:
default: latest
description: A Docker image tag
type: string
workspace-root:
default: .
description: |
Workspace root path that is either an absolute path or a path relative to the working directory. Defaults to '.' (the working directory)
type: string
steps:
- when:
condition: <<parameters.attach-workspace>>
steps:
- attach_workspace:
at: <<parameters.workspace-root>>
- when:
condition:
equal:
- ""
- << parameters.repository >>
steps:
- docker/build:
docker-context: <<parameters.docker-context>>
dockerfile: <<parameters.dockerfile>>
extra_build_args: <<parameters.extra_build_args>>
image: <<parameters.image>>
no_output_timeout: <<parameters.no_output_timeout>>
path: <<parameters.path>>
registry: <<parameters.registry-url>>/$<<parameters.google-project-id>>
step-name: Build Docker image for GCR
tag: <<parameters.tag>>
- when:
condition:
not:
equal:
- ""
- << parameters.repository >>
steps:
- docker/build:
docker-context: <<parameters.docker-context>>
dockerfile: <<parameters.dockerfile>>
extra_build_args: <<parameters.extra_build_args>>
image: <<parameters.image>>
no_output_timeout: <<parameters.no_output_timeout>>
path: <<parameters.path>>
registry: <<parameters.registry-url>>/$<<parameters.google-project-id>>/<<parameters.repository>>
step-name: Build Docker image for GCR
tag: <<parameters.tag>>
gcr-auth:
description: |
Configure Docker to use gcloud as a credential helper.
parameters:
components:
default: ""
description: |
The list of gcloud components to install. Space separated. See https://cloud.google.com/sdk/docs/components for additional
type: string
gcloud-service-key:
default: GCLOUD_SERVICE_KEY
description: The gcloud service key
type: env_var_name
gcp_cred_config_file_path:
default: ~/gcp_cred_config.json
description: Output location of OIDC credentials.
type: string
google-compute-region:
default: GOOGLE_COMPUTE_REGION
description: |
The Google compute region to connect with via the gcloud CLI
type: env_var_name
google-compute-zone:
default: GOOGLE_COMPUTE_ZONE
description: |
The Google compute zone to connect with via the gcloud CLI
type: env_var_name
google-project-id:
default: GOOGLE_PROJECT_ID
description: |
Environment variable name for the Google project ID to connect with via the gcloud CLI
type: env_var_name
google_project_number:
default: GOOGLE_PROJECT_NUMBER
description: |
Name of environment variable storing the Google project number
used to configure OIDC.
type: env_var_name
registry-url:
default: gcr.io
description: |
The GCR registry URL from ['', us, eu, asia].gcr.io, or an artifact registry url from [GOOGLE_COMPUTE_REGION, us, eu, asia]-docker.pkg.dev
type: string
service_account_email:
default: OIDC_SERVICE_ACCOUNT_EMAIL
description: Environment variable containing OIDC service account email.
type: env_var_name
use_oidc:
default: false
description: Set to true to enable OIDC
type: boolean
version:
default: latest
description: |
The version of the gcloud CLI to install. If left to "latest", the latest version will be installed. Otherwise, provide the full version number as it appears in the URL on this page: https://cloud.google.com/sdk/docs/downloads-versioned-archives
type: string
workload_identity_pool_id:
default: OIDC_WIP_ID
description: |
Environment variable containing OIDC configured workload identity pool is stored.
type: env_var_name
workload_identity_pool_provider_id:
default: OIDC_WIP_PROVIDER_ID
description: |
Environment variable containing OIDC configured workload identity pool provider ID is stored.
type: env_var_name
steps:
- gcp-cli/setup:
components: << parameters.components >>
gcloud_service_key: <<parameters.gcloud-service-key>>
gcp_cred_config_file_path: << parameters.gcp_cred_config_file_path >>
google_compute_region: <<parameters.google-compute-region>>
google_compute_zone: <<parameters.google-compute-zone>>
google_project_id: <<parameters.google-project-id>>
google_project_number: << parameters.google_project_number >>
service_account_email: << parameters.service_account_email >>
use_oidc: << parameters.use_oidc >>
version: << parameters.version >>
workload_identity_pool_id: << parameters.workload_identity_pool_id >>
workload_identity_pool_provider_id: << parameters.workload_identity_pool_provider_id >>
- run:
command: |
#!/usr/bin/env bash
ORB_VAL_REGISTRY_URL="$(circleci env subst "$ORB_VAL_REGISTRY_URL")"
unameOut="$(uname -s)"
case "${unameOut}" in
Linux*) platform=linux;;
Darwin*) platform=mac;;
CYGWIN*) platform=windows;;
MINGW*) platform=windows;;
MSYS_NT*) platform=windows;;
*) platform="UNKNOWN:${unameOut}"
esac
# Set sudo to work whether logged in as root user or non-root user
if [[ $EUID == 0 ]] || [[ "${platform}" == "windows" ]]; then
export SUDO=""
else
export SUDO="sudo"
fi
# configure Docker to use gcloud as a credential helper
mkdir -p "$HOME/.docker"
if [[ "$ORB_VAL_REGISTRY_URL" == *"docker.pkg.dev" ]]; then
gcloud auth configure-docker --quiet --project "${!ORB_ENV_PROJECT_ID}" "$ORB_VAL_REGISTRY_URL"
else
gcloud auth configure-docker --quiet --project "${!ORB_ENV_PROJECT_ID}"
fi
# if applicable, provide user access to the docker config file
if [[ -d "$HOME/.docker" ]]; then
$SUDO chown "$USER:$USER" "$HOME/.docker" -R
fi
if [[ -d "$HOME/.config" ]]; then
$SUDO chown "$USER:$USER" "$HOME/.config" -R
fi
environment:
ORB_ENV_PROJECT_ID: << parameters.google-project-id >>
ORB_VAL_REGISTRY_URL: << parameters.registry-url >>
name: gcloud auth configure-docker
push-image:
description: Push a container image to the GCR registry
parameters:
digest-path:
default: ""
description: (Optional) The path to save the RepoDigest of the pushed image
type: string
google-project-id:
default: GOOGLE_PROJECT_ID
description: The Google project ID to connect with via the gcloud CLI
type: env_var_name
image:
description: A name for your docker image
type: string
registry-url:
default: gcr.io
description: The GCR registry URL from ['', us, eu, asia].gcr.io
type: string
repository:
default: ""
description: |
The Artifact Registry requires a HOST-NAME/PROJECT-ID/REPOSITORY/IMAGE format. If pushing to the Artifact Registry, the repository to push the image to
type: string
tag:
default: latest
description: Comma-separated list of docker image tags. The contents of each will be evaluated.
type: string
steps:
- run:
command: "#!/bin/bash \n\nORB_VAL_REGISTRY_URL=\"$(circleci env subst \"$ORB_VAL_REGISTRY_URL\")\"\nORB_VAL_REPOSITORY=\"$(circleci env subst \"$ORB_VAL_REPOSITORY\")\"\nORB_VAL_IMAGE=\"$(circleci env subst \"$ORB_VAL_IMAGE\")\"\nORB_VAL_DIGEST_PATH=\"$(circleci env subst \"$ORB_VAL_DIGEST_PATH\")\"\n\nIFS=\",\" read -ra DOCKER_TAGS \\<<< \"$ORB_EVAL_TAG\"\nPROJECT_ID=\"${!ORB_ENV_PROJECT_ID}\"\n\nDOCKER_PATH=\"$ORB_VAL_REGISTRY_URL/$PROJECT_ID/$ORB_VAL_IMAGE\"\nif [ -n \"${ORB_VAL_REPOSITORY}\" ]; then\n DOCKER_PATH=\"$ORB_VAL_REGISTRY_URL/$PROJECT_ID/$ORB_VAL_REPOSITORY/$ORB_VAL_IMAGE\"\nfi\n\nfor tag_to_eval in \"${DOCKER_TAGS[@]}\"; do\n TAG=$(eval echo \"$tag_to_eval\")\n set -x\n docker push \"${DOCKER_PATH}:${TAG}\"\n set +x\ndone\n\nif [ -n \"$ORB_VAL_DIGEST_PATH\" ]; then\n mkdir -p \"$(dirname \"$ORB_VAL_DIGEST_PATH\")\"\n SAMPLE_FIRST=$(eval echo \"${DOCKER_TAGS[0]}\")\n set -x\n docker image inspect --format=\"{{index .RepoDigests 0}}\" \"$DOCKER_PATH:$SAMPLE_FIRST\" > \"$ORB_VAL_DIGEST_PATH\"\n set +x\nfi\n"
environment:
ORB_ENV_PROJECT_ID: << parameters.google-project-id >>
ORB_EVAL_TAG: << parameters.tag >>
ORB_VAL_DIGEST_PATH: << parameters.digest-path >>
ORB_VAL_IMAGE: << parameters.image >>
ORB_VAL_REGISTRY_URL: << parameters.registry-url >>
ORB_VAL_REPOSITORY: << parameters.repository >>
name: Push image to GCR
tag-image:
description: Add a tag to an existing published image
parameters:
google-project-id:
default: GOOGLE_PROJECT_ID
description: The Google project ID to connect with via the gcloud CLI
type: env_var_name
image:
description: A Docker image name
type: string
registry-url:
default: gcr.io
description: The GCR registry URL from ['', us, eu, asia].gcr.io
type: string
repository:
default: ""
description: |
The Artifact Registry requires a HOST-NAME/PROJECT-ID/REPOSITORY/IMAGE format. If pushing to the Artifact Registry, the repository to push the image to
type: string
source-tag:
description: An existing Docker image tag. The contents will be evaluated.
type: string
target-tag:
description: A new Docker image tag. The contents will be evaluated.
type: string
steps:
- run:
command: |
#!/bin/bash
ORB_VAL_REGISTRY_URL="$(circleci env subst "$ORB_VAL_REGISTRY_URL")"
ORB_VAL_REPOSITORY="$(circleci env subst "$ORB_VAL_REPOSITORY")"
ORB_VAL_IMAGE="$(circleci env subst "$ORB_VAL_IMAGE")"
SOURCE_TAG=$(eval echo "$ORB_EVAL_SOURCE_TAG")
TARGET_TAG=$(eval echo "$ORB_EVAL_TARGET_TAG")
IMAGE_ROOT="$ORB_VAL_REGISTRY_URL/${!ORB_ENV_PROJECT_ID}/$ORB_VAL_IMAGE"
if [ -n "${ORB_VAL_REPOSITORY}" ]; then
IMAGE_ROOT="$ORB_VAL_REGISTRY_URL/${!ORB_ENV_PROJECT_ID}/$ORB_VAL_REPOSITORY/$ORB_VAL_IMAGE"
fi
gcloud container images add-tag --quiet \
"$IMAGE_ROOT:$SOURCE_TAG" \
"$IMAGE_ROOT:$TARGET_TAG"
environment:
ORB_ENV_PROJECT_ID: << parameters.google-project-id >>
ORB_EVAL_SOURCE_TAG: << parameters.source-tag >>
ORB_EVAL_TARGET_TAG: << parameters.target-tag >>
ORB_VAL_IMAGE: << parameters.image >>
ORB_VAL_REGISTRY_URL: << parameters.registry-url >>
ORB_VAL_REPOSITORY: << parameters.repository >>
name: Add <<parameters.target-tag>> tag to <<parameters.registry-url>>/$<<parameters.google-project-id>>/<<parameters.image>>:<<parameters.source-tag>>
executors:
default:
description: A debian-based machine executor
machine:
image: default
jobs:
add-image-tag:
description: |
Install GCP CLI, if needed, and configure. Adds a tag to an existing image.
executor: <<parameters.executor>>
parameters:
executor:
default: default
description: executor to use for this job
type: executor
gcloud-service-key:
default: GCLOUD_SERVICE_KEY
description: The gcloud service key
type: env_var_name
gcloud_components:
default: ""
description: |
The list of gcloud components to install. Space separated. See https://cloud.google.com/sdk/docs/components for additional info.
type: string
gcloud_version:
default: latest
description: |
Version of gcloud CLI to install.
type: string
gcp_cred_config_file_path:
default: ~/gcp_cred_config.json
description: Output location of OIDC credentials.
type: string
google-compute-region:
default: GOOGLE_COMPUTE_REGION
description: The Google compute zone to connect with via the gcloud CLI
type: env_var_name
google-compute-zone:
default: GOOGLE_COMPUTE_ZONE
description: The Google compute zone to connect with via the gcloud CLI
type: env_var_name
google-project-id:
default: GOOGLE_PROJECT_ID
description: The Google project ID to connect with via the gcloud CLI
type: env_var_name
google_project_number:
default: GOOGLE_PROJECT_NUMBER
description: |
Name of environment variable storing the Google project number
used to configure OIDC.
type: env_var_name
image:
description: A name for your Docker image
type: string
registry-url:
default: gcr.io
description: The GCR registry URL from ['', us, eu, asia].gcr.io
type: string
service_account_email:
default: OIDC_SERVICE_ACCOUNT_EMAIL
description: Environment variable containing OIDC service account email.
type: env_var_name
source-tag:
description: An existing Docker image tag
type: string
target-tag:
description: A new Docker image tag
type: string
use_oidc:
default: false
description: Set to true to enable OIDC
type: boolean
workload_identity_pool_id:
default: OIDC_WIP_ID
description: |
Environment variable containing OIDC configured workload identity pool is stored.
type: env_var_name
workload_identity_pool_provider_id:
default: OIDC_WIP_PROVIDER_ID
description: |
Environment variable containing OIDC configured workload identity pool provider ID is stored.
type: env_var_name
steps:
- gcr-auth:
components: <<parameters.gcloud_components>>
gcloud-service-key: <<parameters.gcloud-service-key>>
gcp_cred_config_file_path: << parameters.gcp_cred_config_file_path >>
google-compute-region: <<parameters.google-compute-region>>
google-compute-zone: <<parameters.google-compute-zone>>
google-project-id: <<parameters.google-project-id>>
google_project_number: << parameters.google_project_number >>
service_account_email: << parameters.service_account_email >>
use_oidc: << parameters.use_oidc >>
version: <<parameters.gcloud_version>>
workload_identity_pool_id: << parameters.workload_identity_pool_id >>
workload_identity_pool_provider_id: << parameters.workload_identity_pool_provider_id >>
- tag-image:
google-project-id: <<parameters.google-project-id>>
image: <<parameters.image>>
registry-url: <<parameters.registry-url>>
source-tag: <<parameters.source-tag>>
target-tag: <<parameters.target-tag>>
build-and-push-image:
description: |
Install GCP CLI, if needed, and configure. Build and push image to repository.
executor: <<parameters.executor>>
parameters:
attach-workspace:
default: false
description: |
Boolean for whether or not to attach to an existing workspace. Default is false.
type: boolean
digest-path:
default: ""
description: (Optional) The path to save the RepoDigest of the pushed image
type: string
docker-context:
default: .
description: |
Path to the directory containing your build context, defaults to . (working directory)
type: string
dockerfile:
default: Dockerfile
description: Name of dockerfile to use, defaults to Dockerfile
type: string
executor:
default: default
description: executor to use for this job
type: executor
extra_build_args:
default: ""
description: |
Extra flags to pass to docker build. For examples, see https://docs.docker.com/engine/reference/commandline/build
type: string
gcloud-service-key:
default: GCLOUD_SERVICE_KEY
description: The gcloud service key
type: env_var_name
gcloud_components:
default: ""
description: |
The list of gcloud components to install. Space separated. See https://cloud.google.com/sdk/docs/components for additional info.
type: string
gcloud_version:
default: latest
description: |
Version of gcloud CLI to install.
type: string
gcp_cred_config_file_path:
default: ~/gcp_cred_config.json
description: Output location of OIDC credentials.
type: string
google-compute-region:
default: GOOGLE_COMPUTE_REGION
description: The Google compute zone to connect with via the gcloud CLI
type: env_var_name
google-compute-zone:
default: GOOGLE_COMPUTE_ZONE
description: The Google compute zone to connect with via the gcloud CLI
type: env_var_name
google-project-id:
default: GOOGLE_PROJECT_ID
description: The Google project ID to connect with via the gcloud CLI
type: env_var_name
google_project_number:
default: GOOGLE_PROJECT_NUMBER
description: |
Name of environment variable storing the Google project number
used to configure OIDC.
type: env_var_name
image:
description: A name for your Docker image
type: string
no_output_timeout:
default: 10m
description: |
Pass through a default timeout if your Docker build does not output anything for more than 10 minutes.
type: string
path:
default: .
description: |
Path to the directory containing your Dockerfile, defaults to . (working directory)
type: string
registry-url:
default: gcr.io
description: |
The GCR registry URL from ['', us, eu, asia].gcr.io, or an artifact registry url from [GOOGLE_COMPUTE_REGION, us, eu, asia]-docker.pkg.dev
type: string
remote-docker-version:
default: docker24
description: |
Specify the remote docker version. See: https://circleci.com/docs/2.0/building-docker-images/#docker-version
type: string
repository:
default: ""
description: |
The Artifact Registry requires a HOST-NAME/PROJECT-ID/REPOSITORY/IMAGE format. If pushing to the Artifact Registry, the repository to push the image to
type: string
service_account_email:
default: OIDC_SERVICE_ACCOUNT_EMAIL
description: Environment variable containing OIDC service account email.
type: env_var_name
setup-remote-docker:
default: false
description: |
Setup and use CircleCI's remote Docker environment for Docker and docker-compose commands? Not required if using the default executor
type: boolean
tag:
default: latest
description: A Docker image tag
type: string
use-docker-layer-caching:
default: false
description: |
Setup docker layer caching for optimized build. Not available if using the default executor.
type: boolean
use_oidc:
default: false
description: Set to true to enable OIDC
type: boolean
workload_identity_pool_id:
default: OIDC_WIP_ID
description: |
Environment variable containing OIDC configured workload identity pool is stored.
type: env_var_name
workload_identity_pool_provider_id:
default: OIDC_WIP_PROVIDER_ID
description: |
Environment variable containing OIDC configured workload identity pool provider ID is stored.
type: env_var_name
workspace-root:
default: .
description: |
Workspace root path that is either an absolute path or a path relative to the working directory. Defaults to '.' (the working directory)
type: string
steps:
- checkout
- when:
condition: <<parameters.setup-remote-docker>>
steps:
- setup_remote_docker:
docker_layer_caching: <<parameters.use-docker-layer-caching>>
version: <<parameters.remote-docker-version>>
- gcr-auth:
components: <<parameters.gcloud_components>>
gcloud-service-key: <<parameters.gcloud-service-key>>
gcp_cred_config_file_path: << parameters.gcp_cred_config_file_path >>
google-compute-region: <<parameters.google-compute-region>>
google-compute-zone: <<parameters.google-compute-zone>>
google-project-id: <<parameters.google-project-id>>
google_project_number: << parameters.google_project_number >>
registry-url: <<parameters.registry-url>>
service_account_email: << parameters.service_account_email >>
use_oidc: << parameters.use_oidc >>
version: <<parameters.gcloud_version>>
workload_identity_pool_id: << parameters.workload_identity_pool_id >>
workload_identity_pool_provider_id: << parameters.workload_identity_pool_provider_id >>
- build-image:
attach-workspace: <<parameters.attach-workspace>>
docker-context: <<parameters.docker-context>>
dockerfile: <<parameters.dockerfile>>
extra_build_args: <<parameters.extra_build_args>>
google-project-id: <<parameters.google-project-id>>
image: <<parameters.image>>
no_output_timeout: <<parameters.no_output_timeout>>
path: <<parameters.path>>
registry-url: <<parameters.registry-url>>
repository: <<parameters.repository>>
tag: <<parameters.tag>>
workspace-root: <<parameters.workspace-root>>
- push-image:
digest-path: <<parameters.digest-path>>
google-project-id: <<parameters.google-project-id>>
image: <<parameters.image>>
registry-url: <<parameters.registry-url>>
repository: <<parameters.repository>>
tag: <<parameters.tag>>
examples:
build-and-push-digest:
description: |
Log into Google Cloud Plaform, build and push image to GCR, then echo the image digest
usage:
version: "2.1"
orbs:
gcp-gcr: circleci/gcp-gcr@x.y.z
jobs:
build-and-push:
executor: gcp-gcr/default
steps:
- checkout
- gcp-gcr/gcr-auth
- gcp-gcr/build-image:
image: orb-test
no_output_timeout: 20m
registry-url: eu.gcr.io
- gcp-gcr/push-image:
digest-path: /tmp/digest.txt
image: orb-test
registry-url: eu.gcr.io
- run:
command: |
echo "Digest is: $(</tmp/digest.txt)"
workflows:
commit:
jobs:
- build-and-push
optimized-build-and-push:
description: |
Log into Google Cloud Plaform, then build and push image to GCR. Uses a custom executor to reduce the setup time.
usage:
version: "2.1"
orbs:
gcp-gcr: circleci/gcp-gcr@x.y.z
workflows:
build_and_push_image:
jobs:
- gcp-gcr/build-and-push-image:
context: myContext
executor: my-executor
image: my-image
registry-url: us.gcr.io
setup-remote-docker: true
use-docker-layer-caching: true
simple-build-and-push:
description: |
Log into Google Cloud Plaform, then build and push image to GCR
usage:
version: "2.1"
orbs:
gcp-gcr: circleci/gcp-gcr@x.y.z
workflows:
build_and_push_image:
jobs:
- gcp-gcr/build-and-push-image:
context: myContext
image: my-image
registry-url: us.gcr.io
simple-build-and-push-artifact-registry:
description: |
Log into Google Cloud Plaform, then build and push image to Artifact Registry
usage:
version: "2.1"
orbs:
gcp-gcr: circleci/gcp-gcr@x.y.z
workflows:
build_and_push_image:
jobs:
- gcp-gcr/build-and-push-image:
context: myContext
image: my-image
registry-url: us-central1-docker.pkg.dev
repository: my-repo
tag-existing-image:
description: |
Log into Google Cloud Plaform, then tag an existing image with "latest"
usage:
version: "2.1"
orbs:
gcp-gcr: circleci/gcp-gcr@x.y.z
workflows:
build_and_push_image:
jobs:
- gcp-gcr/tag-image:
context: myContext
image: my-image
registry-url: us.gcr.io
source-tag: mytag1
target-tag: mytag2