55#
66# Usage:
77#
8- # docker-publish
8+ # docker-publish
99# optional flag "-r private-repos" enables Docker Build Kit to allow ssh socket mounting to pull private modules/repos
1010
1111
@@ -42,17 +42,11 @@ image_exists() {
4242}
4343
4444check_ecr_vars () {
45- # ECR required env vars. If OIDC_ECR_UPLOAD_ROLE is defined via context, then use it to login to aws.
46- if [[ -z $OIDC_ECR_UPLOAD_ROLE ]]; then
47- if [[ -z $ECR_ACCOUNT_ID ]]; then echo " Missing var for ECR: ECR_ACCOUNT_ID" && exit 1; fi
48- if [[ -z $ECR_PUSH_SECRET ]]; then echo " Missing var for ECR: ECR_PUSH_SECRET" && exit 1; fi
49- else
50- if [ -z " ${CIRCLE_OIDC_TOKEN_V2} " ]; then
51- echo " OIDC Token cannot be found. A CircleCI context must be specified."
52- exit 1
53- fi
54- echo " Using AWS role defined in \$ OIDC_ECR_UPLOAD_ROLE to login to aws ecr"
45+ if [ -z " ${CIRCLE_OIDC_TOKEN_V2} " ]; then
46+ echo " OIDC Token cannot be found. A CircleCI context must be specified."
47+ exit 1
5548 fi
49+ echo " Using AWS role defined in \$ OIDC_ECR_UPLOAD_ROLE to login to aws ecr"
5650}
5751
5852ecr_login (){
@@ -86,9 +80,9 @@ SHORT_SHA=${CIRCLE_SHA1:0:7}
8680
8781ORG=clever
8882
89- ECR_REGION_US_WEST_1= us-west-1
83+ # Only push to us-west-2, images are replicated to other regions
9084ECR_REGION_US_WEST_2=us-west-2
91- ECR_REGION_US_EAST_1 =us-east -1
85+ ECR_REGION_US_WEST_1 =us-west -1
9286AWS_ECR_PROFILE=oidc-ecr-profile
9387
9488echo " Docker version..."
@@ -99,47 +93,32 @@ check_ecr_vars
9993
10094install_awscli
10195
102- # Some Dockerfiles for private repos depend on public images (and vice versa) in us-west-1
103- echo " If necessary, add the ECR_BUILD_ID and ECR_BUILD_SECRET env vars to circle manually."
104- echo " They can be found in init-service as CI_ECR_XXX_KEY and CI_ECR_XXX_SECRET."
96+ # Some Dockerfiles for private repos depend on public images (and vice
97+ # versa) in us-west-1. This login allows us to pull the images for the
98+ # final image build.
99+ echo " If necessary, add the ECR_BUILD_ID env var to circle manually."
105100if [[ -n $ECR_BUILD_ID ]]; then
106- if [[ -z $OIDC_ECR_UPLOAD_ROLE ]]; then
107- echo " Logging into ECR in us-west-1 using static credentials..."
108- ecr_login us-west-1 $ECR_BUILD_ID $ECR_BUILD_SECRET
109- else
110- echo " Logging into ECR using role credentials..."
111- assume_role_with_web_identity $OIDC_ECR_UPLOAD_ROLE $AWS_ECR_PROFILE
112- ecr_login_with_profile $ECR_REGION_US_WEST_1
113- fi
101+ echo " Logging into ECR using role credentials..."
102+ assume_role_with_web_identity $OIDC_ECR_UPLOAD_ROLE $AWS_ECR_PROFILE
103+ ecr_login_with_profile $ECR_REGION_US_WEST_1
114104fi
115105
116- if [ -z " $( docker images -q $ORG /$REPO :$SHORT_SHA ) " ]; then
117- echo " Building docker image..."
106+ if [ -z " $( docker images -q $ORG /$REPO :$SHORT_SHA ) " ]; then
107+ echo " Building docker image..."
118108 if [ $readopt == " private-repos" ]; then
119109 echo " With Private Repos..."
120- DOCKER_BUILDKIT=1 docker build --ssh default -t $ORG /$REPO :$SHORT_SHA .
110+ DOCKER_BUILDKIT=1 docker build --ssh default -t $ORG /$REPO :$SHORT_SHA .
121111 else
122112 docker build -t $ORG /$REPO :$SHORT_SHA .
123113 fi
124114else
125115 echo " Image already exists... skipping build"
126116fi
127117
128- # ECR login.
129- if [[ -z $OIDC_ECR_UPLOAD_ROLE ]]; then
130- echo " Logging into ECR using static credentials..."
131- ecr_login $ECR_REGION_US_WEST_1 $ECR_PUSH_ID $ECR_PUSH_SECRET
132- ecr_login $ECR_REGION_US_WEST_2 $ECR_PUSH_ID $ECR_PUSH_SECRET
133- ecr_login $ECR_REGION_US_EAST_1 $ECR_PUSH_ID $ECR_PUSH_SECRET
134- else
135- echo " Logging into ECR using role credentials..."
136- assume_role_with_web_identity $OIDC_ECR_UPLOAD_ROLE $AWS_ECR_PROFILE
137- ecr_login_with_profile $ECR_REGION_US_WEST_1
138- ecr_login_with_profile $ECR_REGION_US_WEST_2
139- ecr_login_with_profile $ECR_REGION_US_EAST_1
140- fi
118+ # ECR login. Only push to us-west-2, images are replicated to other regions.
119+ echo " Logging into ECR using role credentials..."
120+ assume_role_with_web_identity $OIDC_ECR_UPLOAD_ROLE $AWS_ECR_PROFILE
121+ ecr_login_with_profile $ECR_REGION_US_WEST_2
141122
142- echo " Pushing to ECR..."
143- push_ecr_image $ECR_REGION_US_WEST_1
123+ echo " Pushing to ECR (us-west-2 only, images replicated to other regions)..."
144124push_ecr_image $ECR_REGION_US_WEST_2
145- push_ecr_image $ECR_REGION_US_EAST_1
0 commit comments