-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjenkinsfile-ptrf-backend
177 lines (163 loc) · 7.98 KB
/
jenkinsfile-ptrf-backend
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
pipeline {
environment {
branchname = env.BRANCH_NAME.toLowerCase()
kubeconfig = getKubeconf(env.branchname)
registryCredential = 'jenkins_registry'
namespace = "${env.branchname == 'develop' ? 'sme-ptrf-dev' : env.branchname == 'suporte' ? 'sme-ptrf-hom2' : env.branchname == 'homolog' ? 'sme-ptrf-hom' : env.branchname == 'homolog-r2' ? 'sme-ptrf-hom2' : 'sme-ptrf' }"
}
agent { kubernetes {
label 'builder'
defaultContainer 'builder'
}
}
options {
buildDiscarder(logRotator(numToKeepStr: '20', artifactNumToKeepStr: '5'))
disableConcurrentBuilds()
skipDefaultCheckout()
}
stages {
stage('CheckOut') {
steps { checkout scm }
}
stage('Testes Lint') {
when { anyOf { branch 'master'; branch 'develop'; branch 'suporte'; branch 'homolog-r2_'; branch 'pre-release'; branch 'atualizarpython_'; branch 'homolog_' } }
agent {
kubernetes {
label 'python311'
defaultContainer 'python311'
}
}
steps {
checkout scm
sh 'pip install --user pipenv -r requirements/local.txt' //instalação das dependências
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
sh '''
pwd
export PATH=$PATH:/root/.local/bin
python manage.py collectstatic --noinput
flake8 --format=pylint --exit-zero --exclude migrations,__pycache__,manage.py,settings.py,.env,__tests__,tests --output-file=flake8-output.txt
'''
}
}
}
stage('Testes Unitarios') {
when { anyOf { branch 'master'; branch 'develop'; branch 'suporte'; branch 'homolog-r2_'; branch 'pre-release'; branch 'atualizarpython_'; branch 'homolog_' } }
agent {
kubernetes {
label 'python311'
defaultContainer 'python311'
}
}
steps {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') { //Segue esteira mesmo com falha
checkout scm
sh 'pip install --user pipenv -r requirements/local.txt' //instalação das dependências
sh 'pip install pytest-xdist'
sh '''
export PATH=$PATH:/root/.local/bin
python manage.py collectstatic --noinput
coverage run -m pytest -n 4
coverage xml
'''
}
}
}
stage('AnaliseCodigo') {
when { anyOf { branch 'master'; branch 'develop'; branch 'suporte'; branch 'homolog-r2_'; branch 'pre-release'; branch 'atualizarpython' } }
agent { kubernetes {
label 'python311'
defaultContainer 'builder'
}
}
steps {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') { //Segue esteira mesmo com falha
withSonarQubeEnv('sonarqube-local'){
sh 'echo "[ INFO ] Iniciando analise Sonar..." && sonar-scanner \
-Dsonar.projectKey=SME-PTRF-BackEnd \
-Dsonar.python.coverage.reportPaths=*.xml'
}
}
}
}
stage('Build') {
when { anyOf { branch 'master'; branch 'main'; branch "story/*"; branch 'develop'; branch 'release'; branch 'homolog'; branch 'suporte'; branch 'homolog-r2_'; branch 'pre-release'; branch 'atualizarpython' } }
steps {
script {
imagename1 = "registry.sme.prefeitura.sp.gov.br/${env.branchname}/ptrf-backend"
dockerImage1 = docker.build(imagename1, "-f Dockerfile .")
docker.withRegistry( 'https://registry.sme.prefeitura.sp.gov.br', registryCredential ) {
dockerImage1.push()
}
sh "docker rmi $imagename1"
}
}
}
stage('Deploy'){
when { anyOf { branch 'master'; branch 'main'; branch 'development'; branch 'develop'; branch 'release'; branch 'homolog'; branch 'suporte'; branch 'homolog-r2_'; branch 'pre-release'; branch 'atualizarpython' } }
steps {
script{
if ( env.branchname == 'main' || env.branchname == 'master' || env.branchname == 'homolog' || env.branchname == 'release' ) {
withCredentials([string(credentialsId: 'aprovadores-ptrf', variable: 'aprovadores')]) {
timeout(time: 24, unit: "HOURS") {
input message: 'Deseja realizar o deploy?', ok: 'SIM', submitter: "${aprovadores}"
}
}
}
withCredentials([file(credentialsId: "${kubeconfig}", variable: 'config')]){
if( env.branchname == 'atualizarpython' ){
sh('rm -f '+"$home"+'/.kube/config')
sh('cp $config '+"$home"+'/.kube/config')
sh 'kubectl rollout restart deployment/sigescolapre-backend -n sme-sigescola-pre'
sh 'kubectl rollout restart deployment/sigescolapre-celery -n sme-sigescola-pre'
sh 'kubectl rollout restart deployment/sigescolapre-flower -n sme-sigescola-pre'
sh('rm -f '+"$home"+'/.kube/config')
} else {
sh('rm -f '+"$home"+'/.kube/config')
sh('cp $config '+"$home"+'/.kube/config')
sh "echo ${namespace}"
sh "kubectl rollout restart deployment/ptrf-backend -n ${namespace}"
sh "kubectl rollout restart deployment/ptrf-celery -n ${namespace}"
sh "kubectl rollout restart deployment/ptrf-flower -n ${namespace}"
}
}
}
}
}
stage('Deploy Treino'){
when { anyOf { branch 'master'; branch 'main' } }
steps {
withCredentials([file(credentialsId: "config_release", variable: 'config')]){
sh('rm -f '+"$home"+'/.kube/config')
sh('cp $config '+"$home"+'/.kube/config')
sh 'kubectl rollout restart deployment/treinamento-backend -n sme-ptrf-treino'
sh 'kubectl rollout restart deployment/treinamento-celery -n sme-ptrf-treino'
sh 'kubectl rollout restart deployment/treinamento-flower -n sme-ptrf-treino'
sh('rm -f '+"$home"+'/.kube/config')
}
}
}
stage('Deploy QA'){
when { anyOf { branch 'homolog' } }
steps {
withCredentials([file(credentialsId: "config_release", variable: 'config')]){
sh('rm -f '+"$home"+'/.kube/config')
sh('cp $config '+"$home"+'/.kube/config')
sh 'kubectl rollout restart deployment/qa-backend -n sme-ptrf-qa'
sh('rm -f '+"$home"+'/.kube/config')
}
}
}
}
}
def getKubeconf(branchName) {
if("main".equals(branchName)) { return "config_prod"; }
else if ("master".equals(branchName)) { return "config_prod"; }
else if ("homolog".equals(branchName)) { return "config_release"; }
else if ("homolog-r2".equals(branchName)) { return "config_release"; }
else if ("suporte".equals(branchName)) { return "config_release"; }
else if ("release".equals(branchName)) { return "config_release"; }
else if ("development".equals(branchName)) { return "config_release"; }
else if ("develop".equals(branchName)) { return "config_release"; }
else if ("pre-release".equals(branchName)) { return "config_prd"; }
else if ("atualizarpython".equals(branchName)) { return "config_prd"; }
}