-
Notifications
You must be signed in to change notification settings - Fork 3
/
entrypoint.sh
executable file
·158 lines (123 loc) · 3.1 KB
/
entrypoint.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/sh
CMD="$1"
REPOSITORY=${GITHUB_REPOSITORY}
USERNAME=${USERNAME:-$GITHUB_ACTOR}
REPONAME=$(echo "${REPOSITORY}" | cut -d'/' -f2)
_error() {
echo -e "$1"
if [ "${LOOSE_ERROR}" == "true" ]; then
exit 0
else
exit 1
fi
}
_aws_pre() {
if [ -z "${AWS_ACCESS_KEY_ID}" ]; then
_error "AWS_ACCESS_KEY_ID is not set."
fi
if [ -z "${AWS_SECRET_ACCESS_KEY}" ]; then
_error "AWS_SECRET_ACCESS_KEY is not set."
fi
if [ -z "${AWS_REGION}" ]; then
AWS_REGION="us-east-1"
fi
}
_docker_tag() {
if [ -z "${TAG_NAME}" ]; then
if [ -f ./target/TAG_NAME ]; then
TAG_NAME=$(cat ./target/TAG_NAME | xargs)
elif [ -f ./target/VERSION ]; then
TAG_NAME=$(cat ./target/VERSION | xargs)
elif [ -f ./VERSION ]; then
TAG_NAME=$(cat ./VERSION | xargs)
fi
if [ -z "${TAG_NAME}" ]; then
_error "TAG_NAME is not set."
fi
fi
}
_docker_push() {
echo "docker build -t ${IMAGE_URI}:${TAG_NAME} ."
docker build -t ${IMAGE_URI}:${TAG_NAME} .
echo "docker push ${IMAGE_URI}:${TAG_NAME}"
docker push ${IMAGE_URI}:${TAG_NAME}
if [ "${LATEST}" == "true" ]; then
echo "docker tag ${IMAGE_URI}:latest"
docker tag ${IMAGE_URI}:${TAG_NAME} ${IMAGE_URI}:latest
echo "docker push ${IMAGE_URI}:latest"
docker push ${IMAGE_URI}:latest
fi
}
_docker_pre() {
if [ -z "${USERNAME}" ]; then
_error "USERNAME is not set."
fi
if [ -z "${PASSWORD}" ]; then
_error "PASSWORD is not set."
fi
if [ -z "${IMAGE_NAME}" ]; then
IMAGE_NAME="${GITHUB_REPOSITORY}"
fi
if [ -z "${IMAGE_URI}" ]; then
if [ -z "${RESISTRY}" ]; then
IMAGE_URI="${IMAGE_NAME}"
else
IMAGE_URI="${RESISTRY}/${IMAGE_NAME}"
fi
fi
_docker_tag
}
_docker() {
_docker_pre
echo "docker login ${REGISTRY} -u ${USERNAME}"
echo ${PASSWORD} | docker login ${REGISTRY} -u ${USERNAME} --password-stdin
_docker_push
}
_docker_ecr_pre() {
_aws_pre
if [ -z "${AWS_ACCOUNT_ID}" ]; then
AWS_ACCOUNT_ID="$(aws sts get-caller-identity | grep "Account" | cut -d'"' -f4)"
fi
if [ -z "${IMAGE_NAME}" ]; then
IMAGE_NAME="${GITHUB_REPOSITORY}"
fi
if [ -z "${IMAGE_URI}" ]; then
IMAGE_URI="${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/${IMAGE_NAME}"
fi
_docker_tag
if [ "${IMAGE_TAG_MUTABILITY}" != "IMMUTABLE" ]; then
IMAGE_TAG_MUTABILITY="MUTABLE"
fi
}
_docker_ecr() {
_docker_ecr_pre
# aws credentials
aws configure <<-EOF > /dev/null 2>&1
${AWS_ACCESS_KEY_ID}
${AWS_SECRET_ACCESS_KEY}
${AWS_REGION}
text
EOF
echo "aws ecr get-login --no-include-email"
aws ecr get-login --no-include-email | sh
COUNT=$(aws ecr describe-repositories | jq '.repositories[] | .repositoryName' | grep "\"${IMAGE_NAME}\"" | wc -l | xargs)
if [ "x${COUNT}" == "x0" ]; then
echo "aws ecr create-repository ${IMAGE_NAME}"
aws ecr create-repository --repository-name ${IMAGE_NAME} --image-tag-mutability ${IMAGE_TAG_MUTABILITY}
fi
_docker_push
}
if [ -z "${CMD}" ]; then
CMD="--docker"
fi
echo "[${CMD:2}] start..."
case "${CMD:2}" in
docker)
_docker
;;
ecr)
_docker_ecr
;;
*)
_error
esac