generated from ita-social-projects/DevTemplate
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Jenkinsfile
308 lines (289 loc) · 13 KB
/
Jenkinsfile
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
def CODE_VERSION = ''
def IS_IMAGE_BUILDED = false
def IS_IMAGE_PUSH = false
def isSuccess
def preDeployFrontStage
def preDeployBackStage
def vers
def SEM_VERSION = ''
pipeline {
agent {
label 'stage'
}
environment {
GH_TOKEN = credentials('GH_TOKEN')
}
options {
skipDefaultCheckout true
disableConcurrentBuilds()
}
stages {
stage('Checkout') {
steps {
checkout([
$class: 'GitSCM',
branches: scm.branches,
extensions: scm.extensions + [[$class: 'LocalBranch'], [$class: 'WipeWorkspace']],
userRemoteConfigs: [[credentialsId: 'StreetcodeGithubCreds', url: '[email protected]:ita-social-projects/StreetCode.git']],
doGenerateSubmoduleConfigurations: scm.doGenerateSubmoduleConfigurations
])
}
}
stage('Print details') {
steps {
echo "BUILD_ID..............${env.BUILD_ID}"
echo "BUILD_NUMBER..........${env.BUILD_NUMBER}"
echo "BUILD_TAG.............${env.BUILD_TAG}"
echo "EXECUTOR_NUMBER.......${env.EXECUTOR_NUMBER}"
echo "JOB_NAME..............${env.JOB_NAME}"
echo "NODE_NAME.............${env.NODE_NAME}"
echo "WORKSPACE.............${env.WORKSPACE}"
}
}
stage('Setup dependencies') {
steps {
script {
sh 'dotnet tool update --global dotnet-coverage'
sh 'dotnet tool update --global dotnet-sonarscanner'
sh 'dotnet tool update --global GitVersion.Tool --version 5.12.0'
sh 'docker image prune --force --all --filter "until=72h"'
sh 'docker system prune --force --all --filter "until=72h"'
}
}
}
stage('Build') {
steps {
script {
sh './Streetcode/build.sh Run'
sh(script: 'dotnet gitversion > GITVERSION_PROPERTIES', returnStdout: true)
sh "cat GITVERSION_PROPERTIES"
sh(script: "dotnet gitversion | grep -oP '(?<=\"MajorMinorPatch\": \")[^\"]*' > version", returnStatus: true)
sh "cat version"
vers = readFile(file: 'version').trim()
sh "echo ${vers}"
env.CODE_VERSION = readFile(file: 'version').trim()
echo "${env.CODE_VERSION}"
SEM_VERSION="${env.CODE_VERSION}"
env.CODE_VERSION = "${env.CODE_VERSION}.${env.BUILD_NUMBER}"
echo "${env.CODE_VERSION}"
def gitCommit = sh(returnStdout: true, script: 'git log -1 --pretty=%B | cat').trim()
currentBuild.displayName = "${env.CODE_VERSION}-${BRANCH_NAME}:${gitCommit}"
}
}
}
stage('Setup environment') {
steps {
sh './Streetcode/build.sh SetupIntegrationTestsEnvironment'
}
}
stage('Run tests') {
steps {
parallel(
Unit_test: {
sh 'dotnet test ./Streetcode/Streetcode.XUnitTest/Streetcode.XUnitTest.csproj --configuration Release'
},
Integration_test: {
sh 'dotnet test ./Streetcode/Streetcode.XIntegrationTest/Streetcode.XIntegrationTest.csproj --configuration Release'
}
)
}
}
stage('Sonar scan') {
environment {
SONAR = credentials('sonar_token')
}
steps {
sh 'sudo apt install openjdk-17-jdk openjdk-17-jre -y'
sh ''' echo "Sonar scan"
dotnet sonarscanner begin /k:"ita-social-projects_StreetCode" /o:"ita-social-projects" /d:sonar.token=$SONAR /d:sonar.host.url="https://sonarcloud.io" /d:sonar.cs.vscoveragexml.reportsPaths="**/coverage.xml"
dotnet build ./Streetcode/Streetcode.sln --configuration Release
dotnet-coverage collect "dotnet test ./Streetcode/Streetcode.sln --configuration Release" -f xml -o "coverage.xml"
dotnet sonarscanner end /d:sonar.token=$SONAR
'''
}
}
stage('Build image') {
when {
branch pattern: "release/[0-9].[0-9].[0-9]", comparator: "REGEXP"
}
steps {
script {
withCredentials([usernamePassword(credentialsId: 'docker-login-streetcode', passwordVariable: 'password', usernameVariable: 'username')]){
sh "docker build -t ${username}/streetcode:latest ."
IS_IMAGE_BUILDED = true
}
}
}
}
stage('Push image') {
when {
expression { IS_IMAGE_BUILDED == true }
}
steps {
script {
withCredentials([usernamePassword(credentialsId: 'docker-login-streetcode', passwordVariable: 'password', usernameVariable: 'username')]){
sh 'echo "${password}" | docker login -u "${username}" --password-stdin'
sh "docker push ${username}/streetcode:latest"
sh "docker tag ${username}/streetcode:latest ${username}/streetcode:${env.CODE_VERSION}"
sh "docker push ${username}/streetcode:${env.CODE_VERSION}"
IS_IMAGE_PUSH = true
}
}
}
}
stage('Deploy Stage'){
when {
expression { IS_IMAGE_PUSH == true }
}
steps {
input message: 'Do you want to approve Staging deployment?', ok: 'Yes', submitter: 'admin_1, ira_zavushchak , dev'
script {
checkout scmGit(
branches: [[name: 'main']],
userRemoteConfigs: [[credentialsId: 'StreetcodeGithubCreds', url: '[email protected]:ita-social-projects/Streetcode-DevOps.git']])
preDeployBackStage = sh(script: 'docker container inspect $(docker container ls -aq) --format "{{.Config.Image}}" | grep "streetcodeua/streetcode:" | perl -pe \'($_)=/([0-9]+([.][0-9]+)+)/\'', returnStdout: true).trim()
echo "Last Tag Stage backend: ${preDeployBackStage}"
preDeployFrontStage = sh(script: 'docker container inspect $(docker container ls -aq) --format "{{.Config.Image}}" | grep "streetcodeua/streetcode_client:" | perl -pe \'($_)=/([0-9]+([.][0-9]+)+)/\'', returnStdout: true).trim()
echo "Last Tag Stage frontend: ${preDeployFrontStage}"
echo "DOCKER_TAG_BACKEND ${env.CODE_VERSION}"
echo "DOCKER_TAG_FRONTEND ${preDeployFrontStage}"
sh 'docker image prune --force --filter "until=72h"'
sh 'docker system prune --force --filter "until=72h"'
sh """ export DOCKER_TAG_BACKEND=${env.CODE_VERSION}
export DOCKER_TAG_FRONTEND=${preDeployFrontStage}
docker stop backend frontend nginx loki certbot
docker container prune -f
docker volume prune -f
docker network prune -f
sleep 10
docker compose --env-file /etc/environment up -d"""
}
}
}
stage('WHAT IS THE NEXT STEP') {
when {
expression { IS_IMAGE_PUSH == true }
}
steps {
script {
CHOICES = ["deployProd"];
env.yourChoice = input message: 'Do you want to deploy to Production?', ok : 'Proceed', submitter: 'admin_1, ira_zavushchak',id :'choice_id',
parameters: [choice(choices: CHOICES, description: 'Developer team can abort to switch next step as rollback stage', name: 'CHOICE')]
}
}
post {
aborted{
input message: 'Do you want to rollback Staging deployment?', ok: 'Yes', submitter: 'admin_1, ira_zavushchak , dev'
script{
echo "Rollback Tag Stage backend: ${preDeployBackStage}"
echo "Rollback Tag Stage frontend: ${preDeployFrontStage}"
sh 'docker image prune --force --filter "until=72h"'
sh 'docker system prune --force --filter "until=72h"'
sh """
export DOCKER_TAG_BACKEND=${preDeployBackStage}
export DOCKER_TAG_FRONTEND=${preDeployFrontStage}
docker stop backend frontend nginx loki certbot
docker container prune -f
docker volume prune -f
docker network prune -f
sleep 10
docker compose --env-file /etc/environment up -d"""
}
}
success {
script {
isSuccess = '1'
}
}
}
}
stage('Deploy prod') {
agent {
label 'production'
}
when {
expression { env.yourChoice == 'deployProd' }
}
steps {
script {
checkout scmGit(
branches: [[name: 'main']],
userRemoteConfigs: [[credentialsId: 'StreetcodeGithubCreds', url: 'https://github.com/ita-social-projects/Streetcode-DevOps.git']])
preDeployBackProd = sh(script: 'docker container inspect $(docker container ls -aq) --format "{{.Config.Image}}" | grep "streetcodeua/streetcode:" | perl -pe \'($_)=/([0-9]+([.][0-9]+)+)/\'', returnStdout: true).trim()
echo "Last Tag Prod backend: ${preDeployBackProd}"
preDeployFrontProd = sh(script: 'docker container inspect $(docker container ls -aq) --format "{{.Config.Image}}" | grep "streetcodeua/streetcode_client:" | perl -pe \'($_)=/([0-9]+([.][0-9]+)+)/\'', returnStdout: true).trim()
echo "Last Tag Prod frontend: ${preDeployFrontProd}"
sh 'docker image prune --force --filter "until=72h"'
sh 'docker system prune --force --filter "until=72h"'
sh """
export DOCKER_TAG_BACKEND=${env.CODE_VERSION}
export DOCKER_TAG_FRONTEND=${preDeployFrontProd}
docker stop backend frontend nginx loki certbot
docker container prune -f
docker volume prune -f
docker network prune -f
sleep 10
docker compose --env-file /etc/environment up -d"""
}
}
post {
success {
script {
isSuccess = '1'
}
}
}
}
stage('Sync after release') {
when {
expression { isSuccess == '1' }
}
steps {
script {
sh 'echo ${BRANCH_NAME}'
sh "git checkout master"
sh 'echo ${BRANCH_NAME}'
sh "git merge release/${env.SEM_VERSION}"
sh "git push origin main"
}
}
post {
success {
sh 'gh auth status'
sh "gh release create v${vers} --generate-notes --draft"
}
}
}
stage('Rollback Prod') {
agent {
label 'production'
}
when {
expression { env.yourChoice == 'deployProd' }
}
steps {
input message: 'Do you want to rollback Production deployment?', ok: 'Yes', submitter: 'admin_1, ira_zavushchak '
script {
echo "Rollback Tag Prod backend: ${preDeployBackProd}"
echo "Rollback Tag Prod frontend: ${preDeployFrontProd}"
sh 'docker image prune --force --filter "until=72h"'
sh 'docker system prune --force --filter "until=72h"'
sh """ export DOCKER_TAG_BACKEND=${preDeployBackProd}
export DOCKER_TAG_FRONTEND=${preDeployFrontProd}
docker stop backend frontend nginx loki certbot
docker container prune -f
docker volume prune -f
docker network prune -f
sleep 10
docker compose --env-file /etc/environment up -d"""
}
}
}
}
post {
always {
sh 'docker stop local_sql_server'
sh 'docker rm local_sql_server'
}
}
}