-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.gitlab-ci.yml
231 lines (213 loc) · 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
## Documentation https://docs.gitlab.com/ee/ci/yaml/
image: registry.gitlab.com/kyb/byte_vector
stages:
- prepare
- build
- test
- deploy
variables:
GIT_STRATEGY: fetch ## https://docs.gitlab.com/ee/ci/yaml/README.html#git-strategy
GIT_SUBMODULE_STRATEGY: normal ## https://docs.gitlab.com/ee/ci/yaml/README.html#git-submodule-strategy
before_script:
- function DEBUG { >&2 "$@" || true; }
.runner_tags_docker:
tags:
- docker
# Use TLS https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#tls-enabled
docker_image:
stage: prepare
image: docker:19.03.12
services:
- docker:19.03.12-dind
only:
changes:
- Dockerfile
#- .gitlab-ci.yml
refs:
- master
- CI
- docker-build
script:
- set -x
- docker login -u $CI_REGISTRY_USER -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
- docker pull $CI_REGISTRY_IMAGE:latest || true
- docker build --cache-from $CI_REGISTRY_IMAGE:latest --tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA --tag $CI_REGISTRY_IMAGE:latest .
- docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
- docker push $CI_REGISTRY_IMAGE:latest
manual_docker_image:
extends: docker_image
when: manual
only:
changes:
- Dockerfile
- .gitlab-ci.yml
except:
- master
- tags
build_and_test:
stage: build
except:
- tags
extends: .runner_tags_docker
script:
## Be sure of current branch, not detached HEAD
- git checkout -B $CI_COMMIT_REF_NAME
## -------- BUILD --------
- cmake -B build -DBUILD_TESTING=ON
- cmake --build build -- -j #VERBOSE=1
#- cmake --build build --target byte_vector_amalgamate
## -------- TEST --------
- cmake --build build --target test #- ( cd build && ctest; )
- ./build/byte_vector-demo
## -------- PREPARE ARTIFACTS -------
- ln -f build/{rev_label,byte_vector.hh} -t.
- git log -1 --format=%B >commit_message
artifacts:
paths:
- "byte_vector.hh"
- "rev_label"
- "commit_message"
## --- STORE ARTIFACTS TO A DEDICATED BRANCH ---
store_artifacts:
stage: deploy
extends: .runner_tags_docker
only:
refs:
- master
- CI
variables:
- $KI
# except:
# - tags
dependencies:
- build_and_test
image: registry.gitlab.com/kyb/byte_vector #/git-ubuntu
variables:
GIT_STRATEGY: none
script:
## --------- GET REV_LABEL ----------
- REV_LABEL="$(cat rev_label)" #"$(sed -En 's|^#define\s+BUILD_GIT\s+\"(.*)\"|\1|p' build_info.h | tr / _)"
- 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.new "## byte_vector $REV_LABEL"
- ln byte_vector.hh artifacts/byte_vector-$REV_LABEL.hh
- echo >>README.md.new "[byte_vector-$REV_LABEL.hh](byte_vector-$REV_LABEL.hh) "
- echo >>README.md.new -e "\n## Changelog\n$(cat commit_message)\n\n---\n"
- cat >>README.md.new artifacts/README.md || true
- mv README.md.new artifacts/README.md
- cd artifacts
- ln -sf byte_vector-$REV_LABEL.hh byte_vector.hh
- git add .
- DEBUG git status
- git config --global user.email "ci@byte_vector.c++"
- git config --global user.name "byte_vector GitLab CI"
- git commit -m"$REV_LABEL
$(cat commit_message)
"
## ------------ PUSH --------------
- git push "$SSH_URL" HEAD:artifacts/$CI_COMMIT_REF_NAME #ToDo use $CI_REPOSITORY_URL
## ----------- EPILOG -----------
- echo "See artifacts at "$'\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!
.remove_stale_artifacts:
stage: deploy
extends: .runner_tags_docker
only:
refs:
- master
variables:
- $KI
except:
- tags
dependencies:
## No dependancies
image: alpine
variables:
GIT_CHECKOUT: "false"
GIT_SUBMODULE_STRATEGY: none
script:
- apk add --no-cache git perl openssh-client
- mkdir /temp && mount -t tmpfs -o size=500m tmpfs /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 || 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
releases_page:
stage: deploy
extends: .runner_tags_docker
image: alpine
variables:
GIT_CHECKOUT: "false"
GIT_STRATEGY: none
dependencies:
- build_and_test
#- store_artifacts
when: manual
only:
refs:
- master
- CI
variables:
- $PRI_TOKEN
script:
- apk add --no-cache curl jq
## --------- GET REV_LABEL ----------
- REV_LABEL="$(cat rev_label)"
- echo -e $'\e[1;37m' "$REV_LABEL" $'\e[0m'
- function name_url { for x; do echo $COMMA'{"name":"'"$x"'","url":"'"$CI_PROJECT_URL/raw/artifacts/$CI_COMMIT_REF_NAME/$x"'"}'; COMMA=, ;done; }
- description="$(cat commit_message)"
- description="$description\n\n### [byte_vector-$REV_LABEL.hh]($CI_PROJECT_URL/raw/artifacts/$CI_COMMIT_REF_NAME/byte_vector-$REV_LABEL.hh)"
- description="${description//$'\n'/\\n}"
- DEBUG echo "$description"
- >
JSON_REQ='{
"name": "'"$REV_LABEL"'",
"tag_name": "'"$REV_LABEL"'",
"ref": "'"$CI_COMMIT_SHA"'",
"description": "'"$description"'",
"assets": {
"links": [
{
"name": "Artifacts branch",
"url": "'"$CI_PROJECT_URL/tree/artifacts/$CI_COMMIT_REF_NAME"'"
},'"
$(name_url byte_vector-$REV_LABEL.hh)
]
}
}"
- echo "$JSON_REQ" | jq || { echo "$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
auto_releases_page:
extends: releases_page
when: on_success # oveeride manual
only:
refs:
- master
variables:
- $PUBLISH_TO_RELEASES_PAGE
except:
refs:
- tags