-
Notifications
You must be signed in to change notification settings - Fork 0
/
push-image.sh
executable file
·68 lines (58 loc) · 1.77 KB
/
push-image.sh
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
set -xe
##
# ENV validation
#
if [ -z "$REPOSITORY_URL" ]; then
echo '$REPOSITORY_URL is a required ENV variable!'
exit 1
fi
if [ -z "$AWS_REGION" ]; then
echo '$AWS_REGION is a required ENV variable!'
exit 1
fi
if [ -z "$AWS_ACCOUNT_ID" ]; then
echo '$AWS_ACCOUNT_ID is a required ENV variable!'
exit 1
fi
if [ -z "$REPO_NAME" ]; then
echo '$REPO_NAME is a required ENV variable!'
exit 1
fi
if [ -z "$IMAGE_TAG" ]; then
echo '$IMAGE_TAG is a required ENV variable!'
exit 1
fi
echo "REPOSITORY_URL='$REPOSITORY_URL' AWS_REGION='$AWS_REGION' AWS_ACCOUNT_ID='$AWS_ACCOUNT_ID' REPO_NAME='$REPO_NAME' IMAGE_TAG='$IMAGE_TAG'"
##
# Variable creation
#
IMAGE_IDENTIFIER="$REPO_NAME:$IMAGE_TAG"
IMAGE="$REPOSITORY_URL:$IMAGE_TAG"
echo $IMAGE_IDENTIFIER
echo $IMAGE
##
# Log in to the AWS ECR registry
#
# https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ecr/get-login-password.html
#
aws ecr get-login-password \
--region $AWS_REGION \
| docker login \
--username AWS \
--password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com
##
# Re-tag the image to the identifier that will be pushed up to ECR
docker tag $IMAGE_IDENTIFIER $IMAGE
##
# Check the SHA of the local and remote images. Don't push if they are the same
#
LOCAL_SHA=$(docker images --no-trunc --quiet $IMAGE_IDENTIFIER | grep -oh 'sha256:[0-9,a-z]*')
REMOTE_SHA=$(aws ecr describe-images --repository-name $REPO_NAME --image-ids imageTag=$IMAGE_TAG --query 'imageDetails[0].imageDigest'| grep -oh 'sha256:[0-9,a-z]*' || echo 'image doesnt exist')
echo "LOCAL SHA: $LOCAL_SHA"
echo "REMOTE SHA: $REMOTE_SHA"
if [ "$LOCAL_SHA" != "$REMOTE_SHA" ]; then
docker push $IMAGE
sleep 60
else
echo 'LOCAL AND REMOTE SHA values are identical. Skipping docker push.'
fi