-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjenkinsfile-simulador-serap-app
119 lines (109 loc) · 5.24 KB
/
jenkinsfile-simulador-serap-app
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
pipeline {
environment {
branchname = env.BRANCH_NAME.toLowerCase()
kubeconfig = getKubeconf(env.branchname)
registryCredential = 'jenkins_registry'
namespace = "${env.branchname == 'develop' ? 'serap-estud-dev' : env.branchname == 'release' ? 'serap-estud-hom' : env.branchname == 'release-r2' ? 'serap-estud-hom2' : 'serap-estudante' }"
deployment = "${env.branchname == 'develop' ? 'simulador-serap-front' : env.branchname == 'release' ? 'simulador-serap-front' : env.branchname == 'release-r2' ? 'simulador-serap-front' : 'sme-simulador-prova-serap-front' }"
}
agent { kubernetes {
label 'builder'
defaultContainer 'builder'
}
}
options {
buildDiscarder(logRotator(numToKeepStr: '5', artifactNumToKeepStr: '5'))
disableConcurrentBuilds()
skipDefaultCheckout()
}
stages {
stage('CheckOut') {
steps {
checkout scm
script {
APP_VERSION = sh(returnStdout: true, script: "cat pubspec.yaml | grep version: | awk '{print \$2}'") .trim()
sh("echo ${APP_VERSION}")
sh("echo ${BUILD_NUMBER}")
}
}
}
stage('Build Dev') {when { anyOf { branch 'develop'; } }
steps {
withCredentials([ file(credentialsId: "simulador-prova-serap-front-environment-${branchname}", variable: 'ENVS')]) {
script {
sh 'if [ -d "envs" ]; then rm -f envs; fi'
sh 'cp ${ENVS} .env'
sh 'if [ -d "envs" ]; then rm -f envs; fi'
imagename1 = "registry.sme.prefeitura.sp.gov.br/${env.branchname}/sme-simulador-prova-serap-front"
dockerImage1 = docker.build(imagename1, " --build-arg ENVIRONMENT=development --build-arg APP_VERSION=${APP_VERSION} --build-arg BUILD_NUMBER=${BUILD_NUMBER} -f Dockerfile .")
docker.withRegistry( 'https://registry.sme.prefeitura.sp.gov.br', registryCredential ) {
dockerImage1.push()
}
sh "docker rmi $imagename1"
}
}
}
}
stage('Build Hom') {when { anyOf { branch 'release'; } }
steps {
withCredentials([ file(credentialsId: "simulador-prova-serap-front-environment-${branchname}", variable: 'ENVS')]) {
script {
sh 'if [ -d "envs" ]; then rm -f envs; fi'
sh 'cp ${ENVS} .env'
sh 'if [ -d "envs" ]; then rm -f envs; fi'
imagename1 = "registry.sme.prefeitura.sp.gov.br/${env.branchname}/sme-simulador-prova-serap-front"
dockerImage1 = docker.build(imagename1, " --build-arg ENVIRONMENT=staging --build-arg APP_VERSION=${APP_VERSION} --build-arg BUILD_NUMBER=${BUILD_NUMBER} -f Dockerfile .")
docker.withRegistry( 'https://registry.sme.prefeitura.sp.gov.br', registryCredential ) {
dockerImage1.push()
}
sh "docker rmi $imagename1"
}
}
}
}
stage('Build Prod') {when { anyOf { branch 'master'; } }
steps {
withCredentials([ file(credentialsId: "simulador-prova-serap-front-environment-${branchname}", variable: 'ENVS')]) {
script {
sh 'if [ -d "envs" ]; then rm -f envs; fi'
sh 'cp ${ENVS} .env'
sh 'if [ -d "envs" ]; then rm -f envs; fi'
imagename1 = "registry.sme.prefeitura.sp.gov.br/${env.branchname}/sme-simulador-prova-serap-front"
dockerImage1 = docker.build(imagename1, " --build-arg ENVIRONMENT=production --build-arg APP_VERSION=${APP_VERSION} --build-arg BUILD_NUMBER=${BUILD_NUMBER} -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 'develop'; branch 'release'; branch 'homolog'; } }
steps {
script{
withCredentials([file(credentialsId: "${kubeconfig}", variable: 'config')]){
sh('if [ -f '+"$home"+'/.kube/config ];then rm -f '+"$home"+'/.kube/config; fi')
sh('cp $config '+"$home"+'/.kube/config')
sh 'kubectl rollout restart deployment/${deployment} -n ${namespace}'
sh('rm -f '+"$home"+'/.kube/config')
}
}
}
}
}
post {
always {
sh('if [ -f '+"$home"+'/.kube/config ];then rm -f '+"$home"+'/.kube/config; fi')
sh 'if [ -d "envs" ]; then rm -f envs; fi'
sh('if [ -f .env ];then rm -f .env; fi')
}
}
}
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 ("release".equals(branchName)) { return "config_release"; }
else if ("develop".equals(branchName)) { return "config_release"; }
}