salix/Jenkinsfile

154 lines
4.7 KiB
Groovy

#!/usr/bin/env groovy
pipeline {
agent any
options {
disableConcurrentBuilds()
}
environment {
REGISTRY = 'registry.verdnatura.es'
DOCKER_HOST_1 = 'vch1.verdnatura.es'
DOCKER_HOST_2 = 'vch2.verdnatura.es'
TAG = "${env.BRANCH_NAME}"
}
stages {
stage('Checkout') {
steps {
script {
env.COMPOSE_PROJECT_NAME = env.JOB_NAME.replace('/', '-');
env.GIT_COMMITTER_EMAIL = sh(
script: 'git --no-pager show -s --format="%ae"',
returnStdout: true
).trim()
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;
}
}
echo "Committer: ${env.GIT_COMMITTER_EMAIL}"
}
}
stage('Install') {
environment {
NODE_ENV = ""
}
steps {
nodejs('node-lts') {
sh 'npm install --no-audit'
sh 'gulp install'
}
}
}
stage('Test') {
when {
branch 'dev';
}
environment {
NODE_ENV = ""
FIREFOX_BIN = "/opt/firefox/firefox-bin"
DB_HOST = "${env.DOCKER_HOST_2}"
}
steps {
nodejs('node-lts') {
sh 'karma start --junit'
sh 'gulp backTestDocker --junit --random --run-chown'
}
}
}
stage('Build') {
when {
not { branch 'dev' }
}
environment {
CREDS = credentials('docker-registry')
}
steps {
nodejs('node-lts') {
sh 'gulp build'
}
sh 'docker login --username $CREDS_USR --password $CREDS_PSW $REGISTRY'
sh 'docker-compose build --parallel'
sh 'docker-compose push'
}
}
stage('Deploy') {
when {
not { branch 'dev' }
}
environment {
DOCKER_TLS_VERIFY = 1
}
parallel {
stage('Host 1') {
environment {
DOCKER_HOST = "tcp://${env.DOCKER_HOST_1}:2376"
}
steps {
withCredentials([dockerCert(credentialsId: 'docker', variable: 'DOCKER_CERT_PATH')]) {
sh 'docker-compose pull'
sh 'docker-compose up -d --scale back=2'
}
}
}
stage('Host 2') {
environment {
DOCKER_HOST = "tcp://${env.DOCKER_HOST_2}:2376"
}
steps {
withCredentials([dockerCert(credentialsId: 'docker', variable: 'DOCKER_CERT_PATH')]) {
sh 'docker-compose pull'
sh 'docker-compose up -d --scale back=2'
}
}
}
}
}
stage('Cleanup') {
when {
not { branch 'dev' }
}
steps {
sh 'docker logout $REGISTRY'
}
}
}
post {
always {
script {
if (env.BRANCH_NAME == 'dev') {
junit '*/junitresults.xml'
junit 'junitresults.xml'
}
if (!env.GIT_COMMITTER_EMAIL) return
try {
mail(
to: env.GIT_COMMITTER_EMAIL,
subject: "Pipeline: ${env.JOB_NAME} (${env.BUILD_NUMBER}): ${currentBuild.currentResult}",
body: "Check status at ${env.BUILD_URL}"
)
} catch (e) {
echo e.toString()
}
}
}
}
}