1. circleci/aws-ecr@9.3.7

circleci/aws-ecr@9.3.7

Certified
Sections
Build images and push them to the Amazon Elastic Container Registry.
Created: October 26, 2018Version Published: November 21, 2024Releases: 82
Org Usage:
2497

Orb Quick Start Guide

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: aws-ecr: circleci/aws-ecr@9.3.7

Use aws-ecr elements in your existing workflows and jobs.

Usage Examples

build_and_push_image_w_registry_login

In some cases, images being built using the build_and_push_image job need to pull a custom Docker image from a container registry like DockerHub, Heroku or GitHub Container Registry. This requires users to log into these registries first before the images can be built. This is an example of a using the build_and_push_image job with a custom registry login step. NOTE: The container_registry_login parameter must be set to true. Registry usernames and passwords are stored as environment variables in CircleCI with this example.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 version: '2.1' orbs: aws-cli: circleci/aws-cli@5.1 aws-ecr: circleci/aws-ecr@9.0 workflows: build-and-push-image-with-container-registry-login: jobs: - aws-ecr/build_and_push_image: auth: - aws-cli/setup: role_arn: arn:aws:iam::123456789012 container_registry_login: true dockerfile: Dockerfile path: . region: us-west-2 registry_login: - run: docker login -u ${HEROKU_USERNAME} -p ${HEROKU_API_KEY} - run: docker login -u ${GITHUB_USERNAME} -p ${GITHUB_TOKEN} - run: docker login -u ${DOCKERHUB_ID} -p ${DOCKERHUB_PASSWORD} repo: my-sample-repo tag: sampleTag

build_test_then_push_image

This is an example of a job that builds a docker image with docker buildx. It tests the image and before pushing it to the specified ECR repository. NOTE: The push_image parameter must be set to false. Only one platform can be specified and loaded into the local Docker Daemon. Loading multi-architecture images is not supported at this time.

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 version: '2.1' orbs: aws-cli: circleci/aws-cli@5.1 aws-ecr: circleci/aws-ecr@9.0 jobs: build-test-then-push-with-buildx: machine: image: ubuntu-2204:current steps: - aws-ecr/build_and_push_image: attach_workspace: true auth: - aws-cli/setup: role_arn: arn:aws:iam::123456789012 create_repo: true dockerfile: Dockerfile path: workspace platform: linux/amd64 push_image: false region: us-west-2 repo: my-sample-repo tag: sampleTag workspace_root: . - run: command: > set -x docker run 123456789012.dkr.ecr.us-west-2.amazonaws.com/<< parameters.repo >>:<< parameters.tag >> ping -V status=$(echo "$?") if [ "${status}" != "0" ]; then exit 1; else exit 0; fi set +x name: Tests for docker image - aws-ecr/push_image: region: << parameters.region >> repo: << parameters.repo >> tag: << parameters.tag >> workflows: build-image-test-image-push-image-with-buildx: jobs: - build-test-then-push-with-buildx: context: CircleCI_OIDC_Token

simple_build_and_push

Log into AWS, build and push image to Amazon ECR using OIDC for authentication. Import the aws-cli orb and authenticate using the aws-cli/setup command with a valid role-arn for OIDC authentication.

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 version: '2.1' orbs: aws-cli: circleci/aws-cli@5.1 aws-ecr: circleci/aws-ecr@9.0 workflows: build_and_push_image: jobs: - aws-ecr/build_and_push_image: account_id: ${AWS_ACCOUNT_ID} auth: - aws-cli/setup: profile_name: OIDC-USER role_arn: arn:aws:iam::123456789012:role/VALID_OIDC_ECR_ROLE context: CircleCI_OIDC_Token create_repo: true dockerfile: myDockerfile executors: base extra_build_args: '--compress' no_output_timeout: 20m path: pathToMyDockerfile platform: linux/amd64 profile_name: OIDC-User public_registry: false push_image: true region: ${AWS_DEFAULT_REGION} repo: myECRRepository repo_encryption_kms_key: arn:aws:kms::123456789012:key/UUID4_OF_KMS_KEY_ID repo_encryption_type: KMS repo_policy_path: repo-policy.json repo_scan_on_push: true set_repo_policy: true skip_when_tags_exist: false tag: latest,myECRRepoTag

Jobs

build_and_push_image

Log into Amazon ECR and push image to repository. Authentication with OIDC or static AWS keys using the aws-cli/setup command is required. NOTE: Some commands may not work with AWS CLI Version 1.

Show job Source
PARAMETER
DESCRIPTION
REQUIRED
DEFAULT
TYPE
account_id
The 12 digit AWS id associated with the ECR account. This field is required
No
${AWS_ACCOUNT_ID}
string
attach_workspace
Boolean for whether or not to attach to an existing workspace. Default is false.
No
false
boolean
auth
The authentication method used to access your AWS account. Import the aws-cli orb in your config and provide the aws-cli/setup command to authenticate with your preferred method. View examples for more information.
Yes
-
steps
aws_domain
The AWS domain for your region, e.g in China, the AWS domain is amazonaws.com.cn The default value is amazonaws.com
No
amazonaws.com
string
build_path
Path to the directory containing your build context. Defaults to . (working directory).
No
.
string
checkout
Boolean for whether or not to checkout as a first step. Default is true.
No
true
boolean
container_registry_login
Enable login to different image container registries such as DockerHub, Heroku or Github. Defaults to false.
No
false
boolean
create_repo
Should the repo be created if it does not exist?
No
false
boolean
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. This parameter accepts multi-line arguments. If your argument spans multiple lines, please use the Folded Block Style denoted by `>-` (e.g. extra_build_args: >-). For examples of available flags, see https://docs.docker.com/engine/reference/commandline/buildx_build
No
''
string
lifecycle_policy_path
The path to the .json file containing the lifecycle policy to be applied to a specified repository in AWS ECR.
No
''
string
no_output_timeout
The amount of time to allow the docker build command to run before timing out. Defaults to '10m'
No
10m
string
path
Path to the directory containing your Dockerfile. Defaults to . (working directory).
No
.
string
platform
Platform targets for the docker image, multi arch images. Ex. linux/amd64,linux/arm64
No
linux/amd64
string
profile_name
AWS profile name to be configured.
No
default
string
public_registry
Set to true if building and pushing an image to a Public Registry on ECR.
No
false
boolean
public_registry_alias
The public registry alias for your public repositories. This parameter is required if pushing to a public repository It can be found in the Amazon ECR console > Public Registries.
No
${AWS_ECR_PUBLIC_REGISTRY_ALIAS}
string
push_image
Set to false to build an image without pushing to repository. Defaults to true.
No
true
boolean
region
AWS region of ECR repository. Defaults to environment variable ${AWS_DEFAULT_REGION}
No
${AWS_DEFAULT_REGION}
string
registry_login
Custom container registry login step e.g docker -u $DOCKER_ID -p $DOCKER_PASSWORD
No
[sequence of length 2] see source
steps
remote_docker_layer_caching
Enable Docker layer caching if using remote Docker engine. Requires setup_remote_docker to be set to true. Defaults to false.
No
false
boolean
remote_docker_version
Specific remote docker version
No
''
string
repo
Name of an Amazon ECR repository
Yes
-
string
repo_encryption_kms_key
If you use the KMS encryption type, specify the KMS key to use for encryption. The alias, key ID, or full ARN of the KMS key can be specified.
No
''
string
repo_encryption_type
The encryption type to use.
No
AES256
enum
repo_policy_path
The path to the .json file containing the repository policy to be applied to a specified repository in AWS ECR.
No
''
string
repo_scan_on_push
Should the created repo be security scanned on push?
No
true
boolean
set_repo_policy
Should a repository policy be set?
No
false
boolean
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
skip_when_tags_exist
Whether to skip image building if all specified tags already exist in ECR
No
false
boolean
tag
A comma-separated string containing docker image tags to build and push (default = latest)
No
latest
string
use_credentials_helper
When true the authentication to docker registry will be done using the ecr-credential-helper. This avoids having the password saved in plain text. Otherwise it will use the classic docker login command which is more compatible.
No
true
boolean
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

Commands

build_and_push_image

