-
Notifications
You must be signed in to change notification settings - Fork 1
/
builder.sh
executable file
·370 lines (294 loc) · 8.93 KB
/
builder.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/bin/bash
OS_NAME="$(uname | awk '{print tolower($0)}')"
SHELL_DIR=$(dirname $0)
RUN_PATH="."
CMD=${1:-$CIRCLE_JOB}
PARAM=${2}
USERNAME=${CIRCLE_PROJECT_USERNAME}
REPONAME=${CIRCLE_PROJECT_REPONAME}
BRANCH=${CIRCLE_BRANCH:-main}
DOCKER_USER=${DOCKER_USER:-$USERNAME}
DOCKER_PASS=${DOCKER_PASS}
CIRCLE_BUILDER=${CIRCLE_BUILDER}
GITHUB_TOKEN=${GITHUB_TOKEN}
PUBLISH_PATH=${PUBLISH_PATH}
SLACK_TOKEN=${SLACK_TOKEN}
################################################################################
# command -v tput > /dev/null && TPUT=true
TPUT=
_echo() {
if [ "${TPUT}" != "" ] && [ "$2" != "" ]; then
echo -e "$(tput setaf $2)$1$(tput sgr0)"
else
echo -e "$1"
fi
}
_result() {
echo
_echo "# $@" 4
}
_command() {
echo
_echo "$ $@" 3
}
_success() {
echo
_echo "+ $@" 2
exit 0
}
_error() {
echo
_echo "- $@" 1
exit 1
}
_replace() {
if [ "${OS_NAME}" == "darwin" ]; then
sed -i "" -e "$1" $2
else
sed -i -e "$1" $2
fi
}
_prepare() {
# target
mkdir -p ${RUN_PATH}/target/publish
mkdir -p ${RUN_PATH}/target/release
if [ -f ${RUN_PATH}/target/circleci-stop ]; then
_success "circleci-stop"
fi
}
_build() {
if [ ! -f ${RUN_PATH}/VERSION ]; then
_error "not found VERSION"
fi
_result "BRANCH=${BRANCH}"
_result "PR_URL=${CIRCLE_PULL_REQUEST}"
# release version
MAJOR=$(cat ${RUN_PATH}/VERSION | xargs | cut -d'.' -f1)
MINOR=$(cat ${RUN_PATH}/VERSION | xargs | cut -d'.' -f2)
PATCH=$(cat ${RUN_PATH}/VERSION | xargs | cut -d'.' -f3)
if [ "${PATCH}" != "x" ]; then
VERSION="${MAJOR}.${MINOR}.${PATCH}"
printf "${VERSION}" > ${RUN_PATH}/target/VERSION
else
# latest versions
GITHUB="https://api.github.com/repos/${USERNAME}/${REPONAME}/releases"
VERSION=$(curl -s ${GITHUB} | grep "tag_name" | grep "${MAJOR}.${MINOR}." | head -1 | cut -d'"' -f4 | cut -d'-' -f1)
if [ -z ${VERSION} ]; then
VERSION="${MAJOR}.${MINOR}.0"
fi
_result "VERSION=${VERSION}"
# new version
if [ "${BRANCH}" == "main" ] || [ "${BRANCH}" == "master" ]; then
VERSION=$(echo ${VERSION} | awk -F. '{$NF = $NF + 1;} 1' | sed 's/ /./g')
else
if [ "${CIRCLE_PULL_REQUEST}" != "" ]; then
PR_NUM=$(echo "${CIRCLE_PULL_REQUEST}" | cut -d'/' -f7)
fi
if [ "${PR_NUM}" != "" ]; then
VERSION="${VERSION}-${PR_NUM}"
echo ${PR_NUM} > ${RUN_PATH}/target/PR
else
VERSION=""
fi
fi
if [ "${VERSION}" != "" ]; then
printf "${VERSION}" > ${RUN_PATH}/target/VERSION
fi
fi
_result "VERSION=${VERSION}"
}
_publish() {
if [ "${BRANCH}" != "main" ] && [ "${BRANCH}" != "master" ]; then
return
fi
if [ ! -f ${RUN_PATH}/target/VERSION ]; then
_result "not found target/VERSION"
return
fi
if [ -f ${RUN_PATH}/target/PR ]; then
_result "found target/PR"
return
fi
if [ -z ${PUBLISH_PATH} ]; then
_result "not found PUBLISH_PATH"
return
fi
BUCKET="$(echo "${PUBLISH_PATH}" | cut -d'/' -f1)"
# aws s3 sync
_command "aws s3 sync ${RUN_PATH}/target/publish/ s3://${PUBLISH_PATH}/ --acl public-read"
aws s3 sync ${RUN_PATH}/target/publish/ s3://${PUBLISH_PATH}/ --acl public-read
# aws cf reset
CFID=$(aws cloudfront list-distributions --query "DistributionList.Items[].{Id:Id,Origin:Origins.Items[0].DomainName}[?contains(Origin,'${BUCKET}')] | [0]" | grep 'Id' | cut -d'"' -f4)
if [ "${CFID}" != "" ]; then
aws cloudfront create-invalidation --distribution-id ${CFID} --paths "/*"
fi
}
_release() {
if [ ! -f ${RUN_PATH}/target/VERSION ]; then
_result "not found target/VERSION"
return
fi
if [ -z ${GITHUB_TOKEN} ]; then
_result "not found GITHUB_TOKEN"
return
fi
VERSION=$(cat ${RUN_PATH}/target/VERSION | xargs)
_result "VERSION=${VERSION}"
# printf "${VERSION}" > ${RUN_PATH}/target/release/${VERSION}
# if [ -f ${RUN_PATH}/target/PR ]; then
# GHR_PARAM="-delete -prerelease"
# else
# GHR_PARAM="-delete"
# fi
# _command "go get github.com/tcnksm/ghr"
# go get github.com/tcnksm/ghr
# # github release
# _command "ghr ${VERSION} ${RUN_PATH}/target/release/"
# ghr -t ${GITHUB_TOKEN:-EMPTY} \
# -u ${USERNAME} \
# -r ${REPONAME} \
# -c ${CIRCLE_SHA1} \
# ${GHR_PARAM} \
# ${VERSION} ${RUN_PATH}/target/release/
_command "hub release create ${VERSION}"
hub release create ${VERSION}
}
_docker() {
if [ ! -f ${RUN_PATH}/target/VERSION ]; then
_result "not found target/VERSION"
return
fi
if [ -z ${DOCKER_PASS} ]; then
_result "not found DOCKER_USER"
return
fi
VERSION=$(cat ${RUN_PATH}/target/VERSION | xargs)
_result "VERSION=${VERSION}"
_command "docker login -u $DOCKER_USER"
docker login -u $DOCKER_USER -p $DOCKER_PASS
_command "docker build -t ${USERNAME}/${REPONAME}:${VERSION} ."
docker build -f ${PARAM:-Dockerfile} -t ${USERNAME}/${REPONAME}:${VERSION} .
_command "docker push ${USERNAME}/${REPONAME}:${VERSION}"
docker push ${USERNAME}/${REPONAME}:${VERSION}
# if [ "${PARAM}" == "latest" ]; then
# _command "sudo docker tag ${USERNAME}/${REPONAME}:${VERSION} ${USERNAME}/${REPONAME}:latest"
# sudo docker tag ${USERNAME}/${REPONAME}:${VERSION} ${USERNAME}/${REPONAME}:latest
# _command "docker push ${USERNAME}/${REPONAME}:latest"
# docker push ${USERNAME}/${REPONAME}:latest
# fi
_command "docker logout"
docker logout
}
_buildx() {
if [ ! -f ${RUN_PATH}/target/VERSION ]; then
_result "not found target/VERSION"
return
fi
if [ -z ${DOCKER_PASS} ]; then
_result "not found DOCKER_USER"
return
fi
VERSION=$(cat ${RUN_PATH}/target/VERSION | xargs)
_result "VERSION=${VERSION}"
if [ ! -f ~/.docker/cli-plugins/docker-buildx ]; then
mkdir -p ~/.docker/cli-plugins
url="https://github.com/docker/buildx/releases/download/v0.5.1/buildx-v0.5.1.linux-amd64"
curl -sSL -o ~/.docker/cli-plugins/docker-buildx ${url}
chmod a+x ~/.docker/cli-plugins/docker-buildx
fi
_command "docker buildx version"
docker buildx version
_command "docker login -u $DOCKER_USER"
docker login -u $DOCKER_USER -p $DOCKER_PASS
_command "docker buildx build -t ${USERNAME}/${REPONAME}:${VERSION} ."
docker buildx build --push -f ${PARAM:-Dockerfile} \
--platform linux/arm/v7,linux/arm64/v8,linux/amd64 \
-t ${USERNAME}/${REPONAME}:${VERSION} .
# _command "docker push ${USERNAME}/${REPONAME}:${VERSION}"
# docker push ${USERNAME}/${REPONAME}:${VERSION}
# if [ "${PARAM}" == "latest" ]; then
# _command "sudo docker tag ${USERNAME}/${REPONAME}:${VERSION} ${USERNAME}/${REPONAME}:latest"
# sudo docker tag ${USERNAME}/${REPONAME}:${VERSION} ${USERNAME}/${REPONAME}:latest
# _command "docker push ${USERNAME}/${REPONAME}:latest"
# docker push ${USERNAME}/${REPONAME}:latest
# fi
_command "docker logout"
docker logout
}
_trigger() {
if [ ! -f ${RUN_PATH}/target/VERSION ]; then
_result "not found target/VERSION"
return
fi
if [ -z ${CIRCLE_BUILDER} ]; then
_result "not found CIRCLE_BUILDER"
return
fi
if [ -z ${PERSONAL_TOKEN:-$CIRCLE_TOKEN} ]; then
_result "not found PERSONAL_TOKEN"
return
fi
VERSION=$(cat ${RUN_PATH}/target/VERSION | xargs)
_result "VERSION=${VERSION}"
PERSONAL_TOKEN=${PERSONAL_TOKEN:-$CIRCLE_TOKEN}
# CIRCLE_API="https://circleci.com/api/v1.1/project/github/${CIRCLE_BUILDER}"
# CIRCLE_URL="${CIRCLE_API}?circle-token=${PERSONAL_TOKEN}"
# https://circleci.com/docs/api/v2/#get-a-pipeline-39-s-workflows
CIRCLE_API="https://circleci.com/api/v2/project/gh/${CIRCLE_BUILDER}/pipeline"
CIRCLE_URL="${CIRCLE_API}?circle-token=${PERSONAL_TOKEN}"
# build_parameters
PAYLOAD="{\"parameters\":{"
PAYLOAD="${PAYLOAD}\"username\":\"${TG_USERNAME:-${USERNAME}}\","
PAYLOAD="${PAYLOAD}\"project\":\"${TG_PROJECT:-${REPONAME}}\","
PAYLOAD="${PAYLOAD}\"version\":\"${TG_VERSION:-${VERSION}}\""
PAYLOAD="${PAYLOAD}}}"
_result "PAYLOAD=${PAYLOAD}"
curl -X POST \
-u ${PERSONAL_TOKEN}: \
-H "Content-Type: application/json" \
-d "${PAYLOAD}" "${CIRCLE_API}"
}
_slack() {
if [ ! -f ${RUN_PATH}/target/VERSION ]; then
_result "not found target/VERSION"
return
fi
if [ -z ${SLACK_TOKEN} ]; then
_result "not found SLACK_TOKEN"
return
fi
VERSION=$(cat ${RUN_PATH}/target/VERSION | xargs)
_result "VERSION=${VERSION}"
# send slack
curl -sL opspresso.github.io/tools/slack.sh | bash -s -- \
--token="${SLACK_TOKEN}" --username="${USERNAME}" \
--footer="<https://github.com/${USERNAME}/${REPONAME}/releases/tag/${VERSION}|${USERNAME}/${REPONAME}>" \
--footer_icon="https://opspresso.github.io/tools/favicon/github.png" \
--color="good" --title="${REPONAME}" "\`${VERSION}\`"
}
################################################################################
_prepare
case ${CMD} in
build|package)
_build
;;
publish)
_publish
;;
release)
_release
;;
docker)
_docker
;;
buildx)
_buildx
;;
trigger)
_trigger
;;
slack)
_slack
;;
esac
_success