-
Notifications
You must be signed in to change notification settings - Fork 0
/
push-image-ecr.sh
105 lines (79 loc) · 2.06 KB
/
push-image-ecr.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
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
#!/bin/bash
set -e
usage="Usage: push-image-ecr -s -a aws_credentials -n image_name -e ecr_url -t custom_tag\n
-s : to execute docker build with sudo \n
-a aws_credentials (mandatory) : aws credentials to use awscli \n
-n image_name (mandatory) : the image's name to tag and push \n
-e ecr_url (mandatory) : url ecr \n
-t custom_tag (optional) : user tag to add on ECR \n
-d : no date tag will be added"
function help
{
echo -e $usage
exit 0
}
function error_exit
{
echo -e "$1 \n
$usage \n
$2 \n" 1>&2
exit 1
}
sudo=""
image_name=""
while getopts 'sda:n:e:t:' opt; do
case $opt in
a) aws_credentials_arg="$OPTARG" ;;
s) sudo="sudo" ;;
n) image_name="$OPTARG" ;;
e) ecr_url="$OPTARG" ;;
t) custom_tag="$OPTARG" ;;
d) nodate="nodate" ;;
*) exit 1 ;;
esac
done
if [ -z $image_name ]
then
error_exit "\"image_name\" is mandatory."
fi
if [ -z $aws_credentials_arg ]
then
error_exit "\"aws_credentials\" is mandatory."
fi
if [ -z $ecr_url ]
then
error_exit "\"ecr_url\" is mandatory."
fi
base_tag="$ecr_url/$image_name"
latest_tag="$base_tag:latest"
date_tag="$base_tag:` date +%d%m%Y%H%M`"
eval "$sudo docker tag $image_name:latest $latest_tag"
if [ -z "$nodate" ]
then
eval "$sudo docker tag $image_name:latest $date_tag"
fi
if [ ! -z "$custom_tag" ]
then
custom_tag="$base_tag:$custom_tag"
eval "$sudo docker tag $image_name:latest $custom_tag"
fi
eval aws_credentials=\$$aws_credentials_arg
AWS_ACCESS_KEY_ID=${aws_credentials%:*}
AWS_SECRET_ACCESS_KEY=${aws_credentials#*:}
ecr_cmd="$sudo docker run --rm -e AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY -e AWS_DEFAULT_REGION=eu-west-1 anigeo/awscli ecr"
ecr_get_login="$ecr_cmd get-login"
docker_getlogin=$($ecr_get_login)
exp='-e none'
blank=''
docker_getlogin="${docker_getlogin/$exp/$blank}"
eval "$sudo $docker_getlogin"
eval "$sudo docker push $latest_tag"
if [ -z "$nodate" ]
then
eval "$sudo docker push $date_tag"
fi
if [ ! -z "$custom_tag" ]
then
eval "$sudo docker push $custom_tag"
fi
exit 0