forked from docker-library/wordpress
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build-publish.sh
100 lines (68 loc) · 2.04 KB
/
build-publish.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
#!/bin/bash
set -e
usage="Usage: build-publish sudo docker_build_opt docker_build_credentials docker_build_name docker_build_location aws_credentials \n
sudo : to execute with sudo \n
docker_build_opt : all the docker options except the image name /tag \n
docker_build_credentials : name of the credentials defined in Jenkins, to build the docker image (if needed ex : git credentials). \n
docker_build_name : the image's name /tag \n
docker_build_location : the location of the Dockerfile \n
aws_credentials : name of the credentials defined in Jenkins, to login on AWS \n"
function help
{
echo -e $usage
exit 0
}
function error_exit
{
echo -e "$1 \n
$usage \n
$2 \n" 1>&2
exit 1
}
if [ ! -z $1 ]
then
if [ $1 != "sudo" ] && [ $1 != "help" ]
then
error_exit "The first argument must be \"sudo\" or empty."
else
if [ $1 == "help" ]
then
help
fi
fi
fi
if [ -z "$4" ]
then
error_exit "\"docker_build_name\" is mandatory."
fi
if [ -z "$5" ]
then
error_exit "\"docker_build_location\" is mandatory."
fi
if [ -z "$6" ]
then
error_exit "\"aws_credentials\" is mandatory."
fi
docker_build_cmd="$1 docker build $2 -t $4 $5"
eval docker_build_credentials=\$$3
if [ ! -z $docker_build_credentials ];
then
USERNAME=${docker_build_credentials%:*}
PASSWORD=${docker_build_credentials#*:}
REPLACE=USERNAME
docker_build_cmd=${docker_build_cmd/$REPLACE/$USERNAME}
REPLACE=PASSWORD
docker_build_cmd=${docker_build_cmd/$REPLACE/$PASSWORD}
fi
eval $docker_build_cmd
[ $? != 0 ] && \
error "Docker image build failed !" && exit 1
eval aws_credentials=\$$6
AWS_ACCESS_KEY_ID=${aws_credentials%:*}
AWS_SECRET_ACCESS_KEY=${aws_credentials#*:}
ecr_get_login="$1 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 get-login"
$1 `$ecr_get_login`
eval "$1 docker push $4"
eval "$1 docker rm $($1 docker ps -a -f 'status=exited' -q --no-trunc)" || true
eval "$1 docker rmi $($1 docker images --filter 'dangling=true' -q --no-trunc)" || true
exit 0