Log into Amazon ECR, build and push a Docker image to the specified repository. NOTE: Some commands may not work with AWS CLI Version 1.

Show command Source
PARAMETER
DESCRIPTION
REQUIRED
DEFAULT
TYPE
account_id
The 12 digit AWS id associated with the ECR account. This field is required
No
${AWS_ACCOUNT_ID}
string
attach_workspace
Boolean for whether or not to attach to an existing workspace. Default is false.
No
false
boolean
auth
The authentication method used to access your AWS account. Import the aws-cli orb in your config and provide the aws-cli/setup command to authenticate with your preferred method. View examples for more information.
Yes
-
steps
aws_domain
The AWS domain for your region, e.g in China, the AWS domain is amazonaws.com.cn The default value is amazonaws.com
No
amazonaws.com
string
build_path
Path to the directory containing your build context. Defaults to . (working directory).
No
.
string
checkout
Boolean for whether or not to checkout as a first step. Default is true.
No
true
boolean
container_registry_login
Enable login to different image container registries such as DockerHub, Heroku or Github. Defaults to false.
No
false
boolean
create_repo
Should the repo be created if it does not exist?
No
false
boolean
dockerfile
Name of dockerfile to use. Defaults to Dockerfile.
No
Dockerfile
string
extra_build_args
Extra flags to pass to docker build. This parameter accepts multi-line arguments. If your argument spans multiple lines, please use the Folded Block Style denoted by `>-` (e.g. extra_build_args: >-). For examples of available flags, see https://docs.docker.com/engine/reference/commandline/buildx_build
No
''
string
lifecycle_policy_path
The path to the .json file containing the lifecycle policy to be applied to a specified repository in AWS ECR.
No
''
string
no_output_timeout
The amount of time to allow the docker build command to run before timing out (default is `10m`)
No
10m
string
path
Path to the directory containing your Dockerfile. Defaults to . (working directory).
No
.
string
platform
Platform targets for the docker image, multi arch images. Ex. linux/amd64,linux/arm64
No
linux/amd64
string
profile_name
AWS profile name to be configured.
No
default
string
public_registry
Set to true if building and pushing an image to a Public Registry on ECR.
No
false
boolean
public_registry_alias
The public registry alias for your public repositories. This parameter is required if pushing to a public repository It can be found in the Amazon ECR console > Public Registries.
No
${AWS_ECR_PUBLIC_REGISTRY_ALIAS}
string
push_image
Set to false to build an image without pushing to repository. Defaults to true.
No
true
boolean
region
Name of env var storing your AWS region information, defaults to AWS_DEFAULT_REGION
No
${AWS_DEFAULT_REGION}
string
registry_login
Custom container registry login step e.g docker -u $DOCKER_ID -p $DOCKER_PASSWORD
No
[sequence of length 2] see source
steps
remote_docker_layer_caching
Enable Docker layer caching if using remote Docker engine. Defaults to false.
No
false
boolean
remote_docker_version
Specific remote docker version
No
''
string
repo
Name of an Amazon ECR repository
Yes
-
string
repo_encryption_kms_key
If you use the KMS encryption type, specify the KMS key to use for encryption. The alias, key ID, or full ARN of the KMS key can be specified.
No
''
string
repo_encryption_type
The encryption type to use.
No
AES256
enum
repo_policy_path
The path to the .json file containing the repository policy to be applied to a specified repository in AWS ECR.
No
''
string
repo_scan_on_push
Should the created repo be security scanned on push?
No
true
boolean
set_repo_policy
Should a repository policy be set?
No
false
boolean
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
skip_when_tags_exist
Whether to skip image building if all specified tags already exist in ECR
No
false
boolean
tag
A comma-separated string containing docker image tags to build and push (default = latest)
No
latest
string
use_credentials_helper
When true the authentication to docker registry will be done using the ecr-credential-helper. This avoids having the password saved in plain text. Otherwise it will use the classic docker login command which is more compatible.
No
true
boolean
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_image

Build a Docker image with docker buildx. NOTE: Some commands may not work with AWS CLI Version 1.

Show command Source
PARAMETER
DESCRIPTION
REQUIRED
DEFAULT
TYPE
account_id
The 12 digit AWS Account ID associated with the ECR account. This field is required
No
${AWS_ACCOUNT_ID}
string
aws_domain
The AWS domain for your region, e.g in China, the AWS domain is amazonaws.com.cn The default value is amazonaws.com
No
amazonaws.com
string
build_path
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. This parameter accepts multi-line arguments. If your argument spans multiple lines, please use the Folded Block Style denoted by `>-` (e.g. extra_build_args: >-). For examples of available flags, see https://docs.docker.com/engine/reference/commandline/buildx_build
No
''
string
lifecycle_policy_path
The path to the .json file containing the lifecycle policy to be applied to a specified repository in AWS ECR. The docker buildx command uses a builder-context to build multi-architecture images simultaneously. In order for buildx builders to be saved with Docker Layer Caching, the builder must explicitly be named. Specify the a builder name with this parameter. This parameter defaults to "default-builder" if none is provided.
No
''
string
no_output_timeout
The amount of time to allow the docker command to run before timing out.
No
10m
string
path
Path to the directory containing your Dockerfile. Defaults to . (working directory).
No
.
string
platform
Platform targets for the docker image, multi arch images. Ex. linux/amd64,linux/arm64.
No
linux/amd64
string
profile_name
AWS profile name to be configured. Only required when skip_when_tags_exist or ecr_login are set to true.
No
default
string
public_registry
Set to true if building and pushing an image to a Public Registry on ECR.
No
false
boolean
public_registry_alias
The public registry alias for your public repositories. This parameter is required if pushing to a public repository It can be found in the Amazon ECR console > Public Registries.
No
${AWS_ECR_PUBLIC_REGISTRY_ALIAS}
string
push_image
Set to false to build an image without pushing to repository. Defaults to true.
No
true
boolean
region
AWS region of ECR repository. Defaults to environment variable ${AWS_DEFAULT_REGION}
No
${AWS_DEFAULT_REGION}
string
repo
Name of an Amazon ECR repository
Yes
-
string
skip_when_tags_exist
Whether to skip image building if all specified tags already exist in ECR
No
false
boolean
tag
A comma-separated string containing docker image tags (default = latest)
No
latest
string

create_repo

Create a new AWS ECR repository. NOTE: Some commands may not work with AWS CLI Version 1.

Show command Source
PARAMETER
DESCRIPTION
REQUIRED
DEFAULT
TYPE
encryption_kms_key
If you use the KMS encryption type, specify the KMS key to use for encryption. The alias, key ID, or full ARN of the KMS key can be specified.
No
''
string
profile_name
AWS profile name to be configured.
No
default
string
public_registry
Set to true if building and pushing an image to a Public Registry on ECR.
No
false
boolean
region
AWS region of ECR repository. Defaults to environment variable ${AWS_DEFAULT_REGION}
No
${AWS_DEFAULT_REGION}
string
repo
Name of an Amazon ECR repository
Yes
-
string
repo_encryption_type
The encryption type to use.
No
AES256
enum
repo_scan_on_push
Should the created repo be security scanned on push?
No
true
boolean

ecr_login

Authenticate into the Amazon ECR service. This command requires jq. NOTE: Some commands may not work with AWS CLI Version 1.

Show command Source
PARAMETER
DESCRIPTION
REQUIRED
DEFAULT
TYPE
account_id
The 12 digit AWS id associated with the ECR account. This field is required
No
${AWS_ACCOUNT_ID}
string
aws_domain
AWS domain, China regions will require override.
No
amazonaws.com
string
profile_name
AWS profile name to be used for login.
No
default
string
public_registry
Set to true if building and pushing an image to a Public Registry on ECR.
No
false
boolean
region
AWS region of ECR repository. Defaults to environment variable ${AWS_DEFAULT_REGION}
No
${AWS_DEFAULT_REGION}
string
use_credentials_helper
When true the authentication to docker registry will be done using the ecr-credential-helper. This avoids having the password saved in plain text. Otherwise it will use the classic docker login command which is more compatible. If you are having issues with credentials helper login, set this to false.
No
true
boolean

push_image

Push a container image to the Amazon ECR registry NOTE: Some commands may not work with AWS CLI Version 1.

