-
Notifications
You must be signed in to change notification settings - Fork 0
/
.gitlab-ci.yml
311 lines (294 loc) · 10.7 KB
/
.gitlab-ci.yml
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
image: alpine:latest
variables:
GIT_USER_EMAIL: [email protected]
GIT_USER_NAME: git-rev-label.ci
before_script:
- echo -e "Fetch job trace with \n\e[1;3m curl -H \"PRIVATE-TOKEN:xxxxx\" https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/jobs/$CI_JOB_ID/trace \e[0m"
- function DEBUG { >&2 "$@" || true; }
- set -o nounset -o pipefail
## ---
#rm -f /tmp/xtrace_file;
#XTRACE_COLOR="$(echo -e $'\e[32m')" NOCOLOR="$(echo -e $'\e[0m')"
- >-
mkfifo /tmp/xtrace_file;
trap "rm -v /tmp/xtrace_file" EXIT;
exec 7<>/tmp/xtrace_file;
{ while read -u7; do echo -e $'\e[32m'"$REPLY"$'\e[0m'; done; } &
export BASH_XTRACEFD=7;
.retry:
retry:
max: 2
when:
- runner_system_failure
- stuck_or_timeout_failure
build_rev_label:
stage: build
image: $CI_REGISTRY/$CI_PROJECT_PATH:latest
extends: .retry
except:
- tags
script:
# - apk add --no-cache --update bash git perl
# - wget 'https://gitlab.com/kyb/git-rev-label/raw/artifacts/master/git-rev-label' -qO /tmp/git-rev-label && bash /tmp/git-rev-label --install
- git checkout -B $CI_COMMIT_REF_NAME
- DEBUG git rev-label
- DEBUG echo CI_PIPELINE_IID=$CI_PIPELINE_IID;
## --- Checkout previous binary to allow script BUILD detect BUILD_ID from it and increment.
- >-
git fetch origin artifacts/$CI_COMMIT_REF_NAME &&
git checkout origin/artifacts/$CI_COMMIT_REF_NAME -- git-rev-label &&
git reset -- git-rev-label || true
- DEBUG git log -1 --format=%B origin/artifacts/$CI_COMMIT_REF_NAME --
- DEBUG git status
- bash -x ./BUILD ## ToDo change format for tags. See CI_COMMIT_TAG
- echo -e '\e[1;37m'"$(./git-rev-label --version)"'\e[0m'
- git log -1 --format=%B >commit_message
artifacts:
paths:
- "git-rev-label"
- "commit_message"
npm_publish:
stage: deploy
dependencies:
- build_rev_label
variables:
#GIT_DEPTH: 1
NPM_PUBLISH_ATTEMPTS: 3
only:
- master
- npm_publish
extends: .retry
script:
- apk add --no-cache --update npm bash
- DEBUG echo -e '\e[1;37m'"$(./git-rev-label --version)"'\e[0m'
- VERSION_NPM="$( ./git-rev-label --version-npm )"
- echo -e '\e[1;37m'"$VERSION_NPM"'\e[0m'
- sed -i 's#"version":\s*".*"#"version":"'"$VERSION_NPM"'"#' package.json
- DEBUG cat package.json
- echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" >~/.npmrc ## https://blog.npmjs.org/post/118393368555/deploying-with-npm-private-modules
- |-
echo NPM_PUBLISH_ATTEMPTS=$NPM_PUBLISH_ATTEMPTS
NPM_PUBLISH_ATTEMPTS=3
while test $NPM_PUBLISH_ATTEMPTS -gt 0 ; do
npm publish . && break ;
NPM_PUBLISH_ATTEMPTS=$( echo $NPM_PUBLISH_ATTEMPTS-1 | bc ) ;
sleep 4;
done;
test $NPM_PUBLISH_ATTEMPTS -eq 0 && exit 1;
## CHECK
- sleep 5
- npm install -g git-rev-label
- DEBUG ./git-rev-label --version
- DEBUG git-rev-label --version
- DEBUG which -a git-rev-label
- test "$(git-rev-label --version)" = "$(./git-rev-label --version)"
## --- STORE ARTIFACTS TO A DEDICATED BRANCH ---
store_artifacts:
stage: deploy
except:
- tags
dependencies:
- build_rev_label
only:
variables:
- $KI
variables:
GIT_STRATEGY: none
extends: .retry
script:
- apk add --no-cache --update bash git openssh-client perl
## --------- GET REV_LABEL ----------
- REV_LABEL=$(./git-rev-label --rev-label)
- echo -e $'\e[1;37m' "$REV_LABEL" $'\e[0m'
## --------- URL --------------
- echo "$KI" >/tmp/ki && chmod 400 /tmp/ki
- export GIT_SSH_COMMAND="ssh -i/tmp/ki -oStrictHostKeyChecking=no"
- SSH_URL=$(echo "$CI_REPOSITORY_URL" | perl -pe 's#https://(.+?):(.+?)@(.+?)/(.+?)\.git#git@\3:\4.git#' )
## ---------- CLONE, ADD and COMMIT ---------------
- >
git clone "$SSH_URL" --depth 1 --single-branch -b artifacts/$CI_COMMIT_REF_NAME ./artifacts || {
git init artifacts && git -C artifacts checkout --orphan artifacts/$CI_COMMIT_REF_NAME;
}
- echo >README.md.tmp "## git-rev-label $REV_LABEL"
- |-
(
xtrace_file=a;
mkfifo $xtrace_file;
trap "rm -v $xtrace_file" EXIT
exec 7<>$xtrace_file; ## Keep open
{ while read -u7; do echo -e $'\e[32m'"$REPLY"$'\e[0m'; done; } &
BASH_XTRACEFD=7;
set -x;
for f in git-rev-label ;do
F="$f.$REV_LABEL";
ln $f artifacts/$F || {
echo >&2 "Forced ln $f";
ln -f $f artifacts/$F;
};
ln -f artifacts/$F artifacts/$f; ## hard-link to latest version
git -C artifacts add $F $f;
echo >>README.md.tmp "[$F]($F) ";
done;
)
- echo -e >>README.md.tmp "\n### Changelog\n$(cat commit_message)\n\n---\n"
- cat artifacts/README.md >>README.md.tmp || true
- mv README.md.tmp artifacts/README.md
- cd artifacts
- git add README.md
- DEBUG git status
- git config --global user.email "$GIT_USER_EMAIL"
- git config --global user.name "$GIT_USER_NAME"
- git commit -m"$REV_LABEL
$(cat commit_message)
"
## ------------ PUSH --------------
- git push "$SSH_URL" HEAD:artifacts/$CI_COMMIT_REF_NAME
## ----------- EPILOG -----------
- echo -e "See artifacts at \n \e[1;37m$CI_PROJECT_URL/tree/artifacts/$CI_COMMIT_REF_NAME\e[0m"
## ToDo use git ls-remote. No fetch required. Much faster on a big repos! GIT_STRATEGY: none
## git ls-remote --heads "$CI_REPOSITORY_URL" artifacts/\* | cut -d\t -f2 | sed 's#refs/heads/##'
remove_stale_artifacts:
stage: deploy
except:
- tags
dependencies: #none
variables:
GIT_CHECKOUT: "false"
#ToDo GIT_STRATEGY: none ## use ls-remote
GIT_SUBMODULE_STRATEGY: none
extends: .retry
script:
- apk add --no-cache --update bash git openssh-client perl
- ln -sf $(mktemp -d) /temp
## --- FIND STALED ARTIFACTS BRANCHES ---
- git branch -r --list origin/artifacts/\* | grep -v HEAD | sed -e s#origin/##g -Ee s#^\\s+##g >/temp/ARTIFACTS_BRANCHES
- DEBUG cat /temp/ARTIFACTS_BRANCHES
- git branch -r --list origin/\* | grep -v -e HEAD -e artifacts/| sed -e s#origin/##g -Ee s#^\\s+#artifacts/#g >/temp/BRANCHES
- DEBUG cat /temp/BRANCHES
- fgrep -vf /temp/BRANCHES /temp/ARTIFACTS_BRANCHES >/temp/STALE_ARTIFACTS_BRANCHES || { echo "No stale artifacts. Nothing to remove."; return 0; }
- DEBUG cat /temp/STALE_ARTIFACTS_BRANCHES
- PUSH_SPEC=$(sed -e 's#\s+##g' -e 's#^#:#g' /temp/STALE_ARTIFACTS_BRANCHES)
- DEBUG echo "$PUSH_SPEC"
## --------- URL --------------
- echo "$KI" >/tmp/ki && chmod 400 /tmp/ki
- export GIT_SSH_COMMAND="ssh -i/tmp/ki -oStrictHostKeyChecking=no"
- SSH_URL=$(echo "$CI_REPOSITORY_URL" | perl -pe 's#https://(.+?):(.+?)@(.+?)/(.+?)\.git#git@\3:\4.git#' )
- git push "$SSH_URL" $PUSH_SPEC
retry: 1
releases_page:
stage: deploy
variables:
GIT_CHECKOUT: "false"
dependencies: #none
only:
- master
- CI
- releases_page
extends: .retry
script:
- apk add --no-cache curl jq bash git
## --------- GET REV_LABEL ----------
- DEBUG ./git-rev-label --rev-label
- REV_LABEL=$(./git-rev-label --rev-label)
- echo -e $'\e[1;37m'"$REV_LABEL"$'\e[0m'
- COMMIT_MSG="$(git log -1 "origin/$CI_COMMIT_REF_NAME" --format=%B)"
- COMMIT_MSG="${COMMIT_MSG//$'\n'/\\n}"
- DEBUG echo "$COMMIT_MSG"
- ESCAPED_COMMIT_MSG="$(echo "$COMMIT_MSG"|sed -E 's,([\"]),\\\1,g')"
- >
JSON_REQ='{
"name": "'"$REV_LABEL"'",
"tag_name": "'"$REV_LABEL"'",
"ref": "'"$CI_COMMIT_SHA"'",
"description": "'"$ESCAPED_COMMIT_MSG"'",
"assets": {
"links": [
{
"name": "Artifacts branch",
"url": "'"$CI_PROJECT_URL/tree/artifacts/$CI_COMMIT_REF_NAME"'"
},{
"name": "'"git-rev-label"'",
"url": "'"$CI_PROJECT_URL/raw/artifacts/$CI_COMMIT_REF_NAME/git-rev-label.$REV_LABEL"'"
}
]
}
}'
- echo "$JSON_REQ" | jq || { echo >&2 "Wrong JSON_REQ:\ $JSON_REQ"; false; } ## VALIDATE
- >-
curl --header "PRIVATE-TOKEN: $PRI_TOKEN"
"https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/releases"
--header 'Content-Type: application/json'
--data "$JSON_REQ"
--request POST
> /tmp/resp
- cat /tmp/resp | jq
- cat /tmp/resp | test "$(jq -r .name)" == "$REV_LABEL" ## Assert the result
homebrew:
stage: deploy
dependencies:
- build_rev_label
only:
- master
- homebrew
- CI
extends: .retry
script:
- apk add bash git openssh-client perl
## --------- GET REV_LABEL ----------
- REV_LABEL=$(./git-rev-label --rev-label)
- echo -e $'\e[1;37m' "$REV_LABEL" $'\e[0m'
## ---
- sed -i -E 's#^(\s*url) .*#\1 '"'$CI_PROJECT_URL/raw/artifacts/$CI_COMMIT_REF_NAME/git-rev-label'"'#g' homebrew/git-rev-label.rb
- sed -i -E 's#^(\s*sha256) .*#\1 '"'$(sha256sum ./git-rev-label | cut -f1 -d\ )'"'#g' homebrew/git-rev-label.rb
- sed -i -E 's#^(\s*version) .*#\1 '"'$(./git-rev-label --version-npm)'"'#g' homebrew/git-rev-label.rb
#- sed -i -E 's#^(\s*version) .*#\1 '"'$(./git-rev-label --rev-label)'"'#g' homebrew/git-rev-label.rb
- DEBUG cat homebrew/git-rev-label.rb
- DEBUG git diff --color=always -- homebrew/git-rev-label.rb
## ---
- git config --global user.email "$GIT_USER_EMAIL"
- git config --global user.name "$GIT_USER_NAME"
- chmod 400 $GITHUB_KI && export GIT_SSH_COMMAND="ssh -i$GITHUB_KI -oStrictHostKeyChecking=no"
- git clone --depth 1 [email protected]:ivakyb/homebrew-git-rev-label.git
- ln -f homebrew/git-rev-label.rb homebrew-git-rev-label/git-rev-label.rb
- cd homebrew-git-rev-label
- DEBUG git diff --color=always -- git-rev-label.rb
- git commit -am"$REV_LABEL"
- git push
## -- as subrepo --
#- git subrepo push homebrew
### --- as submodule ---
#- git -C homebrew push $SSH_URL HEAD:master
## ToDo open pull request to homebrew-core
## ---
## -- ToDo test with brew install git-rev-label
docker_image:
image: docker:stable
services:
- docker:dind
stage: deploy
dependencies:
- build_rev_label
only:
- master
- docker_image
variables:
GIT_DEPTH: 1
#extends: .retry
retry: 2
script:
- apk add bash
- VERSION_NPM="$(./git-rev-label --version-npm -x -D)"
- DEBUG echo "VERSION_NPM=$VERSION_NPM"
- docker info
- echo "$CI_REGISTRY_PASSWORD" | docker login registry.gitlab.com -u "$CI_REGISTRY_USER" --password-stdin
- echo "$DOCKER_HUB_P" | docker login -u "$DOCKER_HUB_U" --password-stdin
- docker pull $CI_REGISTRY/$CI_PROJECT_PATH:latest
- docker build --tag $CI_REGISTRY/$CI_PROJECT_PATH:latest .
- docker tag $CI_REGISTRY/$CI_PROJECT_PATH:latest ${CI_REGISTRY}/${CI_PROJECT_PATH}:$VERSION_NPM
- docker tag $CI_REGISTRY/$CI_PROJECT_PATH:latest ikyb/git-rev-label
- docker push $CI_REGISTRY/$CI_PROJECT_PATH:latest
- docker push $CI_REGISTRY/$CI_PROJECT_PATH:$VERSION_NPM
- docker push ikyb/git-rev-label
# after_script:
# - docker logout ${CI_REGISTRY}
# - docker logout