-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.gitlab-ci.yml
executable file
·179 lines (156 loc) · 5.64 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
stages:
- build
- test
- deploy
default:
image: docker:dind
before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_JOB_TOKEN $CI_REGISTRY
variables:
IMAGE_PATH: reg.cicd.tlow.ir/fbgerami/monorepo
VERSION: v1.0.6
vote-build-job:
stage: build
image: docker:dind
script:
- cat /root/.docker/config.json
- cd vote
- docker build -t ${IMAGE_PATH}/vote:$CI_COMMIT_SHORT_SHA .
- docker push ${IMAGE_PATH}/vote:$CI_COMMIT_SHORT_SHA
result-build-job:
stage: build
image: docker:dind
script:
- cd result
- docker build -t ${IMAGE_PATH}/result:$CI_COMMIT_SHORT_SHA .
- docker push ${IMAGE_PATH}/result:$CI_COMMIT_SHORT_SHA
worker-build-job:
stage: build
image: docker:dind
script:
- cd worker
- docker build -t ${IMAGE_PATH}/worker:$CI_COMMIT_SHORT_SHA .
- docker push ${IMAGE_PATH}/worker:$CI_COMMIT_SHORT_SHA
worker-test-image:
stage: test
needs: ["worker-build-job"]
script:
- docker pull ${IMAGE_PATH}/worker:$CI_COMMIT_SHORT_SHA
- echo "worker image test"
- docker tag ${IMAGE_PATH}/worker:$CI_COMMIT_SHORT_SHA ${IMAGE_PATH}/worker:${VERSION}
- docker push ${IMAGE_PATH}/worker:${VERSION}
result-test-image:
stage: test
needs: ["result-build-job"]
script:
- docker pull ${IMAGE_PATH}/result:$CI_COMMIT_SHORT_SHA
- echo "result image test"
- docker tag ${IMAGE_PATH}/result:$CI_COMMIT_SHORT_SHA ${IMAGE_PATH}/result:${VERSION}
- docker push ${IMAGE_PATH}/result:${VERSION}
vote-test-image:
stage: test
needs: ["vote-build-job"]
script:
- docker pull ${IMAGE_PATH}/vote:$CI_COMMIT_SHORT_SHA
- echo "vote image test"
- docker tag ${IMAGE_PATH}/vote:$CI_COMMIT_SHORT_SHA ${IMAGE_PATH}/vote:${VERSION}
- docker push ${IMAGE_PATH}/vote:${VERSION}
# voting-app-scan-image:
# stage: test
# image: aquasec/trivy
# needs: ["vote-test-image","result-test-image","worker-test-image"]
# variables:
# VOTE_IMAGE: ${IMAGE_PATH}/vote:${VERSION}
# RESULT_IMAGE: ${IMAGE_PATH}/result:${VERSION}
# WORKER_IMAGE: ${IMAGE_PATH}/worker:${VERSION}
# script:
# - trivy --exit-code 0 --cache-dir .trivycache/ --no-progress --format template --template "@/tmp/trivy-gitlab.tpl" -o gl-container-scanning-report.json $VOTE_IMAGE
# - trivy --exit-code 1 --cache-dir .trivycache/ --severity CRITICAL --no-progress $VOTE_IMAGE
# - trivy --exit-code 0 --cache-dir .trivycache/ --no-progress --format template --template "@/tmp/trivy-gitlab.tpl" -o gl-container-scanning-report.json $RESULT_IMAGE
# - trivy --exit-code 1 --cache-dir .trivycache/ --severity CRITICAL --no-progress $RESULT_IMAGE
# - trivy --exit-code 0 --cache-dir .trivycache/ --no-progress --format template --template "@/tmp/trivy-gitlab.tpl" -o gl-container-scanning-report.json $WORKER_IMAGE
# - trivy --exit-code 1 --cache-dir .trivycache/ --severity CRITICAL --no-progress $WORKER_IMAGE
# cache:
# paths:
# - /cache/.trivycache/
# artifacts:
# reports:
# container_scanning: gl-container-scanning-report.json
deploy-to-pre-product:
stage: deploy
variables:
DOMAIN: pre-product.tlow.ir
SERVER_NAME: pre-product.tlow.ir
HOSTNAME: pre-product
SSH_PORT: 8431
SSH_USER: root
SERVICE_PATH: /opt/services/vote-services
before_script:
- eval $(ssh-agent -s)
- chmod 400 ${SSH_PRIVATE_KEY}
- ssh-add ${SSH_PRIVATE_KEY}
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
script:
- |
# Create directory if not exist
ssh -o StrictHostKeyChecking=no -p ${SSH_PORT} ${SSH_USER}@${SERVER_NAME} "
[ -d ${SERVICE_PATH} ] || mkdir -p ${SERVICE_PATH}
"
# change env file
sed -i "s/FQDN/${DOMAIN}/g" .env
sed -i "s/SERVER_NAME/${HOSTNAME}/g" .env
sed -i "s/PIPELINE_ID/${VERSION}/g" .env
# move compose and env file to server
scp -o StrictHostKeyChecking=no -P${SSH_PORT} .env ${SSH_USER}@${SERVER_NAME}:${SERVICE_PATH}/
scp -o StrictHostKeyChecking=no -P${SSH_PORT} compose.yml ${SSH_USER}@${SERVER_NAME}:${SERVICE_PATH}/
# deploy service on server
ssh -o StrictHostKeyChecking=no -p ${SSH_PORT} ${SSH_USER}@${SERVER_NAME} "
docker login -u $CI_REGISTRY_USER -p $CI_JOB_TOKEN2 $CI_REGISTRY
cd ${SERVICE_PATH}
docker compose pull
docker compose up -d
"
environment:
name: pre-product
url: https://vote.$DOMAIN
deploy-to-production:
stage: deploy
variables:
DOMAIN: product.tlow.ir
SERVER_NAME: product.tlow.ir
HOSTNAME: product
SSH_PORT: 8431
SSH_USER: root
SERVICE_PATH: /opt/services/vote-services
before_script:
- eval $(ssh-agent -s)
- chmod 400 "$SSH_PRIVATE_KEY"
- ssh-add "$SSH_PRIVATE_KEY"
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
script:
- |
# Create directory if not exist
ssh -o StrictHostKeyChecking=no -p ${SSH_PORT} ${SSH_USER}@${SERVER_NAME} "
[ -d ${SERVICE_PATH} ] || mkdir -p ${SERVICE_PATH}
"
# change env file
sed -i "s/FQDN/${DOMAIN}/g" .env
sed -i "s/SERVER_NAME/${HOSTNAME}/g" .env
sed -i "s/PIPELINE_ID/${VERSION}/g" .env
# move compose and env file to server
scp -o StrictHostKeyChecking=no -P${SSH_PORT} .env ${SSH_USER}@${SERVER_NAME}:${SERVICE_PATH}/
scp -o StrictHostKeyChecking=no -P${SSH_PORT} compose.yml ${SSH_USER}@${SERVER_NAME}:${SERVICE_PATH}/
# deploy service on server
ssh -o StrictHostKeyChecking=no -p ${SSH_PORT} ${SSH_USER}@${SERVER_NAME} "
docker login -u $CI_REGISTRY_USER -p $CI_JOB_TOKEN2 $CI_REGISTRY
cd ${SERVICE_PATH}
docker compose pull
docker compose up -d
"
environment:
name: production
url: https://vote.$DOMAIN
when: manual
#