-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJenkinsfile
127 lines (112 loc) · 5.17 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
pipeline {
environment {
branchname = env.BRANCH_NAME.toLowerCase()
kubeconfig = getKubeconf(env.branchname)
registryCredential = 'jenkins_registry'
}
agent { kubernetes {
label 'dotnet-3-rc'
defaultContainer 'dotnet-3-rc'
}
}
options {
buildDiscarder(logRotator(numToKeepStr: '20', artifactNumToKeepStr: '20'))
disableConcurrentBuilds()
skipDefaultCheckout()
}
stages {
stage('CheckOut') {
steps { checkout scm }
}
stage('Build projeto') {
steps {
sh "echo executando build Worker de projeto"
sh 'cd Src/GestaoAvaliacao.Worker.StudentTestsSent/ && dotnet build'
}
}
stage('AnaliseCodigo') {
when { branch 'release' }
steps {
withSonarQubeEnv('sonarqube-local'){
sh 'echo "[ INFO ] Iniciando analise Sonar..." && dotnet-sonarscanner begin /k:"SME-Serap-main"'
sh 'dotnet build'
sh 'dotnet-sonarscanner end'
}
}
}
stage('Build') {
when { anyOf { branch 'master'; branch 'main'; branch "story/*"; branch 'development'; branch 'release'; branch 'homolog'; } }
agent { kubernetes {
label 'builder'
defaultContainer 'builder'
}
}
steps {
script {
imagename1 = "registry.sme.prefeitura.sp.gov.br/${env.branchname}/serap-worker"
dockerImage1 = docker.build(imagename1, "-f Src/GestaoAvaliacao.Worker.StudentTestsSent/Dockerfile .")
docker.withRegistry( 'https://registry.sme.prefeitura.sp.gov.br', registryCredential ) {
dockerImage1.push()
}
sh "docker rmi $imagename1"
//sh "docker rmi $imagename2"
}
}
}
stage('Deploy'){
when { anyOf { branch 'master'; branch 'main'; branch 'development'; branch 'release'; branch 'homolog'; } }
agent { kubernetes {
label 'builder'
defaultContainer 'builder'
}
}
steps {
script{
if ( env.branchname == 'main' || env.branchname == 'master' || env.branchname == 'homolog' || env.branchname == 'release' ) {
sendTelegram("🤩 [Deploy ${env.branchname}] Job Name: ${JOB_NAME} \nBuild: ${BUILD_DISPLAY_NAME} \nMe aprove! \nLog: \n${env.BUILD_URL}")
timeout(time: 24, unit: "HOURS") {
input message: 'Deseja realizar o deploy?', ok: 'SIM', submitter: 'marlon_goncalves, bruno_alevato'
}
withCredentials([file(credentialsId: "${kubeconfig}", variable: 'config')]){
sh('cp $config '+"$home"+'/.kube/config')
sh 'kubectl rollout restart deployment/serap-worker -n sme-serap'
sh('rm -f '+"$home"+'/.kube/config')
}
}
else{
withCredentials([file(credentialsId: "${kubeconfig}", variable: 'config')]){
sh('cp $config '+"$home"+'/.kube/config')
sh 'kubectl rollout restart deployment/serap-worker -n sme-serap'
sh('rm -f '+"$home"+'/.kube/config')
}
}
}
}
}
}
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 ("homolog".equals(branchName)) { return "config_release"; }
else if ("release".equals(branchName)) { return "config_release"; }
else if ("development".equals(branchName)) { return "config_release"; }
}