#!/usr/bin/env groovy def PROTECTED_BRANCH def BRANCH_ENV = [ test: 'test', master: 'production' ] node { stage('Setup') { env.FRONT_REPLICAS = 1 env.NODE_ENV = BRANCH_ENV[env.BRANCH_NAME] ?: 'dev' PROTECTED_BRANCH = [ 'dev', 'test', 'master' ].contains(env.BRANCH_NAME) // https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#using-environment-variables echo "NODE_NAME: ${env.NODE_NAME}" echo "WORKSPACE: ${env.WORKSPACE}" configFileProvider([ configFile(fileId: 'salix-front.properties', variable: 'PROPS_FILE') ]) { def props = readProperties file: PROPS_FILE props.each {key, value -> env."${key}" = value } props.each {key, value -> echo "${key}: ${value}" } } if (PROTECTED_BRANCH) { configFileProvider([ configFile(fileId: "salix-front.branch.${env.BRANCH_NAME}", variable: 'BRANCH_PROPS_FILE') ]) { def props = readProperties file: BRANCH_PROPS_FILE props.each {key, value -> env."${key}" = value } props.each {key, value -> echo "${key}: ${value}" } } } } } pipeline { agent any options { disableConcurrentBuilds() } tools { nodejs 'node-v18' } environment { PROJECT_NAME = 'lilium' STACK_NAME = "${env.PROJECT_NAME}-${env.BRANCH_NAME}" } stages { stage('Install') { environment { NODE_ENV = "" } steps { sh 'pnpm install --prefer-offline' } } stage('Test') { when { expression { !PROTECTED_BRANCH } } environment { NODE_ENV = "" } steps { sh 'pnpm run test:unit:ci' } post { always { script { try { junit 'junitresults.xml' junit 'junit.xml' } catch (e) { echo e.toString() } } } } } stage('Build') { when { expression { PROTECTED_BRANCH } } environment { CREDENTIALS = credentials('docker-registry') } steps { sh 'quasar build' script { def packageJson = readJSON file: 'package.json' env.VERSION = packageJson.version } dockerBuild() } } stage('Deploy') { when { expression { PROTECTED_BRANCH } } environment { 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}" } } } }