#!/usr/bin/env groovy def PROTECTED_BRANCH def FROM_GIT def RUN_TESTS def RUN_BUILD def BRANCH_ENV = [ test: 'test', master: 'production' ] node { stage('Setup') { env.BACK_REPLICAS = 1 env.NODE_ENV = BRANCH_ENV[env.BRANCH_NAME] ?: 'dev' PROTECTED_BRANCH = [ 'dev', 'test', 'master' ].contains(env.BRANCH_NAME) FROM_GIT = env.JOB_NAME.startsWith('gitea/') RUN_TESTS = !PROTECTED_BRANCH && FROM_GIT RUN_BUILD = PROTECTED_BRANCH && FROM_GIT // 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.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.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-v20' } environment { PROJECT_NAME = 'salix' STACK_NAME = "${env.PROJECT_NAME}-${env.BRANCH_NAME}" } stages { stage('Install') { environment { NODE_ENV = '' } parallel { stage('Back') { steps { sh 'pnpm install --prefer-offline' } } stage('Print') { when { expression { FROM_GIT } } steps { sh 'pnpm install --prefer-offline --prefix=print' } } stage('Front') { when { expression { FROM_GIT } } steps { sh 'pnpm install --prefer-offline --prefix=front' } } } } stage('Stack') { parallel { stage('Back') { stages { stage('Test') { when { expression { RUN_TESTS } } environment { NODE_ENV = '' } steps { sh 'node back/tests.js --ci --junit --network jenkins' } post { always { junit( testResults: 'junitresults.xml', allowEmptyResults: true ) } } } stage('Build') { when { expression { RUN_BUILD } } steps { script { def packageJson = readJSON file: 'package.json' env.VERSION = packageJson.version } sh 'docker-compose build back' } } } } stage('Front') { when { expression { FROM_GIT } } stages { stage('Test') { when { expression { RUN_TESTS } } environment { NODE_ENV = '' } steps { sh 'jest --ci --reporters=default --reporters=jest-junit --maxWorkers=10' } post { always { junit( testResults: 'junit.xml', allowEmptyResults: true ) } } } stage('Build') { when { expression { RUN_BUILD } } steps { script { def packageJson = readJSON file: 'package.json' env.VERSION = packageJson.version } sh 'gulp build' sh 'docker-compose build front' } } } } } } stage('Push') { when { expression { RUN_BUILD } } environment { CREDENTIALS = credentials('docker-registry') } steps { script { def packageJson = readJSON file: 'package.json' env.VERSION = packageJson.version } sh 'docker login --username $CREDENTIALS_USR --password $CREDENTIALS_PSW $REGISTRY' sh 'docker-compose push' } } stage('Deploy') { when { expression { PROTECTED_BRANCH } } parallel { stage('Database') { steps { configFileProvider([ configFile(fileId: "config.${env.NODE_ENV}.ini", variable: 'MYSQL_CONFIG') ]) { sh 'mkdir -p db/remotes' sh 'cp "$MYSQL_CONFIG" db/remotes/$NODE_ENV.ini' } sh 'npx myt push $NODE_ENV --force --commit' } } stage('Docker') { when { expression { FROM_GIT } } 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}" } } } } } post { success { script { if (env.BRANCH_NAME == 'master' && FROM_GIT) { env.GIT_COMMIT_MSG = sh( script: 'git log -1 --pretty=%B ${GIT_COMMIT}', returnStdout: true ).trim() String message = env.GIT_COMMIT_MSG int index = message.indexOf('\n') if (index != -1) message = message.substring(0, index) setEnv() rocketSend( channel: 'vn-database', message: "*DB version uploaded:* ${message}" +"\n$COMMITTER_EMAIL ($BRANCH_NAME)" +"\n$RUN_DISPLAY_URL", rawMessage: true ) } } } unsuccessful { setEnv() sendEmail() } } }