Show command Source
PARAMETER
DESCRIPTION
REQUIRED
DEFAULT
TYPE
account_id
The 12 digit AWS Account ID associated with the ECR account. This field is required
No
${AWS_ACCOUNT_ID}
string
aws_domain
AWS domain, China regions will require override.
No
amazonaws.com
string
public_registry
Set to true if building and pushing an image to a Public Registry on ECR.
No
false
boolean
public_registry_alias
The public registry alias for your public repositories. This parameter is required if pushing to a public repository It can be found in the Amazon ECR console > Public Registries.
No
${AWS_ECR_PUBLIC_REGISTRY_ALIAS}
string
region
AWS region of ECR repository. Defaults to environment variable ${AWS_DEFAULT_REGION}
No
${AWS_DEFAULT_REGION}
string
repo
Name of an Amazon ECR repository
Yes
-
string
tag
A comma-separated string containing docker image tags (default = latest)
No
latest
string

set_repo_policy

Set a repository policy on an AWS ECR repository NOTE: Some commands may not work with AWS CLI Version 1.

Show command Source
PARAMETER
DESCRIPTION
REQUIRED
DEFAULT
TYPE
profile_name
AWS profile name to be configured.
No
default
string
public_registry
Set to true if building and pushing an image to a Public Registry on ECR.
No
false
boolean
region
AWS region of ECR repository. Defaults to environment variable ${AWS_DEFAULT_REGION}
No
${AWS_DEFAULT_REGION}
string
repo
Name of an Amazon ECR repository
Yes
-
string
repo_policy_path
The path to the .json file containing the repository policy to be applied to a specified repository in AWS ECR.
No
''
string

tag_image

Add a tag to an existing published image NOTE: Some commands may not work with AWS CLI Version 1.

Show command Source
PARAMETER
DESCRIPTION
REQUIRED
DEFAULT
TYPE
profile_name
AWS profile to use
No
default
string
repo
Name of an Amazon ECR repository
Yes
-
string
skip_when_tags_exist
Whether to skip tagging an image if any specified tags already exist
No
false
boolean
source_tag
An existing Docker image tag
Yes
-
string
target_tag
A comma-separated string containing docker image tags (default = latest)
No
latest
string

Executors

default

CircleCI's Ubuntu-based machine executor VM: https://circleci.com/docs/2.0/executor-types/#using-machine

Show executor Source
PARAMETER
DESCRIPTION
REQUIRED
DEFAULT
TYPE
docker_layer_caching
-
No
false
boolean
image
-
No
ubuntu-2204:current
string
resource_class
-
No
medium
string

