From d1cdba800d59d3aec8b83c563def92a1ea1759f8 Mon Sep 17 00:00:00 2001 From: Juan Ferrer Toribio Date: Thu, 22 Feb 2024 09:25:38 +0100 Subject: [PATCH] Jenkinsfile refactor --- Jenkinsfile | 53 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 3e434ad2..629d1db9 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,5 +1,27 @@ #!/usr/bin/env groovy +def BRANCH_ENV = [ + test: 'test', + master: 'production' +] + +node { + stage('Setup') { + env.MAIN_REPLICAS = 1 + env.CRON_REPLICAS = 0 + env.NODE_ENV = BRANCH_ENV[env.BRANCH_NAME] ?: 'dev' + + switch (env.BRANCH_NAME) { + case 'master': + env.MAIN_REPLICAS = 3 + env.CRON_REPLICAS = 1 + break + } + + echo "NODE_NAME: ${env.NODE_NAME}" + echo "WORKSPACE: ${env.WORKSPACE}" + } +} pipeline { agent any environment { @@ -7,28 +29,6 @@ pipeline { STACK_NAME = "${env.PROJECT_NAME}-${env.BRANCH_NAME}" } stages { - stage('Checkout') { - steps { - script { - def packageJson = readJSON file: 'package.json' - env.VERSION = packageJson.version - - switch (env.BRANCH_NAME) { - case 'master': - env.NODE_ENV = 'production' - env.MAIN_REPLICAS = 3 - env.CRON_REPLICAS = 1 - break - case 'test': - env.NODE_ENV = 'test' - env.MAIN_REPLICAS = 1 - env.CRON_REPLICAS = 0 - break - } - } - setEnv() - } - } stage('Debuild') { when { anyOf { @@ -60,6 +60,10 @@ pipeline { CREDS = credentials('docker-registry') } steps { + script { + def packageJson = readJSON file: 'package.json' + env.VERSION = packageJson.version + } sh 'docker login --username $CREDS_USR --password $CREDS_PSW $REGISTRY' sh 'docker-compose build --build-arg BUILD_ID=$BUILD_ID --parallel' sh 'docker-compose push' @@ -76,12 +80,17 @@ pipeline { DOCKER_HOST = "${env.SWARM_HOST}" } steps { + script { + def packageJson = readJSON file: 'package.json' + env.VERSION = packageJson.version + } sh "docker stack deploy --with-registry-auth --compose-file docker-compose.yml ${env.STACK_NAME}" } } } post { unsuccessful { + setEnv() sendEmail() } }