-
Notifications
You must be signed in to change notification settings - Fork 54
/
.gitlab-ci.yml
103 lines (95 loc) · 2.67 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
image: docker:stable
variables:
DOCKER_DRIVER: overlay2
IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
DOCKER_TLS_CERTDIR: ""
MYSQL_DATABASE: test
MYSQL_ROOT_PASSWORD: db_password
services:
- docker:dind
stages:
- migrations
- test
- build
- deploy
lint:
image: golangci/golangci-lint:v1.59.1
stage: test
before_script:
- cp .env.testing.gitlab-ci .env.testing
- go install -v github.com/swaggo/swag/cmd/swag@v1.8.10
- swag init -g cmd/main.go
script:
- golangci-lint run
allow_failure: true
migrations:
image: golang:1.21.4-alpine3.18
services:
- mysql:5.7
tags:
- dind
- docker
stage: migrations
before_script:
- apk add --no-cache --update git build-base openssh-client
- cp .env.testing.gitlab-ci .env
- go install -v github.com/swaggo/swag/cmd/swag@v1.8.10
- $GOPATH/bin/swag init -g cmd/main.go
script:
- go get -v ./...
- go run migrations/entry.go --env-path=./ --verbose
- go run migrations/entry.go --rollback --env-path=./ --verbose
test:
image: golang:1.20.1-alpine3.17
services:
- mysql:5.7
tags:
- dind
- docker
stage: test
before_script:
- apk add --no-cache --update git build-base openssh-client
- cp .env.testing.gitlab-ci .env.testing
- go install -v github.com/swaggo/swag/cmd/swag@v1.8.10
- swag init -g cmd/main.go
script:
- go get -v ./...
- go test -v ./...
build:
tags:
- dind
- docker
stage: build
before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
script:
- cp deploy/Dockerfile .
- docker build
--build-arg WEB_PRIVATE_KEY="$WEB_PRIVATE_KEY"
--build-arg GIT_DOMAIN=$CI_SERVER_HOST
-t $CI_REGISTRY_IMAGE:staging .
- docker push $CI_REGISTRY_IMAGE:staging
deploy staging:
image: golang:1.20.1-alpine3.17
environment:
name: staging
when: manual
tags:
- dind
- docker
stage: deploy
before_script:
- apk add --update openssh-client bash
- mkdir -p ~/.ssh
- echo "$STAGING_PRIVATE_KEY" | tr -d '\r' > ~/.ssh/id_rsa
- chmod 600 ~/.ssh/id_rsa
- eval "$(ssh-agent -s)"
- ssh-add ~/.ssh/id_rsa
- ssh-keyscan -p 221 -H $STAGING_IP >> ~/.ssh/known_hosts
script:
- ssh dev@${STAGING_IP} -p 221 "mkdir -p ${PROJECT_PATH}"
- scp -P 221 -r ${STAGING_ENV} dev@${STAGING_IP}:${PROJECT_PATH}/.env
- scp -P 221 -r ./deploy/compose.yml dev@${STAGING_IP}:${PROJECT_PATH}/compose.yml
- ssh dev@$STAGING_IP -p 221 "docker login -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD} ${CI_REGISTRY};
docker pull ${CI_REGISTRY}/${CI_PROJECT_PATH}:staging;
docker compose -f ${PROJECT_PATH}/compose.yml up -d"