-
Notifications
You must be signed in to change notification settings - Fork 0
/
.gitlab-ci.yml
290 lines (265 loc) · 8.23 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
variables:
GO_VERSION: 1.12.6
ARTIFACT_PROJECT_DIR: artifacts
# GITLAB_HOST: repo.nefrosovet.ru
GITLAB_HOST: gitlab.com
ART_SUFFIX: maximus
ART_NAMESPACE: platform
APP_DIR: cmd/thesaurus
APP_BIN: thesaurus
IMAGE_NAME: thesaurus
CSV_PATH: dictionaries
stages:
- prebuild
- build
- pack
- test
# - push
# - cleanup
# - deploy
.common: &common
tags:
- docker
.version: &version
<<: *common
before_script:
- if [ "$CI_COMMIT_REF_NAME" == "master" ]; then export
APP_VER=latest.b${CI_JOB_ID}-${CI_COMMIT_SHA:0:8};
CI_REGISTRY_VER=latest;
ART_PREFIX=prod;
else export
APP_VER=${CI_COMMIT_REF_NAME}.b${CI_JOB_ID}-${CI_COMMIT_SHA:0:8};
CI_REGISTRY_VER=${CI_COMMIT_REF_NAME};
ART_PREFIX=develop; fi
- if [ -n "${CI_COMMIT_TAG}" ]; then export
APP_VER=${CI_COMMIT_TAG};
CI_REGISTRY_VER=${CI_COMMIT_TAG};
ART_PREFIX=prod; fi
.build_env: &build_env
<<: *version
image: golang:${GO_VERSION}
variables:
DOCKER_DRIVER: overlay2
services:
- docker:dind
# tags:
# - k8s
.docker:
extends: .build_env
image: docker:stable
cache:
policy: pull
.docker:build:
extends: .docker
script:
- echo ${CI_BUILD_TOKEN} | docker login -u ${CI_REGISTRY_USER} --password-stdin ${CI_REGISTRY}
- docker build --force-rm --pull --build-arg _path=${ARTIFACT_PROJECT_DIR} --build-arg APP=${BIN_NAME} --build-arg CSV=${CSV_PATH} --label image.version=${APP_VER} -f spec/Dockerfile -t ${CI_REGISTRY_IMAGE}:${CI_REGISTRY_VER} .
- docker push ${CI_REGISTRY_IMAGE}:${CI_REGISTRY_VER}
# - docker build --force-rm --pull --build-arg _path=${ARTIFACT_PROJECT_DIR} --build-arg APP=${BIN_NAME} --build-arg CSV=${CSV_PATH} --label image.version=${APP_VER} -f spec/Dockerfile -t ${CI_REGISTRY_IMAGE}:${CI_PIPELINE_ID} .
# - docker push ${CI_REGISTRY_IMAGE}:${CI_PIPELINE_ID}
.docker:check:
extends: .docker
dependencies: []
script:
- echo ${CI_BUILD_TOKEN} | docker login -u ${CI_REGISTRY_USER} --password-stdin ${CI_REGISTRY}
- docker pull ${CI_REGISTRY_IMAGE}:${CI_REGISTRY_VER}
- docker run --rm -i ${CI_REGISTRY_IMAGE}:${CI_REGISTRY_VER} --version
# - docker run --rm -i ${CI_REGISTRY_IMAGE}:${CI_PIPELINE_ID} --version
# - docker pull ${CI_REGISTRY_IMAGE}:${CI_PIPELINE_ID}
#.docker:push:id:
# extends: .docker
# dependencies: []
# script:
# - echo ${CI_BUILD_TOKEN} | docker login -u ${CI_REGISTRY_USER} --password-stdin ${CI_REGISTRY}
# - echo ${ART_PASSWORD} | docker login -u ${ART_USERNAME} --password-stdin ${ART_REGISTRY}
# - docker pull ${CI_REGISTRY_IMAGE}:${CI_PIPELINE_ID}
# - docker tag ${CI_REGISTRY_IMAGE}:${CI_PIPELINE_ID} ${ART_REGISTRY}/${ART_PREFIX}-docker-${ART_SUFFIX}/${ART_NAMESPACE}/${IMAGE_NAME}:${CI_PIPELINE_ID}
# - docker push ${ART_REGISTRY}/${ART_PREFIX}-docker-${ART_SUFFIX}/${ART_NAMESPACE}/${IMAGE_NAME}:${CI_PIPELINE_ID}
#.docker:push:version:
# extends: .docker
# dependencies: []
# script:
# - echo ${CI_BUILD_TOKEN} | docker login -u ${CI_REGISTRY_USER} --password-stdin ${CI_REGISTRY}
# - echo ${ART_PASSWORD} | docker login -u ${ART_USERNAME} --password-stdin ${ART_REGISTRY}
# - docker pull ${CI_REGISTRY_IMAGE}:${CI_PIPELINE_ID}
# - docker tag ${CI_REGISTRY_IMAGE}:${CI_PIPELINE_ID} ${ART_REGISTRY}/${ART_PREFIX}-docker-${ART_SUFFIX}/${ART_NAMESPACE}/${IMAGE_NAME}:${CI_REGISTRY_VER}
# - docker push ${ART_REGISTRY}/${ART_PREFIX}-docker-${ART_SUFFIX}/${ART_NAMESPACE}/${IMAGE_NAME}:${CI_REGISTRY_VER}
.build_app: &build_app
<<: *build_env
dependencies:
- prebuild
script:
- mkdir -p ${GOPATH}/src/${GITLAB_HOST}/${CI_PROJECT_NAMESPACE}
- cd ${GOPATH}/src/${GITLAB_HOST}/${CI_PROJECT_NAMESPACE}
- ln -s ${CI_PROJECT_DIR}
- cd ${CI_PROJECT_NAME}/${BUILD_PATH}
- CGO_ENABLED=0 GOOS=${BUILD_OS} go build -a -installsuffix cgo -ldflags "-w" -ldflags "-X main.version=${APP_VER}" -o ${GOPATH}/src/${GITLAB_HOST}/${CI_PROJECT_NAMESPACE}/${CI_PROJECT_NAME}/${ARTIFACT_PROJECT_DIR}/${BIN_NAME} .
artifacts:
name: ${CI_PROJECT_NAME}-${BIN_NAME}.b${CI_JOB_ID}-bin
paths:
- ${ARTIFACT_PROJECT_DIR}/${BIN_NAME}
- ${CSV_PATH}
expire_in: 1 week
#.deploy_bin: &deploy_bin
# <<: *build_env
# image: docker.bintray.io/jfrog/jfrog-cli-go
# script:
# - jfrog rt config
# --url=https://${ART_REGISTRY}/artifactory
# --user=${ART_USERNAME}
# --apikey=${ART_PASSWORD}
# rt-${CI_PIPELINE_ID}
# - cd ${ARTIFACT_PROJECT_DIR}
# - tar -czf ${CI_PROJECT_NAME}-${CI_REGISTRY_VER}-${APP_OS}.tar.gz ${BIN_NAME}
# - jfrog rt upload ${CI_PROJECT_NAME}-${CI_REGISTRY_VER}-${APP_OS}.tar.gz ${ART_PREFIX}-bin-${ART_SUFFIX}/${ART_NAMESPACE}/${CI_PROJECT_NAME}-${CI_REGISTRY_VER}-${APP_OS}.tar.gz
prebuild:
extends: .build_env
stage: prebuild
variables:
DEPCACHEDIR: ${CI_PROJECT_DIR}/_dep_cache
cache:
key: "${CI_PROJECT_NAME}-${CI_COMMIT_REF_NAME}"
paths:
- _dep_cache/
script:
- mkdir -p ${GOPATH}/src/${GITLAB_HOST}/${CI_PROJECT_NAMESPACE}
- cd ${GOPATH}/src/${GITLAB_HOST}/${CI_PROJECT_NAMESPACE}
- ln -s ${CI_PROJECT_DIR}
- cd ${CI_PROJECT_NAME}
- go get -u github.com/golang/dep/cmd/dep
- dep ensure
artifacts:
name: ${CI_PROJECT_NAME}.b${CI_JOB_ID}-vendor
paths:
- vendor/
build:linux:
stage: build
extends: .build_app
variables:
BUILD_OS: linux
BUILD_PATH: ${APP_DIR}
BIN_NAME: ${APP_BIN}
build:windows:
stage: build
extends: .build_app
variables:
BUILD_OS: windows
BUILD_PATH: ${APP_DIR}
BIN_NAME: ${APP_BIN}.exe
pack:docker:
stage: pack
extends: .docker:build
variables:
BIN_NAME: ${APP_BIN}
dependencies:
- build:linux
test:docker:
extends: .docker:check
stage: test
variables:
GIT_STRATEGY: none
#test:doc-syntax:
# <<: *common
# image:
# name: goswagger/swagger
# entrypoint: [""]
# stage: test
# dependencies: []
# cache:
# policy: pull
# script:
# - swagger validate docs/swagger.yaml
#test:postman:
# stage: test
# extends: .docker
# image: docker-compose
# dependencies: []
# variables:
# COMPOSE_PROJECT_NAME: ${CI_JOB_ID}
# before_script:
# - echo ${CI_BUILD_TOKEN} | docker login -u ${CI_REGISTRY_USER} --password-stdin ${CI_REGISTRY}
# - echo ${ART_PASSWORD} | docker login -u ${ART_USERNAME} --password-stdin ${ART_REGISTRY}
# script:
# - cd tests/postman
# - docker-compose pull app
# - docker-compose up -d app
# - sleep 5
# - docker-compose build --pull postman
# - docker-compose up --exit-code-from postman --abort-on-container-exit postman
# after_script:
# - cd tests/postman
# - docker-compose down -v
#test:go_test:
# stage: test
# extends: .build_env
# services:
# - name: docker:dind
# - name: mongo:4.0
# alias: mongo
# dependencies:
# - prebuild
# variables:
# THESAURUS_DB_HOST: mongo
# THESAURUS_DB_DATABASE: documents
# script:
# - mkdir -p ${GOPATH}/src/${GITLAB_HOST}/${CI_PROJECT_NAMESPACE}
# - cd ${GOPATH}/src/${GITLAB_HOST}/${CI_PROJECT_NAMESPACE}
# - ln -s ${CI_PROJECT_DIR}
# - cd ${CI_PROJECT_NAME}
# - go test ./...
#push:docker:id:
# stage: push
# extends: .docker:push:id
# variables:
# GIT_STRATEGY: none
# only:
# - branches
#push:docker:version:
# stage: push
# extends: .docker:push:version
# variables:
# GIT_STRATEGY: none
#pages:
# <<: *common
# image: swagger-ui
# stage: deploy
# dependencies: []
# cache:
# policy: pull
# script:
# - mv docs/swagger.yaml /var/www/${CI_COMMIT_SHA:0:8}.yaml
# - sed -i 's|'https://petstore.swagger.io/v2/swagger.json'|'${CI_COMMIT_SHA:0:8}.yaml'|g' /var/www/index.html
# - mv /var/www public
# artifacts:
# name: ${CI_PROJECT_NAME}-${CI_COMMIT_TAG}.b${CI_JOB_ID}-pages
# paths:
# - public
# only:
# - develop
#deploy:bin:linux:
# stage: deploy
# extends: .deploy_bin
# variables:
# APP_OS: linux64
# BIN_NAME: ${APP_BIN}
# dependencies:
# - build:linux
#deploy:bin:windows:
# stage: deploy
# extends: .deploy_bin
# variables:
# APP_OS: win64
# BIN_NAME: ${APP_BIN}.exe
# dependencies:
# - build:windows
#clean:temp_docker_image:
# stage: cleanup
# image: alpine
# dependencies: []
# cache:
# policy: pull
# before_script:
# - apk add --update --no-cache curl jq bash
# script:
# - for repoID in $(curl -s -f https://${GITLAB_HOST}/api/v4/projects/${CI_PROJECT_ID}/registry/repositories?private_token=${MAINTAINCE_TOKEN} | jq -r .[].id); do curl -s -f -X DELETE https://${GITLAB_HOST}/api/v4/projects/${CI_PROJECT_ID}/registry/repositories/${repoID}/tags/${CI_PIPELINE_ID}?private_token=${MAINTAINCE_TOKEN}; done
# <<: *common