#!/usr/bin/env groovy def PROTECTED_BRANCH def FROM_GIT def RUN_TESTS pre: { switch (env.BRANCH_NAME) { case 'test': env.NODE_ENV = 'test' env.BACK_REPLICAS = 2 break case 'master': env.NODE_ENV = 'production' env.BACK_REPLICAS = 4 break default: env.NODE_ENV = 'dev' env.BACK_REPLICAS = 1 } PROTECTED_BRANCH = [ 'dev', 'test', 'master' ].contains(env.BRANCH_NAME) FROM_GIT = env.JOB_NAME.startsWith('gitea/') RUN_TESTS = !PROTECTED_BRANCH && FROM_GIT // Uncomment to enable debugging // https://loopback.io/doc/en/lb3/Setting-debug-strings.html#debug-strings-reference //env.DEBUG = 'strong-remoting:shared-method' } 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('Backend') { steps { sh 'npm install --no-audit --prefer-offline' } } stage('Frontend') { when { expression { FROM_GIT } } steps { sh 'npm install --no-audit --prefer-offline --prefix=front' } } stage('Print') { when { expression { FROM_GIT } } steps { sh 'npm install --no-audit --prefer-offline --prefix=print' } } } } stage('Test') { when { expression { RUN_TESTS } } environment { NODE_ENV = "" TZ = 'Europe/Madrid' } parallel { stage('Backend') { steps { sh 'npm run test:back:ci' } } stage('Frontend') { steps { sh 'jest --ci --reporters=default --reporters=jest-junit --maxWorkers=6' } } } } stage('Build') { when { expression { PROTECTED_BRANCH && FROM_GIT } } environment { CREDENTIALS = credentials('docker-registry') } steps { script { def packageJson = readJSON file: 'package.json' env.VERSION = packageJson.version } sh 'gulp build' dockerBuild() } } 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 { always { script { if (RUN_TESTS) { try { junit 'junitresults.xml' junit 'junit.xml' } catch (e) { echo e.toString() } } } } 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) rocketSend( channel: 'vn-database', message: "*DB version uploaded:* ${message}" +"\n$COMMITTER_EMAIL ($BRANCH_NAME)" +"\n$GIT_URL/commit/$GIT_COMMIT", rawMessage: true ) } } } unsuccessful { setEnv() sendEmail() } } }