#!/usr/bin/env groovy pipeline { agent any options { disableConcurrentBuilds() } environment { PROJECT_NAME = 'salix' REGISTRY = 'registry.verdnatura.es' DOCKER_HOST_1 = 'vch1.verdnatura.es' DOCKER_HOST_2 = 'vch2.verdnatura.es' PORT_MASTER = '5002' PORT_TEST = '5001' TAG = "${env.BRANCH_NAME}" } stages { stage('Checkout') { steps { script { env.COMPOSE_PROJECT_NAME = "${env.PROJECT_NAME}-${env.BRANCH_NAME}" if (!env.GIT_COMMITTER_EMAIL) { env.COMMITTER_EMAIL = sh( script: 'git --no-pager show -s --format="%ae"', returnStdout: true ).trim() } else { env.COMMITTER_EMAIL = env.GIT_COMMITTER_EMAIL; } switch (env.BRANCH_NAME) { case 'master': env.PORT = PORT_MASTER break case 'test': env.PORT = PORT_TEST break } switch (env.BRANCH_NAME) { case 'master': env.NODE_ENV = 'production' break case 'test': env.NODE_ENV = 'test' break } } sh 'printenv' } } stage('Install') { environment { NODE_ENV = "" } steps { nodejs('node-lts') { sh 'npm install --no-audit' sh 'gulp install' } } } stage('Test') { when { not { anyOf { branch 'test' branch 'master' }}} environment { NODE_ENV = "" FIREFOX_BIN = "/opt/firefox/firefox-bin" } steps { nodejs('node-lts') { sh 'jest --ci --reporters=default --reporters=jest-junit --maxWorkers=1' sh 'gulp backTestDockerOnce --junit --random --run-chown' } } } stage('Build') { when { anyOf { branch 'test' branch 'master' }} 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 { anyOf { branch 'test' branch 'master' }} 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('Database') { when { anyOf { branch 'test' branch 'master' }} steps { configFileProvider([ configFile(fileId: "config.${env.NODE_ENV}.ini", variable: 'MYSQL_CONFIG') ]) { sh 'cp "$MYSQL_CONFIG" db/config.$NODE_ENV.ini' } sh 'db/import-changes.sh -f $NODE_ENV' } } stage('Cleanup') { when { anyOf { branch 'test' branch 'master' }} steps { sh 'docker logout $REGISTRY' } } } post { always { script { if (!['master', 'test'].contains(env.BRANCH_NAME)) { try { junit 'junitresults.xml' junit 'junit.xml' } catch (e) { echo e.toString() } } if (!env.COMMITTER_EMAIL) return try { mail( to: env.COMMITTER_EMAIL, subject: "Pipeline: ${env.JOB_NAME} (${env.BUILD_NUMBER}): ${currentBuild.currentResult}", body: "Check status at ${env.BUILD_URL}" ) } catch (e) { echo e.toString() } } } } }