salix/Jenkinsfile

109 lines
2.9 KiB
Groovy

#!/usr/bin/env groovy
def buildError
try {
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;
}
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;
}
node {
stage ('Checkout') {
checkout scm
env.GIT_COMMITTER_EMAIL = sh(
script: "git --no-pager show -s --format='%ae'",
returnStdout: true
).trim()
echo "Committer: ${env.GIT_COMMITTER_EMAIL}"
}
stage ('Build') {
nodejs('node-lts') {
sh "env NODE_ENV= npm install --no-audit"
sh "gulp install"
sh "gulp build"
}
withCredentials([usernamePassword(
credentialsId: 'docker-registry',
usernameVariable: 'USER',
passwordVariable: 'PASSWORD')
]) {
sh "docker login --username \"${env.USER}\" --password \"${env.PASSWORD}\" ${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"
}
}
)
}
}
}
stage ('Cleanup') {
sh "docker logout ${registry}"
}
}
} catch(e) {
buildError = e
}
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: ${status}: ${env.JOB_NAME} (${env.BUILD_NUMBER})",
body: body
)
}
if (buildError)
throw buildError;