This repository has been archived by the owner on Mar 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·192 lines (147 loc) · 3.75 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/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
}
_error_check() {
RESULT=$?
if [ ${RESULT} != 0 ]; then
_error ${RESULT}
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
TAG_NAME="latest"
fi
fi
if [ ! -z "${TAG_POST}" ]; then
TAG_NAME="${TAG_NAME}-${TAG_POST}"
fi
}
_docker_push() {
echo "docker build ${DOCKER_BUILD_ARGS} -t ${IMAGE_URI}:${TAG_NAME} ${BUILD_PATH} -f ${DOCKERFILE}"
docker build ${DOCKER_BUILD_ARGS} -t ${IMAGE_URI}:${TAG_NAME} ${BUILD_PATH} -f ${DOCKERFILE}
_error_check
echo "docker push ${IMAGE_URI}:${TAG_NAME}"
docker push ${IMAGE_URI}:${TAG_NAME}
_error_check
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 "${BUILD_PATH}" ]; then
BUILD_PATH="."
fi
if [ -z "${DOCKERFILE}" ]; then
DOCKERFILE="Dockerfile"
fi
if [ -z "${IMAGE_URI}" ]; then
if [ -z "${REGISTRY}" ]; then
IMAGE_URI="${IMAGE_NAME:-${REPOSITORY}}"
elif [ "${REGISTRY}" == "docker.pkg.github.com" ]; then
IMAGE_URI="${REGISTRY}/${REPOSITORY}/${IMAGE_NAME:-${REPONAME}}"
else
IMAGE_URI="${REGISTRY}/${IMAGE_NAME:-${REPOSITORY}}"
fi
fi
_docker_tag
}
_docker() {
_docker_pre
echo "docker login ${REGISTRY} -u ${USERNAME}"
echo ${PASSWORD} | docker login ${REGISTRY} -u ${USERNAME} --password-stdin
_error_check
_docker_push
echo "docker logout"
docker logout
}
_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 "${BUILD_PATH}" ]; then
BUILD_PATH="."
fi
if [ -z "${IMAGE_NAME}" ]; then
IMAGE_NAME="${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
_error_check
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
echo ::set-output name=TAG_NAME::${TAG_NAME}