-
Notifications
You must be signed in to change notification settings - Fork 12
/
Jenkinsfile.test.conf
226 lines (211 loc) · 9.47 KB
/
Jenkinsfile.test.conf
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
pipeline {
environment {
branchname = env.BRANCH_NAME.toLowerCase()
kubeconfig = getKubeconf(env.branchname)
registryCredential = 'jenkins_registry'
namespace = "${env.branchname == 'release' ? 'novosgp-conf' : 'sme-novosgp' }"
}
agent {
node { label 'sme' }
}
options {
timestamps ()
buildDiscarder(logRotator(numToKeepStr: '10', artifactNumToKeepStr: '5'))
disableConcurrentBuilds()
skipDefaultCheckout()
office365ConnectorWebhooks([
[name: "Office 365", url: "${URL_WEBHOOK_SGP}", notifyBackToNormal: false, notifyFailure: true, notifyRepeatedFailure: false, notifySuccess: true, notifyAborted: true]
])
}
stages {
stage('Checkout') {
agent { label "sme" }
steps {
script {
def nodes = nodesByLabel label: 'jenkinsnodes'
nodes = nodes.sort()
Map tasks = [:]
for (int i = 0; i < nodes.size(); i++) {
def label = nodes[i]
def stageName = "Checkout ${nodes[i]}"
tasks[label] = {
node(label) {
stage(stageName) {
checkout scm
}
}
}
}
timeout(time: 3, unit: 'MINUTES') {
parallel(tasks)
}
}
}
}
stage('Build do Projeto'){
agent { kubernetes {
label 'dotnet5-sonar'
defaultContainer 'dotnet5-sonar'
}
}
steps{
checkout scm
script{
def workspacePath = env.WORKSPACE
def folder = workspacePath.tokenize('/').last()
def sourceDir = "/home/jenkins/agent/temp/${folder}"
sh "cp -r ${sourceDir}/* ${env.WORKSPACE}"
sh "pwd"
sh "hostname"
withSonarQubeEnv('sonarqube-local'){
sh 'dotnet-sonarscanner begin /k:"SME-NovoSGP" /d:sonar.cs.opencover.reportsPaths="**/*coverage.opencover.xml"'
sh 'dotnet build SME.SGP.sln'
//stash includes: "**/*" , name: "bintestes"
sh "tar cf - . | pigz > bintestes.tar.gz"
stash includes: "bintestes.tar.gz" , name: "bintestes"
sh "ls -ltra"
sh 'dotnet-sonarscanner end'
}
}
}
}
stage('Build') {
parallel {
stage('sme-sgp-backend') {
agent { kubernetes {
label 'builder'
defaultContainer 'builder'
}
}
steps{
checkout scm
script {
def workspacePath = env.WORKSPACE
def folder = workspacePath.tokenize('/').last()
def sourceDir = "/home/jenkins/agent/temp/${folder}"
sh "cp -r ${sourceDir}/* ${env.WORKSPACE}"
sh "ls -ltra"
sh "pwd"
sh "hostname"
imagename = "registry.sme.prefeitura.sp.gov.br/${env.branchname}/sme-sgp-backend"
dockerImage1 = docker.build(imagename, "-f src/SME.SGP.Api/Dockerfile .")
docker.withRegistry( 'https://registry.sme.prefeitura.sp.gov.br', registryCredential ) {
dockerImage1.push() }
}
}
}
stage('sme-worker-geral') {
agent { kubernetes {
label 'builder'
defaultContainer 'builder'
}
}
steps{
script {
def workspacePath = env.WORKSPACE
def folder = workspacePath.tokenize('/').last()
def sourceDir = "/home/jenkins/agent/temp/${folder}"
sh "cp -r ${sourceDir}/* ${env.WORKSPACE}"
sh "ls -ltra"
sh "pwd"
sh "hostname"
imagename = "registry.sme.prefeitura.sp.gov.br/${env.branchname}/sme-worker-geral"
dockerImage2 = docker.build(imagename, "-f src/SME.SGP.Worker.Rabbbit/Dockerfile .")
docker.withRegistry( 'https://registry.sme.prefeitura.sp.gov.br', registryCredential ) {
dockerImage2.push() }
}
}
}
stage('sme-worker-webclient') {
agent { kubernetes {
label 'builder'
defaultContainer 'builder'
}
}
steps{
checkout scm
script {
imagename = "registry.sme.prefeitura.sp.gov.br/${env.branchname}/sme-worker-webclient"
dockerImage3 = docker.build(imagename, "-f src/SME.SGP.WebClient/Dockerfile .")
docker.withRegistry( 'https://registry.sme.prefeitura.sp.gov.br', registryCredential ) {
dockerImage3.push() }
}
}
}
}
}
stage('Deploy'){
agent { kubernetes {
label 'builder'
defaultContainer 'builder'
}
}
when { anyOf { branch 'master'; branch 'main'; branch 'development'; branch 'release'; branch 'release-r2'; branch 'pre-prod'; } }
steps {
script{
//if(testPassed){
if ( env.branchname == 'main' || env.branchname == 'master' ) {
withCredentials([string(credentialsId: 'aprovadores-sgp', variable: 'aprovadores')]) {
timeout(time: 24, unit: "HOURS") {
input message: 'Deseja realizar o deploy?', ok: 'SIM', submitter: "${aprovadores}"
}
}
}
withCredentials([file(credentialsId: "${kubeconfig}", variable: 'config')]){
sh('rm -f '+"$home"+'/.kube/config')
sh('cp $config '+"$home"+'/.kube/config')
sh "kubectl rollout restart deployment/worker-geral-test-conf -n ${namespace}"
sh "kubectl rollout restart deployment/webclient-test-conf -n ${namespace}"
sh "kubectl rollout restart deployment/api-sgp-test-conf -n ${namespace}"
sh('rm -f '+"$home"+'/.kube/config')
}
//}
}
}
}
stage('Flyway') {
agent { kubernetes {
label 'flyway'
defaultContainer 'flyway'
}
}
when { anyOf { branch 'master'; branch 'main'; branch 'development'; branch 'release'; branch 'release-r2'; branch 'pre-prod'; } }
steps{
withCredentials([string(credentialsId: "flyway_sgp_${branchname}", variable: 'url')]) {
checkout scm
sh 'pwd'
sh 'ls'
sh 'flyway -url=$url -locations="filesystem:scripts" -outOfOrder=true migrate'
}
}
}
}
post {
success { sendTelegram("🚀 Job Name: ${JOB_NAME} \nBuild: ${BUILD_DISPLAY_NAME} \nStatus: Success \nLog: \n${env.BUILD_URL}console") }
unstable { sendTelegram("💣 Job Name: ${JOB_NAME} \nBuild: ${BUILD_DISPLAY_NAME} \nStatus: Unstable \nLog: \n${env.BUILD_URL}console") }
failure { sendTelegram("💥 Job Name: ${JOB_NAME} \nBuild: ${BUILD_DISPLAY_NAME} \nStatus: Failure \nLog: \n${env.BUILD_URL}console") }
aborted { sendTelegram ("😥 Job Name: ${JOB_NAME} \nBuild: ${BUILD_DISPLAY_NAME} \nStatus: Aborted \nLog: \n${env.BUILD_URL}console") }
}
}
def sendTelegram(message) {
def encodedMessage = URLEncoder.encode(message, "UTF-8")
withCredentials([string(credentialsId: 'telegramToken', variable: 'TOKEN'),
string(credentialsId: 'telegramChatId', variable: 'CHAT_ID')]) {
response = httpRequest (consoleLogResponseBody: true,
contentType: 'APPLICATION_JSON',
httpMode: 'GET',
url: 'https://api.telegram.org/bot'+"$TOKEN"+'/sendMessage?text='+encodedMessage+'&chat_id='+"$CHAT_ID"+'&disable_web_page_preview=true',
validResponseCodes: '200')
return response
}
}
def getKubeconf(branchName) {
if("main".equals(branchName)) { return "config_prd"; }
else if ("master".equals(branchName)) { return "config_prd"; }
else if ("pre-prod".equals(branchName)) { return "config_prd"; }
else if ("homolog".equals(branchName)) { return "config_release"; }
else if ("release".equals(branchName)) { return "config_release"; }
else if ("release-r2".equals(branchName)) { return "config_release"; }
else if ("development".equals(branchName)) { return "config_release"; }
else if ("develop".equals(branchName)) { return "config_release"; }
}