Skip to content

Commit 7f77156

Browse files
authored
Merge pull request #27 from Clever/aws-container-registry
INFRA-2314: Also Push to ECR
2 parents 7ba2d3f + ea54c5f commit 7f77156

File tree

1 file changed

+46
-15
lines changed

1 file changed

+46
-15
lines changed

circleci/docker-publish

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/bin/bash
22

3-
# Logs into Docker registry, then builds and pushes docker image.
4-
# Docker image is tagged with 7 character git commit SHA.
3+
# Logs into Docker registry (DockerHub and optionally ECR),
4+
# then builds and pushes docker image. Docker image is tagged
5+
# with 7 character git commit SHA.
56
#
67
# Usage:
78
#
@@ -10,27 +11,57 @@
1011

1112
set -e
1213

13-
# User supplied args
14-
DOCKER_USER=$1
15-
if [[ -z $DOCKER_USER ]]; then echo "Missing arg1 DOCKER_USER" && exit 1; fi
16-
DOCKER_PASS=$2
17-
if [[ -z $DOCKER_PASS ]]; then echo "Missing arg2 DOCKER_PASS" && exit 1; fi
18-
DOCKER_EMAIL=$3
19-
if [[ -z $DOCKER_EMAIL ]]; then echo "Missing arg3 DOCKER_EMAIL" && exit 1; fi
20-
ORG=$4
21-
if [[ -z $ORG ]]; then echo "Missing arg4 ORG" && exit 1; fi
14+
check_hub_vars() {
15+
# User supplied args
16+
DOCKER_USER=$1
17+
if [[ -z $DOCKER_USER ]]; then echo "Missing arg1 DOCKER_USER" && exit 1; fi
18+
DOCKER_PASS=$2
19+
if [[ -z $DOCKER_PASS ]]; then echo "Missing arg2 DOCKER_PASS" && exit 1; fi
20+
DOCKER_EMAIL=$3
21+
if [[ -z $DOCKER_EMAIL ]]; then echo "Missing arg3 DOCKER_EMAIL" && exit 1; fi
22+
ORG=$4
23+
if [[ -z $ORG ]]; then echo "Missing arg4 ORG" && exit 1; fi
24+
}
25+
26+
push_hub_image() {
27+
docker login -u $DOCKER_USER -p $DOCKER_PASS --email="$DOCKER_EMAIL"
28+
docker push $ORG/$REPO:$SHORT_SHA
29+
}
30+
31+
check_ecr_vars() {
32+
# ECR required env vars
33+
if [[ -z $ECR_ACCOUNT_ID ]]; then echo "Missing var for ECR: ECR_ACCOUNT_ID" && exit 1; fi
34+
if [[ -z $ECR_REGION ]]; then echo "Missing var for ECR: ECR_REGION" && exit 1; fi
35+
if [[ -z $ECR_PUSH_SECRET ]]; then echo "Missing var for ECR: ECR_PUSH_SECRET" && exit 1; fi
36+
}
37+
38+
push_ecr_image(){
39+
eval $(AWS_ACCESS_KEY_ID=$ECR_PUSH_ID AWS_SECRET_ACCESS_KEY=$ECR_PUSH_SECRET aws ecr --region $ECR_REGION get-login)
40+
docker tag $ORG/$REPO:$SHORT_SHA $ECR_ACCOUNT_ID.dkr.ecr.$ECR_REGION.amazonaws.com/$REPO:$SHORT_SHA
41+
docker push $ECR_ACCOUNT_ID.dkr.ecr.$ECR_REGION.amazonaws.com/$REPO:$SHORT_SHA
42+
}
2243

2344
# Set automatically by CircleCI
2445
: ${CIRCLE_PROJECT_REPONAME?"Missing required env var"}
2546
REPO=$CIRCLE_PROJECT_REPONAME
2647
: ${CIRCLE_SHA1?"Missing required env var"}
2748
SHORT_SHA=${CIRCLE_SHA1:0:7}
2849

29-
echo "Logging into DockerHub..."
30-
docker login -u $DOCKER_USER -p $DOCKER_PASS --email="$DOCKER_EMAIL"
50+
# Check CLI + env vars for DockerHub and (conditonally) ECR
51+
check_hub_vars
52+
if [ $ECR_PUSH_ID != "" ]; then
53+
check_ecr_vars
54+
fi
3155

3256
echo "Building docker image..."
3357
docker build -t $ORG/$REPO:$SHORT_SHA .
3458

35-
echo "Pushing docker image..."
36-
docker push $ORG/$REPO:$SHORT_SHA
59+
echo "Pushing to DockerHub..."
60+
push_hub_image
61+
62+
# TODO: fail if ECR_PUSH_ID is not set;
63+
# all repos eventually should dual publish (or explicitly opt-out)
64+
if [ $ECR_PUSH_ID != "" ]; then
65+
echo "ECR access_id available. Pushing to ECR..."
66+
push_ecr_image
67+
fi

0 commit comments

Comments
 (0)