diff --git a/Jenkinsfile b/Jenkinsfile index acacfaa0c..40399b1a3 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -2,109 +2,104 @@ def buildError -try { - def registry = 'registry.verdnatura.es' - env.COMPOSE_PROJECT_NAME = env.JOB_NAME.replace('/', '-'); - env.TAG = env.BRANCH_NAME - - switch (env.BRANCH_NAME) { - case 'master': - env.PORT = 5002 - break; - case 'test': - env.PORT = 5001 - break; +pipeline { + environment { + REGISTRY = 'registry.verdnatura.es' + DOCKER_HOST_1 = 'tcp://vch1.verdnatura.es:2376' + DOCKER_HOST_2 = 'tcp://vch1.verdnatura.es:2376' + TAG = env.BRANCH_NAME } + script { + env.COMPOSE_PROJECT_NAME = env.JOB_NAME.replace('/', '-'); - switch (env.BRANCH_NAME) { - case 'master': - env.NODE_ENV = 'production' - break; - case 'test': - env.NODE_ENV = 'test' - break; - case 'dev': - env.NODE_ENV = 'development' - break; + switch (env.BRANCH_NAME) { + case 'master': + env.PORT = 5002 + break; + case 'test': + env.PORT = 5001 + break; + } + switch (env.BRANCH_NAME) { + case 'master': + env.NODE_ENV = 'production' + break; + case 'test': + env.NODE_ENV = 'test' + break; + case 'dev': + env.NODE_ENV = 'development' + break; + } } - - node { + stages { stage ('Checkout') { checkout scm - env.GIT_COMMITTER_EMAIL = sh( - script: "git --no-pager show -s --format='%ae'", - returnStdout: true - ).trim() + script { + env.GIT_COMMITTER_EMAIL = sh( + script: "git --no-pager show -s --format='%ae'", + returnStdout: true + ).trim() + } echo "Committer: ${env.GIT_COMMITTER_EMAIL}" } - stage ('Build') { + stage ('Install') { + environment { + NODE_ENV = null + } + nodejs('node-lts') { + sh "npm install --no-audit" + sh "gulp install" + } + } + stage ('Build') { + environment { + CREDS = credentials('docker-registry') + } nodejs('node-lts') { - withEnv(['NODE_ENV=']) { - sh "npm install --no-audit" - sh "gulp install" - } sh "gulp build" } - - withCredentials([usernamePassword( - credentialsId: 'docker-registry', - usernameVariable: 'USER', - passwordVariable: 'PASSWORD') - ]) { - sh "docker login --username \$USER --password \$PASSWORD ${registry}" - } - + sh 'docker login --username $CREDS_USR --password $CREDS_PSW $REGISTRY' sh "docker-compose build --parallel" sh "docker-compose push" } stage ('Deploy') { - withEnv(['DOCKER_TLS_VERIFY=1']) { - withCredentials([dockerCert( - credentialsId: 'docker', - variable: 'DOCKER_CERT_PATH') - ]) { - parallel( - vch1: { - withEnv(['DOCKER_HOST=tcp://vch1.verdnatura.es:2376']) { - sh "docker-compose pull" - sh "docker-compose up -d" - } - }, - vch2: { - withEnv(['DOCKER_HOST=tcp://vch2.verdnatura.es:2376']) { - sh "docker-compose pull" - sh "docker-compose up -d" - } - } - ) - } + environment { + DOCKER_TLS_VERIFY = 1 } + parallel( + vch1: { + environment { + DOCKER_HOST = env.DOCKER_HOST_1 + } + sh "docker-compose pull" + sh "docker-compose up -d" + }, + vch2: { + environment { + DOCKER_HOST = env.DOCKER_HOST_2 + } + sh "docker-compose pull" + sh "docker-compose up -d" + } + ) } stage ('Cleanup') { - sh "docker logout ${registry}" + sh 'docker logout $REGISTRY' } } -} catch(e) { - buildError = e -} + post { + always { + script { + if (!env.GIT_COMMITTER_EMAIL) + return -if (env.GIT_COMMITTER_EMAIL) { - def status - def body = "Check status at ${env.BUILD_URL}" - - if (buildError) { - status = 'Failed' - body += "\n\nError: ${buildError.toString()}" - } else { - status = 'Success' + mail( + to: env.GIT_COMMITTER_EMAIL, + subject: "Pipeline: ${env.JOB_NAME} (${env.BUILD_NUMBER}): ${currentBuild.result}", + body: "Check status at ${env.BUILD_URL}" + ) + } + } } - - mail( - to: env.GIT_COMMITTER_EMAIL, - subject: "Pipeline: ${status}: ${env.JOB_NAME} (${env.BUILD_NUMBER})", - body: body - ) } - -if (buildError) - throw buildError;