Skip to content

Commit

Permalink
Merge pull request #62 from prefeiturasp/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
ebufaino authored Jul 8, 2021
2 parents 69dd3f5 + c851644 commit 04c8098
Showing 1 changed file with 89 additions and 198 deletions.
287 changes: 89 additions & 198 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,218 +1,109 @@
pipeline {
environment {
branchname = env.BRANCH_NAME.toLowerCase()
kubeconfig = getKubeconf(env.branchname)
registryCredential = 'jenkins_registry'
}

agent {
node {
label 'py-uniformes'
}
node { label 'python-36-pratoaberto' }
}

options {
buildDiscarder(logRotator(numToKeepStr: '50', artifactNumToKeepStr: '50'))
buildDiscarder(logRotator(numToKeepStr: '5', artifactNumToKeepStr: '5'))
disableConcurrentBuilds()
skipDefaultCheckout()
skipDefaultCheckout()
}



stages {
stage('CheckOut') {
steps {
checkout scm
}
}

stage('Docker build DEV') {
when {
branch 'development'
}
steps {
sh 'echo Build Docker image'

// Start JOB Rundeck para build das imagens Docker e push SME Registry

script {
step([$class: "RundeckNotifier",
includeRundeckLogs: true,

//JOB DE BUILD
jobId: "8c184d21-ec1f-4a4d-9907-ff7c2973369a",
nodeFilters: "",
//options: """
// PARAM_1=value1
// PARAM_2=value2
// PARAM_3=
// """,
rundeckInstance: "Rundeck-SME",
shouldFailTheBuild: true,
shouldWaitForRundeckJob: true,
tags: "",
tailLog: true])
}
}
}

stage('Deploy DEV') {
when {
branch 'development'
stage('CheckOut') {
steps { checkout scm }
}
steps {
sh 'echo Deploying desenvolvimento'
//Start JOB Rundeck para update de deploy Kubernetes DEV

script {
step([$class: "RundeckNotifier",
includeRundeckLogs: true,
jobId: "69734996-1fcb-4cb1-9f16-643a38c7616c",
nodeFilters: "",
//options: """
// PARAM_1=value1
// PARAM_2=value2
// PARAM_3=
// """,
rundeckInstance: "Rundeck-SME",
shouldFailTheBuild: true,
shouldWaitForRundeckJob: true,
tags: "",
tailLog: true])
}


}
}

stage('Docker build HOM') {
when {
branch 'homolog'
}
steps {
sh 'echo build homologacao'

// Start JOB Rundeck para build das imagens Docker e push registry SME

script {
step([$class: "RundeckNotifier",
includeRundeckLogs: true,


//JOB DE BUILD
jobId: "8bddeef2-0fd7-47fc-8775-dc7723f96ba6",
nodeFilters: "",
//options: """
// PARAM_1=value1
// PARAM_2=value2
// PARAM_3=
// """,
rundeckInstance: "Rundeck-SME",
shouldFailTheBuild: true,
shouldWaitForRundeckJob: true,
tags: "",
tailLog: true])
}
}
}

stage('Deploy HOM') {
when {
branch 'homolog'
}
steps {
timeout(time: 24, unit: "HOURS") {

telegramSend("${JOB_NAME}...O Build ${BUILD_DISPLAY_NAME} - Requer uma aprovação para deploy !!!\n Consulte o log para detalhes -> [Job logs](${env.BUILD_URL}console)\n")
input message: 'Deseja realizar o deploy?', ok: 'SIM', submitter: 'anderson_morais'
}



//Start JOB Rundeck para update de imagens no host homologação

script {
step([$class: "RundeckNotifier",
includeRundeckLogs: true,
jobId: "a2cabd5f-c96d-4576-b385-c8a0543d6e92",
nodeFilters: "",
//options: """
// PARAM_1=value1
// PARAM_2=value2
// PARAM_3=
// """,
rundeckInstance: "Rundeck-SME",
shouldFailTheBuild: true,
shouldWaitForRundeckJob: true,
tags: "",
tailLog: true])
}


stage('AnaliseCodigo') {
when { branch 'homolog' }
steps {
withSonarQubeEnv('sonarqube-local'){
sh 'echo "[ INFO ] Iniciando analise Sonar..." && sonar-scanner \
-Dsonar.projectKey=SME-PratoAberto-API \
-Dsonar.sources=.'
}
}
}

stage('Docker Build PROD') {


when {
branch 'master'
}
steps {
timeout(time: 24, unit: "HOURS") {

telegramSend("${JOB_NAME}...O Build ${BUILD_DISPLAY_NAME} - Requer uma aprovação para deploy !!!\n Consulte o log para detalhes -> [Job logs](${env.BUILD_URL}console)\n")
input message: 'Deseja realizar o deploy?', ok: 'SIM', submitter: 'anderson_morais'
}
sh 'echo Deploy produção'

// Start JOB Rundeck para build das imagens Docker e push registry SME

script {
step([$class: "RundeckNotifier",
includeRundeckLogs: true,


//JOB DE BUILD
jobId: "c20d965c-7e50-4ada-b093-5c7856676430",
nodeFilters: "",
//options: """
// PARAM_1=value1
// PARAM_2=value2
// PARAM_3=
// """,
rundeckInstance: "Rundeck-SME",
shouldFailTheBuild: true,
shouldWaitForRundeckJob: true,
tags: "",
tailLog: true])
}
stage('Build') {
when { anyOf { branch 'master'; branch 'main'; branch "story/*"; branch 'development'; branch 'release'; branch 'homolog'; } }
steps {
script {
imagename1 = "registry.sme.prefeitura.sp.gov.br/${env.branchname}/pratoaberto-api"
//imagename2 = "registry.sme.prefeitura.sp.gov.br/${env.branchname}/sme-outra"
dockerImage1 = docker.build(imagename1, "-f Dockerfile .")
//dockerImage2 = docker.build(imagename2, "-f Dockerfile_outro .")
docker.withRegistry( 'https://registry.sme.prefeitura.sp.gov.br', registryCredential ) {
dockerImage1.push()
//dockerImage2.push()
}
sh "docker rmi $imagename1"
//sh "docker rmi $imagename2"
}
}

stage('Deploy PROD') {
when {
branch 'master'
}
steps {
timeout(time: 24, unit: "HOURS") {
telegramSend("${JOB_NAME}...O Build ${BUILD_DISPLAY_NAME} - Requer uma aprovação para deploy !!!\n Consulte o log para detalhes -> [Job logs](${env.BUILD_URL}console)\n")
input message: 'Deseja realizar o deploy?', ok: 'SIM', submitter: 'anderson_morais, bruno_alevato'
}

//Start JOB Rundeck para deploy em produção

script {
step([$class: "RundeckNotifier",
includeRundeckLogs: true,
jobId: "78bbdf0e-625e-42a6-8237-8a7d573a4f28",
nodeFilters: "",
//options: """
// PARAM_1=value1
// PARAM_2=value2
// PARAM_3=
// """,
rundeckInstance: "Rundeck-SME",
shouldFailTheBuild: true,
shouldWaitForRundeckJob: true,
tags: "",
tailLog: true])
}
}
}

}

stage('Deploy'){
when { anyOf { branch 'master'; branch 'main'; branch 'development'; branch 'release'; branch 'homolog'; } }
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: 'kelwy_oliveira, anderson_morais, luis_zimmermann, rodolpho_azeredo'
}
withCredentials([file(credentialsId: "${kubeconfig}", variable: 'config')]){
sh('cp $config '+"$home"+'/.kube/config')
sh 'kubectl rollout restart deployment/pratoaberto-api -n sme-pratoaberto'
sh('rm -f '+"$home"+'/.kube/config')
}
}
else{
withCredentials([file(credentialsId: "${kubeconfig}", variable: 'config')]){
sh('cp $config '+"$home"+'/.kube/config')
sh 'kubectl rollout restart deployment/pratoaberto-api -n sme-pratoaberto'
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_hom"; }
else if ("release".equals(branchName)) { return "config_hom"; }
else if ("development".equals(branchName)) { return "config_dev"; }
}

0 comments on commit 04c8098

Please sign in to comment.