Orb Source

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 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 # 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: | Build images and push them to the Amazon Elastic Container Registry. display: home_url: https://aws.amazon.com/ecr/ source_url: https://github.com/CircleCI-Public/aws-ecr-orb commands: build_and_push_image: description: | Log into Amazon ECR, build and push a Docker image to the specified repository. NOTE: Some commands may not work with AWS CLI Version 1. parameters: account_id: default: ${AWS_ACCOUNT_ID} description: | The 12 digit AWS id associated with the ECR account. This field is required type: string attach_workspace: default: false description: | Boolean for whether or not to attach to an existing workspace. Default is false. type: boolean auth: description: | The authentication method used to access your AWS account. Import the aws-cli orb in your config and provide the aws-cli/setup command to authenticate with your preferred method. View examples for more information. type: steps aws_domain: default: amazonaws.com description: | The AWS domain for your region, e.g in China, the AWS domain is amazonaws.com.cn The default value is amazonaws.com type: string build_path: default: . description: Path to the directory containing your build context. Defaults to . (working directory). type: string checkout: default: true description: | Boolean for whether or not to checkout as a first step. Default is true. type: boolean container_registry_login: default: false description: Enable login to different image container registries such as DockerHub, Heroku or Github. Defaults to false. type: boolean create_repo: default: false description: Should the repo be created if it does not exist? type: boolean 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. This parameter accepts multi-line arguments. If your argument spans multiple lines, please use the Folded Block Style denoted by `>-` (e.g. extra_build_args: >-). For examples of available flags, see https://docs.docker.com/engine/reference/commandline/buildx_build type: string lifecycle_policy_path: default: "" description: | The path to the .json file containing the lifecycle policy to be applied to a specified repository in AWS ECR. type: string no_output_timeout: default: 10m description: | The amount of time to allow the docker build command to run before timing out (default is `10m`) type: string path: default: . description: Path to the directory containing your Dockerfile. Defaults to . (working directory). type: string platform: default: linux/amd64 description: Platform targets for the docker image, multi arch images. Ex. linux/amd64,linux/arm64 type: string profile_name: default: default description: AWS profile name to be configured. type: string public_registry: default: false description: | Set to true if building and pushing an image to a Public Registry on ECR. type: boolean public_registry_alias: default: ${AWS_ECR_PUBLIC_REGISTRY_ALIAS} description: | The public registry alias for your public repositories. This parameter is required if pushing to a public repository It can be found in the Amazon ECR console > Public Registries. type: string push_image: default: true description: Set to false to build an image without pushing to repository. Defaults to true. type: boolean region: default: ${AWS_DEFAULT_REGION} description: | Name of env var storing your AWS region information, defaults to AWS_DEFAULT_REGION type: string registry_login: default: - run: |- echo "Error - container_registry_login parameter is set to true without any registry_login steps." - run: exit 1 description: Custom container registry login step e.g docker -u $DOCKER_ID -p $DOCKER_PASSWORD type: steps remote_docker_layer_caching: default: false description: | Enable Docker layer caching if using remote Docker engine. Defaults to false. type: boolean remote_docker_version: default: "" description: Specific remote docker version type: string repo: description: Name of an Amazon ECR repository type: string repo_encryption_kms_key: default: "" description: | If you use the KMS encryption type, specify the KMS key to use for encryption. The alias, key ID, or full ARN of the KMS key can be specified. type: string repo_encryption_type: default: AES256 description: The encryption type to use. enum: - AES256 - KMS type: enum repo_policy_path: default: "" description: | The path to the .json file containing the repository policy to be applied to a specified repository in AWS ECR. type: string repo_scan_on_push: default: true description: Should the created repo be security scanned on push? type: boolean set_repo_policy: default: false description: Should a repository policy be set? type: boolean 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 skip_when_tags_exist: default: false description: Whether to skip image building if all specified tags already exist in ECR type: boolean tag: default: latest description: | A comma-separated string containing docker image tags to build and push (default = latest) type: string use_credentials_helper: default: true description: | When true the authentication to docker registry will be done using the ecr-credential-helper. This avoids having the password saved in plain text. Otherwise it will use the classic docker login command which is more compatible. type: boolean 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.checkout>> steps: - checkout - when: condition: <<parameters.attach_workspace>> steps: - attach_workspace: at: <<parameters.workspace_root>> - when: condition: and: - <<parameters.remote_docker_layer_caching>> - not: <<parameters.setup_remote_docker>> steps: - run: echo "Docker Layer Caching requires Setup Remote Docker command" && exit 1 - when: condition: <<parameters.setup_remote_docker>> steps: - setup_remote_docker: docker_layer_caching: <<parameters.remote_docker_layer_caching>> version: <<parameters.remote_docker_version>> - steps: <<parameters.auth>> - ecr_login: account_id: <<parameters.account_id>> profile_name: <<parameters.profile_name>> public_registry: <<parameters.public_registry>> region: <<parameters.region>> use_credentials_helper: <<parameters.use_credentials_helper>> - when: condition: <<parameters.create_repo>> steps: - create_repo: encryption_kms_key: <<parameters.repo_encryption_kms_key>> profile_name: <<parameters.profile_name>> public_registry: <<parameters.public_registry>> region: <<parameters.region>> repo: <<parameters.repo>> repo_encryption_type: <<parameters.repo_encryption_type>> repo_scan_on_push: <<parameters.repo_scan_on_push>> - when: condition: <<parameters.set_repo_policy>> steps: - set_repo_policy: profile_name: <<parameters.profile_name>> public_registry: <<parameters.public_registry>> region: <<parameters.region>> repo: <<parameters.repo>> repo_policy_path: <<parameters.repo_policy_path>> - when: condition: <<parameters.container_registry_login>> steps: <<parameters.registry_login>> - build_image: account_id: <<parameters.account_id>> aws_domain: <<parameters.aws_domain>> build_path: <<parameters.build_path>> dockerfile: <<parameters.dockerfile>> extra_build_args: <<parameters.extra_build_args>> lifecycle_policy_path: <<parameters.lifecycle_policy_path>> no_output_timeout: <<parameters.no_output_timeout>> path: <<parameters.path>> platform: <<parameters.platform>> profile_name: <<parameters.profile_name>> public_registry: <<parameters.public_registry>> public_registry_alias: <<parameters.public_registry_alias>> push_image: <<parameters.push_image>> region: <<parameters.region>> repo: <<parameters.repo>> skip_when_tags_exist: <<parameters.skip_when_tags_exist>> tag: <<parameters.tag>> build_image: description: | Build a Docker image with docker buildx. NOTE: Some commands may not work with AWS CLI Version 1. parameters: account_id: default: ${AWS_ACCOUNT_ID} description: | The 12 digit AWS Account ID associated with the ECR account. This field is required type: string aws_domain: default: amazonaws.com description: | The AWS domain for your region, e.g in China, the AWS domain is amazonaws.com.cn The default value is amazonaws.com type: string build_path: 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. This parameter accepts multi-line arguments. If your argument spans multiple lines, please use the Folded Block Style denoted by `>-` (e.g. extra_build_args: >-). For examples of available flags, see https://docs.docker.com/engine/reference/commandline/buildx_build type: string lifecycle_policy_path: default: "" description: | The path to the .json file containing the lifecycle policy to be applied to a specified repository in AWS ECR. The docker buildx command uses a builder-context to build multi-architecture images simultaneously. In order for buildx builders to be saved with Docker Layer Caching, the builder must explicitly be named. Specify the a builder name with this parameter. This parameter defaults to "default-builder" if none is provided. type: string no_output_timeout: default: 10m description: The amount of time to allow the docker command to run before timing out. type: string path: default: . description: Path to the directory containing your Dockerfile. Defaults to . (working directory). type: string platform: default: linux/amd64 description: Platform targets for the docker image, multi arch images. Ex. linux/amd64,linux/arm64. type: string profile_name: default: default description: | AWS profile name to be configured. Only required when skip_when_tags_exist or ecr_login are set to true. type: string public_registry: default: false description: Set to true if building and pushing an image to a Public Registry on ECR. type: boolean public_registry_alias: default: ${AWS_ECR_PUBLIC_REGISTRY_ALIAS} description: | The public registry alias for your public repositories. This parameter is required if pushing to a public repository It can be found in the Amazon ECR console > Public Registries. type: string push_image: default: true description: Set to false to build an image without pushing to repository. Defaults to true. type: boolean region: default: ${AWS_DEFAULT_REGION} description: | AWS region of ECR repository. Defaults to environment variable ${AWS_DEFAULT_REGION} type: string repo: description: Name of an Amazon ECR repository type: string skip_when_tags_exist: default: false description: Whether to skip image building if all specified tags already exist in ECR type: boolean tag: default: latest description: A comma-separated string containing docker image tags (default = latest) type: string steps: - run: command: | #!/bin/bash AWS_ECR_EVAL_REGION="$(eval echo "${AWS_ECR_STR_REGION}")" AWS_ECR_EVAL_REPO="$(eval echo "${AWS_ECR_STR_REPO}")" AWS_ECR_EVAL_TAG="$(eval echo "${AWS_ECR_STR_TAG}")" AWS_ECR_EVAL_PATH="$(eval echo "${AWS_ECR_EVAL_PATH}")" AWS_ECR_STR_AWS_DOMAIN="$(echo "${AWS_ECR_STR_AWS_DOMAIN}" | circleci env subst)" AWS_ECR_EVAL_ACCOUNT_ID="$(eval echo "${AWS_ECR_STR_ACCOUNT_ID}")" AWS_ECR_VAL_ACCOUNT_URL="${AWS_ECR_EVAL_ACCOUNT_ID}.dkr.ecr.${AWS_ECR_EVAL_REGION}.${AWS_ECR_STR_AWS_DOMAIN}" AWS_ECR_EVAL_PUBLIC_REGISTRY_ALIAS="$(eval echo "${AWS_ECR_STR_PUBLIC_REGISTRY_ALIAS}")" AWS_ECR_STR_EXTRA_BUILD_ARGS="$(echo "${AWS_ECR_STR_EXTRA_BUILD_ARGS}" | circleci env subst)" AWS_ECR_EVAL_BUILD_PATH="$(eval echo "${AWS_ECR_EVAL_BUILD_PATH}")" AWS_ECR_EVAL_DOCKERFILE="$(eval echo "${AWS_ECR_STR_DOCKERFILE}")" AWS_ECR_EVAL_PROFILE_NAME="$(eval echo "${AWS_ECR_STR_PROFILE_NAME}")" AWS_ECR_EVAL_PLATFORM="$(eval echo "${AWS_ECR_STR_PLATFORM}")" AWS_ECR_EVAL_LIFECYCLE_POLICY_PATH="$(eval echo "${AWS_ECR_STR_LIFECYCLE_POLICY_PATH}")" # shellcheck disable=SC2034 # used indirectly via environment in `docker buildx` builds BUILDX_NO_DEFAULT_ATTESTATIONS=1 if [ -n "${AWS_ECR_STR_EXTRA_BUILD_ARGS}" ]; then args=() # shellcheck disable=SC2086 eval 'for p in '$AWS_ECR_STR_EXTRA_BUILD_ARGS'; do args+=("$p"); done' for arg in "${args[@]}"; do set -- "$@" "$arg" done fi ECR_COMMAND="ecr" number_of_tags_in_ecr=0 IFS=', ' read -ra platform \<<<"${AWS_ECR_EVAL_PLATFORM}" number_of_platforms="${#platform[@]}" if [ -z "${AWS_ECR_EVAL_ACCOUNT_ID}" ]; then echo "The account ID is not found. Please add the account ID before continuing." exit 1 fi if [ "${AWS_ECR_BOOL_PUBLIC_REGISTRY}" -eq "1" ]; then ECR_COMMAND="ecr-public" AWS_ECR_VAL_ACCOUNT_URL="public.ecr.aws/${AWS_ECR_EVAL_PUBLIC_REGISTRY_ALIAS}" fi IFS="," read -ra DOCKER_TAGS \<<<"${AWS_ECR_EVAL_TAG}" for tag in "${DOCKER_TAGS[@]}"; do if [ "${AWS_ECR_BOOL_SKIP_WHEN_TAGS_EXIST}" -eq "1" ] || [ "${AWS_ECR_BOOL_SKIP_WHEN_TAGS_EXIST}" = "true" ]; then docker_tag_exists_in_ecr=$(aws "${ECR_COMMAND}" describe-images --profile "${AWS_ECR_EVAL_PROFILE_NAME}" --registry-id "${AWS_ECR_EVAL_ACCOUNT_ID}" --region "${AWS_ECR_EVAL_REGION}" --repository-name "${AWS_ECR_EVAL_REPO}" --query "contains(imageDetails[].imageTags[], '${tag}')") if [ "${docker_tag_exists_in_ecr}" = "true" ]; then IFS="," read -ra PLATFORMS \<<<"${AWS_ECR_EVAL_PLATFORM}" for p in "${PLATFORMS[@]}"; do docker pull "${AWS_ECR_VAL_ACCOUNT_URL}/${AWS_ECR_EVAL_REPO}:${tag}" --platform "${p}" done number_of_tags_in_ecr=$((number_of_tags_in_ecr += 1)) fi fi docker_tag_args="${docker_tag_args} -t ${AWS_ECR_VAL_ACCOUNT_URL}/${AWS_ECR_EVAL_REPO}:${tag}" done if [ "${AWS_ECR_BOOL_SKIP_WHEN_TAGS_EXIST}" -eq "0" ] || [[ "${AWS_ECR_BOOL_SKIP_WHEN_TAGS_EXIST}" -eq "1" && ${number_of_tags_in_ecr} -lt ${#DOCKER_TAGS[@]} ]]; then if [ "${AWS_ECR_BOOL_PUSH_IMAGE}" -eq "1" ]; then set -- "$@" --push if [ -n "${AWS_ECR_EVAL_LIFECYCLE_POLICY_PATH}" ]; then aws ecr put-lifecycle-policy \ --profile "${AWS_ECR_EVAL_PROFILE_NAME}" \ --repository-name "${AWS_ECR_EVAL_REPO}" \ --lifecycle-policy-text "file://${AWS_ECR_EVAL_LIFECYCLE_POLICY_PATH}" fi elif [ "${AWS_ECR_BOOL_PUSH_IMAGE}" -eq "0" ] && [ "${number_of_platforms}" -le 1 ]; then set -- "$@" --load fi if [ "${number_of_platforms}" -gt 1 ]; then # In order to build multi-architecture images, a context with binfmt installed must be used. if ! docker context ls | grep builder; then # We need to skip the creation of the builder context if it's already present # otherwise the command will fail when called more than once in the same job. docker context create builder docker run --privileged --rm tonistiigi/binfmt --install all docker --context builder buildx create --name DLC_builder --use fi context_args="--context builder" # if no builder instance is currently used, create one elif ! docker buildx ls | grep -q "default * docker"; then set -x if ! docker buildx ls | grep -q DLC_builder; then docker buildx create --name DLC_builder --use else docker buildx use DLC_builder fi echo "Context is set to DLC_builder" set +x fi set -x docker \ ${context_args:+$context_args} \ buildx build \ -f "${AWS_ECR_EVAL_PATH}"/"${AWS_ECR_EVAL_DOCKERFILE}" \ ${docker_tag_args:+$docker_tag_args} \ --platform "${AWS_ECR_EVAL_PLATFORM}" \ --progress plain \ "$@" \ "${AWS_ECR_EVAL_BUILD_PATH}" set +x fi environment: AWS_ECR_BOOL_PUBLIC_REGISTRY: <<parameters.public_registry>> AWS_ECR_BOOL_PUSH_IMAGE: <<parameters.push_image>> AWS_ECR_BOOL_SKIP_WHEN_TAGS_EXIST: <<parameters.skip_when_tags_exist>> AWS_ECR_EVAL_BUILD_PATH: <<parameters.build_path>> AWS_ECR_EVAL_PATH: <<parameters.path>> AWS_ECR_STR_ACCOUNT_ID: <<parameters.account_id>> AWS_ECR_STR_AWS_DOMAIN: <<parameters.aws_domain>> AWS_ECR_STR_DOCKERFILE: <<parameters.dockerfile>> AWS_ECR_STR_EXTRA_BUILD_ARGS: <<parameters.extra_build_args>> AWS_ECR_STR_LIFECYCLE_POLICY_PATH: <<parameters.lifecycle_policy_path>> AWS_ECR_STR_PLATFORM: <<parameters.platform>> AWS_ECR_STR_PROFILE_NAME: <<parameters.profile_name>> AWS_ECR_STR_PUBLIC_REGISTRY_ALIAS: <<parameters.public_registry_alias>> AWS_ECR_STR_REGION: <<parameters.region>> AWS_ECR_STR_REPO: << parameters.repo >> AWS_ECR_STR_TAG: << parameters.tag >> name: Build Docker Image with buildx no_output_timeout: <<parameters.no_output_timeout>> create_repo: description: | Create a new AWS ECR repository. NOTE: Some commands may not work with AWS CLI Version 1. parameters: encryption_kms_key: default: "" description: | If you use the KMS encryption type, specify the KMS key to use for encryption. The alias, key ID, or full ARN of the KMS key can be specified. type: string profile_name: default: default description: AWS profile name to be configured. type: string public_registry: default: false description: Set to true if building and pushing an image to a Public Registry on ECR. type: boolean region: default: ${AWS_DEFAULT_REGION} description: | AWS region of ECR repository. Defaults to environment variable ${AWS_DEFAULT_REGION} type: string repo: description: Name of an Amazon ECR repository type: string repo_encryption_type: default: AES256 description: The encryption type to use. enum: - AES256 - KMS type: enum repo_scan_on_push: default: true description: Should the created repo be security scanned on push? type: boolean steps: - run: command: | #!/bin/bash AWS_ECR_EVAL_REGION="$(eval echo "${AWS_ECR_STR_REGION}")" AWS_ECR_EVAL_REPO="$(eval echo "${AWS_ECR_STR_REPO}")" AWS_ECR_EVAL_PROFILE_NAME="$(eval echo "${AWS_ECR_STR_PROFILE_NAME}")" AWS_ECR_EVAL_ENCRYPTION_KMS_KEY="$(eval echo "${AWS_ECR_STR_ENCRYPTION_KMS_KEY}")" if [ "$AWS_ECR_BOOL_PUBLIC_REGISTRY" == "1" ]; then aws ecr-public describe-repositories --profile "${AWS_ECR_EVAL_PROFILE_NAME}" --region us-east-1 --repository-names "${AWS_ECR_EVAL_REPO}" >/dev/null 2>&1 || aws ecr-public create-repository --profile "${AWS_ECR_EVAL_PROFILE_NAME}" --region us-east-1 --repository-name "${AWS_ECR_EVAL_REPO}" else IMAGE_SCANNING_CONFIGURATION="scanOnPush=true" if [ "$AWS_ECR_BOOL_REPO_SCAN_ON_PUSH" -ne "1" ]; then IMAGE_SCANNING_CONFIGURATION="scanOnPush=false" fi ENCRYPTION_CONFIGURATION="encryptionType=${AWS_ECR_ENUM_ENCRYPTION_TYPE}" if [ "$AWS_ECR_ENUM_ENCRYPTION_TYPE" == "KMS" ]; then ENCRYPTION_CONFIGURATION+=",kmsKey=${AWS_ECR_EVAL_ENCRYPTION_KMS_KEY}" fi aws ecr describe-repositories \ --profile "${AWS_ECR_EVAL_PROFILE_NAME}" \ --region "${AWS_ECR_EVAL_REGION}" \ --repository-names "${AWS_ECR_EVAL_REPO}" >/dev/null 2>&1 || aws ecr create-repository \ --profile "${AWS_ECR_EVAL_PROFILE_NAME}" \ --region "${AWS_ECR_EVAL_REGION}" \ --repository-name "${AWS_ECR_EVAL_REPO}" \ --image-scanning-configuration "${IMAGE_SCANNING_CONFIGURATION}" \ --encryption-configuration "${ENCRYPTION_CONFIGURATION}" fi environment: AWS_ECR_BOOL_PUBLIC_REGISTRY: <<parameters.public_registry>> AWS_ECR_BOOL_REPO_SCAN_ON_PUSH: <<parameters.repo_scan_on_push>> AWS_ECR_ENUM_ENCRYPTION_TYPE: <<parameters.repo_encryption_type>> AWS_ECR_STR_ENCRYPTION_KMS_KEY: <<parameters.encryption_kms_key>> AWS_ECR_STR_PROFILE_NAME: <<parameters.profile_name>> AWS_ECR_STR_REGION: <<parameters.region>> AWS_ECR_STR_REPO: <<parameters.repo>> name: Create Repository ecr_login: description: | Authenticate into the Amazon ECR service. This command requires jq. NOTE: Some commands may not work with AWS CLI Version 1. parameters: account_id: default: ${AWS_ACCOUNT_ID} description: | The 12 digit AWS id associated with the ECR account. This field is required type: string aws_domain: default: amazonaws.com description: | AWS domain, China regions will require override. type: string profile_name: default: default description: | AWS profile name to be used for login. type: string public_registry: default: false description: Set to true if building and pushing an image to a Public Registry on ECR. type: boolean region: default: ${AWS_DEFAULT_REGION} description: | AWS region of ECR repository. Defaults to environment variable ${AWS_DEFAULT_REGION} type: string use_credentials_helper: default: true description: | When true the authentication to docker registry will be done using the ecr-credential-helper. This avoids having the password saved in plain text. Otherwise it will use the classic docker login command which is more compatible. If you are having issues with credentials helper login, set this to false. type: boolean steps: - run: command: |- #!/bin/bash AWS_ECR_EVAL_REGION="$(eval echo "${AWS_ECR_STR_REGION}")" AWS_ECR_EVAL_PROFILE_NAME="$(eval echo "${AWS_ECR_STR_PROFILE_NAME}")" AWS_ECR_EVAL_ACCOUNT_ID="$(eval echo "${AWS_ECR_STR_ACCOUNT_ID}")" AWS_ECR_VAL_ACCOUNT_URL="${AWS_ECR_EVAL_ACCOUNT_ID}.dkr.ecr.${AWS_ECR_EVAL_REGION}.${AWS_ECR_STR_AWS_DOMAIN}" AWS_ECR_EVAL_PUBLIC_REGISTRY_ALIAS="$(eval echo "${AWS_ECR_STR_PUBLIC_REGISTRY_ALIAS}")" AWS_ECR_BOOL_HELPER="$(eval echo "${AWS_ECR_BOOL_HELPER}")" ECR_COMMAND="ecr" eval "$SCRIPT_UTILS" detect_os set_sudo if [ -z "${AWS_ECR_EVAL_ACCOUNT_ID}" ]; then echo "The account ID is not found. Please add the account ID before continuing." exit 1 fi if [ "$AWS_ECR_BOOL_PUBLIC_REGISTRY" == "1" ]; then AWS_ECR_EVAL_REGION="us-east-1" AWS_ECR_VAL_ACCOUNT_URL="public.ecr.aws/${AWS_ECR_EVAL_PUBLIC_REGISTRY_ALIAS}" ECR_COMMAND="ecr-public" aws "${ECR_COMMAND}" get-login-password --region "${AWS_ECR_EVAL_REGION}" --profile "${AWS_ECR_EVAL_PROFILE_NAME}" \ | docker login --username AWS --password-stdin "${AWS_ECR_VAL_ACCOUNT_URL}" exit 0 fi if [ -f "$HOME/.docker/config.json" ] && grep "${AWS_ECR_VAL_ACCOUNT_URL}" < ~/.docker/config.json > /dev/null 2>&1 ; then echo "Credential helper is already installed and configured" exit 0 fi configure_config_json(){ echo "Configuring config.json..." CONFIG_FILE="$HOME/.docker/config.json" mkdir -p "$(dirname "${CONFIG_FILE}")" jq_flag="" if [ ! -s "${CONFIG_FILE}" ]; then jq_flag="-n" fi jq ${jq_flag} --arg url "${AWS_ECR_VAL_ACCOUNT_URL}" \ --arg helper "ecr-login" '.credHelpers[$url] = $helper' \ "${CONFIG_FILE}" > temp.json && mv temp.json "${CONFIG_FILE}" } install_aws_ecr_credential_helper(){ echo "Installing AWS ECR Credential Helper..." if [[ "$SYS_ENV_PLATFORM" = "linux" && "$AWS_ECR_BOOL_HELPER" = "1" ]]; then HELPER_INSTALLED=$(dpkg --get-selections | (grep amazon-ecr-credential-helper || test $?) | awk '{print $2}') if [[ "$HELPER_INSTALLED" != "install" ]]; then $SUDO apt update $SUDO apt -y install amazon-ecr-credential-helper fi configure_config_json elif [[ "$SYS_ENV_PLATFORM" = "macos" && "$AWS_ECR_BOOL_HELPER" = "1" ]]; then HELPER_INSTALLED=$(brew list -q | grep -q docker-credential-helper-ecr || test $?) if [[ "$HELPER_INSTALLED" -ne 0 ]]; then brew install docker-credential-helper-ecr fi configure_config_json else docker logout "${AWS_ECR_VAL_ACCOUNT_URL}" aws "${ECR_COMMAND}" get-login-password --region "${AWS_ECR_EVAL_REGION}" --profile "${AWS_ECR_EVAL_PROFILE_NAME}" | docker login --username AWS --password-stdin "${AWS_ECR_VAL_ACCOUNT_URL}" fi } install_aws_ecr_credential_helper environment: AWS_ECR_BOOL_HELPER: <<parameters.use_credentials_helper>> AWS_ECR_BOOL_PUBLIC_REGISTRY: <<parameters.public_registry>> AWS_ECR_STR_ACCOUNT_ID: <<parameters.account_id>> AWS_ECR_STR_AWS_DOMAIN: <<parameters.aws_domain>> AWS_ECR_STR_PROFILE_NAME: <<parameters.profile_name>> AWS_ECR_STR_REGION: <<parameters.region>> SCRIPT_UTILS: "#!/bin/bash\n\ndetect_os() { \n detected_platform=\"$(uname -s | tr '[:upper:]' '[:lower:]')\"\n\n case \"$detected_platform\" in\n linux*)\n if grep \"Alpine\" /etc/issue >/dev/null 2>&1; then\n printf '%s\\n' \"Detected OS: Alpine Linux.\"\n SYS_ENV_PLATFORM=linux_alpine\n else\n printf '%s\\n' \"Detected OS: Linux.\"\n SYS_ENV_PLATFORM=linux\n fi \n ;;\n darwin*)\n printf '%s\\n' \"Detected OS: macOS.\"\n SYS_ENV_PLATFORM=macos\n ;;\n msys*|cygwin*)\n printf '%s\\n' \"Detected OS: Windows.\"\n SYS_ENV_PLATFORM=windows\n ;;\n *)\n printf '%s\\n' \"Unsupported OS: \\\"$detected_platform\\\".\"\n exit 1\n ;;\n esac\n\n export SYS_ENV_PLATFORM\n}\n\nset_sudo(){\n if [ \"$SYS_ENV_PLATFORM\" = \"linux_alpine\" ]; then\n if [ \"$ID\" = 0 ]; then export SUDO=\"\"; else export SUDO=\"sudo\"; fi\n else\n if [ \"$EUID\" = 0 ]; then export SUDO=\"\"; else export SUDO=\"sudo\"; fi\n fi\n}" name: Log into Amazon ECR with profile <<parameters.profile_name>> push_image: description: | Push a container image to the Amazon ECR registry NOTE: Some commands may not work with AWS CLI Version 1. parameters: account_id: default: ${AWS_ACCOUNT_ID} description: | The 12 digit AWS Account ID associated with the ECR account. This field is required type: string aws_domain: default: amazonaws.com description: | AWS domain, China regions will require override. type: string public_registry: default: false description: Set to true if building and pushing an image to a Public Registry on ECR. type: boolean public_registry_alias: default: ${AWS_ECR_PUBLIC_REGISTRY_ALIAS} description: | The public registry alias for your public repositories. This parameter is required if pushing to a public repository It can be found in the Amazon ECR console > Public Registries. type: string region: default: ${AWS_DEFAULT_REGION} description: | AWS region of ECR repository. Defaults to environment variable ${AWS_DEFAULT_REGION} type: string repo: description: Name of an Amazon ECR repository type: string tag: default: latest description: A comma-separated string containing docker image tags (default = latest) type: string steps: - run: command: |- #!/bin/bash AWS_ECR_EVAL_REPO="$(eval echo "${AWS_ECR_STR_REPO}")" AWS_ECR_EVAL_TAG="$(eval echo "${AWS_ECR_STR_TAG}")" AWS_ECR_EVAL_REGION="$(eval echo "${AWS_ECR_STR_REGION}")" AWS_ECR_EVAL_ACCOUNT_ID="$(eval echo "${AWS_ECR_STR_ACCOUNT_ID}")" AWS_ECR_VAL_ACCOUNT_URL="${AWS_ECR_EVAL_ACCOUNT_ID}.dkr.ecr.${AWS_ECR_EVAL_REGION}.amazonaws.com" AWS_ECR_EVAL_PUBLIC_REGISTRY_ALIAS="$(eval echo "${AWS_ECR_STR_PUBLIC_REGISTRY_ALIAS}")" echo "$AWS_ECR_VAL_ACCOUNT_URL" >> test.txt if [ -z "${AWS_ECR_EVAL_ACCOUNT_ID}" ]; then echo "The account ID is not found. Please add the account ID before continuing." exit 1 fi if [ "${AWS_ECR_BOOL_PUBLIC_REGISTRY}" == "1" ]; then AWS_ECR_VAL_ACCOUNT_URL="public.ecr.aws/${AWS_ECR_EVAL_PUBLIC_REGISTRY_ALIAS}" fi IFS="," read -ra DOCKER_TAGS \<<< "${AWS_ECR_EVAL_TAG}" for tag in "${DOCKER_TAGS[@]}"; do set -x docker push "${AWS_ECR_VAL_ACCOUNT_URL}/${AWS_ECR_EVAL_REPO}:${tag}" set +x done environment: AWS_ECR_BOOL_PUBLIC_REGISTRY: <<parameters.public_registry>> AWS_ECR_STR_ACCOUNT_ID: << parameters.account_id >> AWS_ECR_STR_AWS_DOMAIN: <<parameters.aws_domain>> AWS_ECR_STR_PUBLIC_REGISTRY_ALIAS: <<parameters.public_registry_alias>> AWS_ECR_STR_REGION: << parameters.region >> AWS_ECR_STR_REPO: << parameters.repo >> AWS_ECR_STR_TAG: << parameters.tag >> name: Push image to AWS ECR set_repo_policy: description: | Set a repository policy on an AWS ECR repository NOTE: Some commands may not work with AWS CLI Version 1. parameters: profile_name: default: default description: AWS profile name to be configured. type: string public_registry: default: false description: Set to true if building and pushing an image to a Public Registry on ECR. type: boolean region: default: ${AWS_DEFAULT_REGION} description: | AWS region of ECR repository. Defaults to environment variable ${AWS_DEFAULT_REGION} type: string repo: description: Name of an Amazon ECR repository type: string repo_policy_path: default: "" description: | The path to the .json file containing the repository policy to be applied to a specified repository in AWS ECR. type: string steps: - run: command: | #!/bin/bash AWS_ECR_EVAL_REGION="$(eval echo "${AWS_ECR_STR_REGION}")" AWS_ECR_EVAL_REPO="$(eval echo "${AWS_ECR_STR_REPO}")" AWS_ECR_EVAL_PROFILE_NAME="$(eval echo "${AWS_ECR_STR_PROFILE_NAME}")" AWS_ECR_EVAL_REPO_POLICY_PATH="$(eval echo "${AWS_ECR_STR_REPO_POLICY_PATH}")" if [ "$AWS_ECR_BOOL_PUBLIC_REGISTRY" == "1" ]; then echo "set-repository-policy is not supported on public repos" exit 1 else aws ecr set-repository-policy \ --profile "${AWS_ECR_EVAL_PROFILE_NAME}" \ --region "${AWS_ECR_EVAL_REGION}" \ --repository-name "${AWS_ECR_EVAL_REPO}" \ --policy-text "file://${AWS_ECR_EVAL_REPO_POLICY_PATH}" fi environment: AWS_ECR_BOOL_PUBLIC_REGISTRY: <<parameters.public_registry>> AWS_ECR_STR_PROFILE_NAME: <<parameters.profile_name>> AWS_ECR_STR_REGION: <<parameters.region>> AWS_ECR_STR_REPO: <<parameters.repo>> AWS_ECR_STR_REPO_POLICY_PATH: <<parameters.repo_policy_path>> name: Set Repository Policy tag_image: description: | Add a tag to an existing published image NOTE: Some commands may not work with AWS CLI Version 1. parameters: profile_name: default: default description: AWS profile to use type: string repo: description: Name of an Amazon ECR repository type: string skip_when_tags_exist: default: false description: Whether to skip tagging an image if any specified tags already exist type: boolean source_tag: description: An existing Docker image tag type: string target_tag: default: latest description: A comma-separated string containing docker image tags (default = latest) type: string steps: - run: command: | #!/bin/bash AWS_ECR_EVAL_REPO="$(eval echo "${AWS_ECR_STR_REPO}")" AWS_ECR_EVAL_SOURCE_TAG="$(eval echo "${AWS_ECR_STR_SOURCE_TAG}")" AWS_ECR_EVAL_TARGET_TAG="$(eval echo "${AWS_ECR_STR_TARGET_TAG}")" AWS_ECR_EVAL_AWS_PROFILE="$(eval echo "${AWS_ECR_STR_AWS_PROFILE}")" # pull the image manifest from ECR set -x MANIFEST="$(aws ecr batch-get-image --repository-name "${AWS_ECR_EVAL_REPO}" --image-ids imageTag="${AWS_ECR_EVAL_SOURCE_TAG}" --query 'images[].imageManifest' --output text --profile "${AWS_ECR_EVAL_AWS_PROFILE}")" # only list images when needed if [ "${AWS_ECR_BOOL_SKIP_WHEN_TAGS_EXIST}" -eq 1 ]; then EXISTING_TAGS="$(aws ecr list-images --repository-name "${AWS_ECR_EVAL_REPO}" --filter "tagStatus=TAGGED" --profile "${AWS_ECR_EVAL_AWS_PROFILE}")" fi IFS="," read -ra ECR_TAGS \<<<"${AWS_ECR_EVAL_TARGET_TAG}" for tag in "${ECR_TAGS[@]}"; do # if skip_when_tags_exist is true if [ "${AWS_ECR_BOOL_SKIP_WHEN_TAGS_EXIST}" -eq 1 ]; then # tag image if tag does not exist if ! echo "${EXISTING_TAGS}" | grep "${tag}"; then aws ecr put-image --repository-name "${AWS_ECR_EVAL_REPO}" --image-tag "${tag}" --image-manifest "${MANIFEST}" --profile "${AWS_ECR_EVAL_AWS_PROFILE}" else echo "Tag \"${tag}\" already exists and will be skipped." fi # tag image when skip_when_tags_exist is false else aws ecr put-image --repository-name "${AWS_ECR_EVAL_REPO}" --image-tag "${tag}" --image-manifest "${MANIFEST}" --profile "${AWS_ECR_EVAL_AWS_PROFILE}" fi done set +x environment: AWS_ECR_BOOL_SKIP_WHEN_TAGS_EXIST: <<parameters.skip_when_tags_exist>> AWS_ECR_STR_AWS_PROFILE: <<parameters.profile_name>> AWS_ECR_STR_REPO: <<parameters.repo>> AWS_ECR_STR_SOURCE_TAG: <<parameters.source_tag>> AWS_ECR_STR_TARGET_TAG: <<parameters.target_tag>> name: <<parameters.target_tag>> tag to <<parameters.repo>>:<<parameters.source_tag>> executors: default: description: | CircleCI's Ubuntu-based machine executor VM: https://circleci.com/docs/2.0/executor-types/#using-machine machine: docker_layer_caching: <<parameters.docker_layer_caching>> image: <<parameters.image>> parameters: docker_layer_caching: default: false type: boolean image: default: ubuntu-2204:current type: string resource_class: default: medium type: string resource_class: <<parameters.resource_class>> jobs: build_and_push_image: description: | Log into Amazon ECR and push image to repository. Authentication with OIDC or static AWS keys using the aws-cli/setup command is required. NOTE: Some commands may not work with AWS CLI Version 1. executor: << parameters.executor >> parameters: account_id: default: ${AWS_ACCOUNT_ID} description: | The 12 digit AWS id associated with the ECR account. This field is required type: string attach_workspace: default: false description: | Boolean for whether or not to attach to an existing workspace. Default is false. type: boolean auth: description: | The authentication method used to access your AWS account. Import the aws-cli orb in your config and provide the aws-cli/setup command to authenticate with your preferred method. View examples for more information. type: steps aws_domain: default: amazonaws.com description: | The AWS domain for your region, e.g in China, the AWS domain is amazonaws.com.cn The default value is amazonaws.com type: string build_path: default: . description: Path to the directory containing your build context. Defaults to . (working directory). type: string checkout: default: true description: | Boolean for whether or not to checkout as a first step. Default is true. type: boolean container_registry_login: default: false description: Enable login to different image container registries such as DockerHub, Heroku or Github. Defaults to false. type: boolean create_repo: default: false description: Should the repo be created if it does not exist? type: boolean 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. This parameter accepts multi-line arguments. If your argument spans multiple lines, please use the Folded Block Style denoted by `>-` (e.g. extra_build_args: >-). For examples of available flags, see https://docs.docker.com/engine/reference/commandline/buildx_build type: string lifecycle_policy_path: default: "" description: | The path to the .json file containing the lifecycle policy to be applied to a specified repository in AWS ECR. type: string no_output_timeout: default: 10m description: | The amount of time to allow the docker build command to run before timing out. Defaults to '10m' type: string path: default: . description: Path to the directory containing your Dockerfile. Defaults to . (working directory). type: string platform: default: linux/amd64 description: Platform targets for the docker image, multi arch images. Ex. linux/amd64,linux/arm64 type: string profile_name: default: default description: AWS profile name to be configured. type: string public_registry: default: false description: Set to true if building and pushing an image to a Public Registry on ECR. type: boolean public_registry_alias: default: ${AWS_ECR_PUBLIC_REGISTRY_ALIAS} description: | The public registry alias for your public repositories. This parameter is required if pushing to a public repository It can be found in the Amazon ECR console > Public Registries. type: string push_image: default: true description: Set to false to build an image without pushing to repository. Defaults to true. type: boolean region: default: ${AWS_DEFAULT_REGION} description: | AWS region of ECR repository. Defaults to environment variable ${AWS_DEFAULT_REGION} type: string registry_login: default: - run: |- echo "Error - container_registry_login parameter is set to true without any registry_login steps." - run: exit 1 description: Custom container registry login step e.g docker -u $DOCKER_ID -p $DOCKER_PASSWORD type: steps remote_docker_layer_caching: default: false description: | Enable Docker layer caching if using remote Docker engine. Requires setup_remote_docker to be set to true. Defaults to false. type: boolean remote_docker_version: default: "" description: Specific remote docker version type: string repo: description: Name of an Amazon ECR repository type: string repo_encryption_kms_key: default: "" description: | If you use the KMS encryption type, specify the KMS key to use for encryption. The alias, key ID, or full ARN of the KMS key can be specified. type: string repo_encryption_type: default: AES256 description: The encryption type to use. enum: - AES256 - KMS type: enum repo_policy_path: default: "" description: | The path to the .json file containing the repository policy to be applied to a specified repository in AWS ECR. type: string repo_scan_on_push: default: true description: Should the created repo be security scanned on push? type: boolean set_repo_policy: default: false description: Should a repository policy be set? type: boolean 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 skip_when_tags_exist: default: false description: Whether to skip image building if all specified tags already exist in ECR type: boolean tag: default: latest description: A comma-separated string containing docker image tags to build and push (default = latest) type: string use_credentials_helper: default: true description: | When true the authentication to docker registry will be done using the ecr-credential-helper. This avoids having the password saved in plain text. Otherwise it will use the classic docker login command which is more compatible. type: boolean 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: - build_and_push_image: account_id: <<parameters.account_id>> attach_workspace: <<parameters.attach_workspace>> auth: <<parameters.auth>> aws_domain: <<parameters.aws_domain>> build_path: <<parameters.build_path>> checkout: <<parameters.checkout>> container_registry_login: <<parameters.container_registry_login>> create_repo: <<parameters.create_repo>> dockerfile: <<parameters.dockerfile>> extra_build_args: <<parameters.extra_build_args>> lifecycle_policy_path: <<parameters.lifecycle_policy_path>> no_output_timeout: <<parameters.no_output_timeout>> path: <<parameters.path>> platform: <<parameters.platform>> profile_name: <<parameters.profile_name>> public_registry: <<parameters.public_registry>> public_registry_alias: <<parameters.public_registry_alias>> push_image: <<parameters.push_image>> region: <<parameters.region>> registry_login: <<parameters.registry_login>> remote_docker_layer_caching: <<parameters.remote_docker_layer_caching>> remote_docker_version: <<parameters.remote_docker_version>> repo: <<parameters.repo>> repo_encryption_kms_key: <<parameters.repo_encryption_kms_key>> repo_encryption_type: <<parameters.repo_encryption_type>> repo_policy_path: <<parameters.repo_policy_path>> repo_scan_on_push: <<parameters.repo_scan_on_push>> set_repo_policy: <<parameters.set_repo_policy>> setup_remote_docker: <<parameters.setup_remote_docker>> skip_when_tags_exist: <<parameters.skip_when_tags_exist>> tag: <<parameters.tag>> use_credentials_helper: <<parameters.use_credentials_helper>> workspace_root: <<parameters.workspace_root>> examples: build_and_push_image_w_registry_login: description: | In some cases, images being built using the build_and_push_image job need to pull a custom Docker image from a container registry like DockerHub, Heroku or GitHub Container Registry. This requires users to log into these registries first before the images can be built. This is an example of a using the build_and_push_image job with a custom registry login step. NOTE: The container_registry_login parameter must be set to true. Registry usernames and passwords are stored as environment variables in CircleCI with this example. usage: version: "2.1" orbs: aws-cli: circleci/aws-cli@5.1 aws-ecr: circleci/aws-ecr@9.0 workflows: build-and-push-image-with-container-registry-login: jobs: - aws-ecr/build_and_push_image: auth: - aws-cli/setup: role_arn: arn:aws:iam::123456789012 container_registry_login: true dockerfile: Dockerfile path: . region: us-west-2 registry_login: - run: docker login -u ${HEROKU_USERNAME} -p ${HEROKU_API_KEY} - run: docker login -u ${GITHUB_USERNAME} -p ${GITHUB_TOKEN} - run: docker login -u ${DOCKERHUB_ID} -p ${DOCKERHUB_PASSWORD} repo: my-sample-repo tag: sampleTag build_test_then_push_image: description: | This is an example of a job that builds a docker image with docker buildx. It tests the image and before pushing it to the specified ECR repository. NOTE: The push_image parameter must be set to false. Only one platform can be specified and loaded into the local Docker Daemon. Loading multi-architecture images is not supported at this time. usage: version: "2.1" orbs: aws-cli: circleci/aws-cli@5.1 aws-ecr: circleci/aws-ecr@9.0 jobs: build-test-then-push-with-buildx: machine: image: ubuntu-2204:current steps: - aws-ecr/build_and_push_image: attach_workspace: true auth: - aws-cli/setup: role_arn: arn:aws:iam::123456789012 create_repo: true dockerfile: Dockerfile path: workspace platform: linux/amd64 push_image: false region: us-west-2 repo: my-sample-repo tag: sampleTag workspace_root: . - run: command: | set -x docker run 123456789012.dkr.ecr.us-west-2.amazonaws.com/<< parameters.repo >>:<< parameters.tag >> ping -V status=$(echo "$?") if [ "${status}" != "0" ]; then exit 1; else exit 0; fi set +x name: Tests for docker image - aws-ecr/push_image: region: << parameters.region >> repo: << parameters.repo >> tag: << parameters.tag >> workflows: build-image-test-image-push-image-with-buildx: jobs: - build-test-then-push-with-buildx: context: CircleCI_OIDC_Token simple_build_and_push: description: | Log into AWS, build and push image to Amazon ECR using OIDC for authentication. Import the aws-cli orb and authenticate using the aws-cli/setup command with a valid role-arn for OIDC authentication. usage: version: "2.1" orbs: aws-cli: circleci/aws-cli@5.1 aws-ecr: circleci/aws-ecr@9.0 workflows: build_and_push_image: jobs: - aws-ecr/build_and_push_image: account_id: ${AWS_ACCOUNT_ID} auth: - aws-cli/setup: profile_name: OIDC-USER role_arn: arn:aws:iam::123456789012:role/VALID_OIDC_ECR_ROLE context: CircleCI_OIDC_Token create_repo: true dockerfile: myDockerfile executors: base extra_build_args: --compress no_output_timeout: 20m path: pathToMyDockerfile platform: linux/amd64 profile_name: OIDC-User public_registry: false push_image: true region: ${AWS_DEFAULT_REGION} repo: myECRRepository repo_encryption_kms_key: arn:aws:kms::123456789012:key/UUID4_OF_KMS_KEY_ID repo_encryption_type: KMS repo_policy_path: repo-policy.json repo_scan_on_push: true set_repo_policy: true skip_when_tags_exist: false tag: latest,myECRRepoTag
Developer Updates
Get tips to optimize your builds
Or join our research panel and give feedback
By submitting this form, you are agreeing to ourTerms of UseandPrivacy Policy.