-
Notifications
You must be signed in to change notification settings - Fork 0
/
.gitlab-ci.yml
129 lines (118 loc) · 3.47 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
stages:
- test
- visualization
- build
- package
- deploy
variables:
IMAGE_NAME: mentoring-gitlab.gabia.com:5050/mentee/mentee_2023.01/team/g-market/gabia_b_shop_backend
.merge_rules:
rules:
- if: '$CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "main" || $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "develop"'
.develop_push_rules:
rules:
- if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_REF_NAME == "develop"'
.main_push_rules:
rules:
- if : '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_REF_NAME == "main"'
test:
rules:
- !reference [.merge_rules, rules]
- !reference [.develop_push_rules, rules]
variables:
GRADLE_OPTS: "-Dorg.gradle.daemon=false"
DOCKER_HOST: "tcp://docker:2375"
DOCKER_DRIVER: overlay2
DOCKER_TLS_CERTDIR: ""
image: openjdk:17-alpine
stage: test
tags:
- gitlab-runner
services:
- name: docker:dind
script:
- ./gradlew test
coverage: '/ - Instruction Coverage: ([0-9.]+)%/'
artifacts:
paths:
- build/reports/jacoco/test/jacoco*.xml
reports:
junit: build/test-results/test/**/TEST-*.xml
#https://docs.gitlab.com/ee/ci/testing/test_coverage_visualization.html
visualization:
rules:
- !reference [.merge_rules, rules]
- !reference [.develop_push_rules, rules]
stage: visualization
needs: ["test"]
image: registry.gitlab.com/haynes/jacoco2cobertura:1.0.7
script:
- python /opt/cover2cover.py build/reports/jacoco/test/jacoco*.xml $CI_PROJECT_DIR/src/main/java/ > build/cobertura.xml
artifacts:
reports:
coverage_report:
coverage_format: cobertura
path: build/cobertura.xml
build:
rules:
- !reference [.main_push_rules, rules]
image: openjdk:17-alpine
stage: build
script:
- ./gradlew clean
- ./gradlew build -x test
artifacts:
paths:
- build/libs/*.jar
expire_in: 10 min
package:
rules:
- !reference [.main_push_rules, rules ]
stage: package
needs: ["build"]
image: docker:latest
services:
- name: docker:dind
before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
script:
- docker pull $IMAGE_NAME:latest || true
- docker build --cache-from $IMAGE_NAME:latest --tag $IMAGE_NAME:$CI_COMMIT_SHA --tag $IMAGE_NAME:latest .
- docker push $IMAGE_NAME:$CI_COMMIT_SHA
- docker push $IMAGE_NAME:latest
after_script:
- docker logout
deploy:
rules:
- !reference [.main_push_rules, rules ]
stage: deploy
needs: ["package"]
tags:
- deployer
before_script:
## Activate ssh-agent
- eval $(ssh-agent -s)
- echo "$SSH_KEY" > id_rsa
- chmod 400 id_rsa
- ssh-add id_rsa
## ADD ssh known hosts
- echo "$SSH_KNOWN_HOSTS" > known_hosts
- mkdir -p ~/.ssh
- mv known_hosts ~/.ssh/known_hosts
## Change permission
- chmod 644 ~/.ssh/known_hosts
- chmod 700 ~/.ssh
- ssh-keygen -R localhost
## Move deploy file and .env file
- scp -o "StrictHostKeyChecking=no" $BLUE_DEPLOY_YAML root@localhost:~/deploy/blue-deploy.yml
- scp -o "StrictHostKeyChecking=no" $GREEN_DEPLOY_YAML root@localhost:~/deploy/green-deploy.yml
- scp -o "StrictHostKeyChecking=no" $DEPLOY_ENV root@localhost:~/deploy/.env
script:
- |
ssh -tt -o StrictHostKeyChecking=no root@localhost <<EOF >/dev/null
cd deploy \
&& docker pull $IMAGE_NAME:latest \
&& docker-compose -f blue-deploy.yml --env-file .env up -d \
&& docker-compose -f green-deploy.yml --env-file .env up -d \
&& exit
EOF