#!/usr/bin/env groovy def PROTECTED_BRANCH def FROM_GIT def RUN_TESTS pipeline { agent any options { disableConcurrentBuilds() } environment { PROJECT_NAME = 'salix' STACK_NAME = "${env.PROJECT_NAME}-${env.BRANCH_NAME}" } tools { nodejs 'node-v20' } stages { stage('Checkout') { steps { script { // Uncomment to enable debugging //env.DEBUG = 'strong-remoting:http-context,strong-remoting:shared-method' switch (env.BRANCH_NAME) { case 'dev': env.NODE_ENV = 'dev' env.BACK_REPLICAS = 1 break case 'test': env.NODE_ENV = 'test' env.BACK_REPLICAS = 2 break case 'master': env.NODE_ENV = 'production' env.BACK_REPLICAS = 4 break } def packageJson = readJSON file: 'package.json' env.VERSION = packageJson.version env.GIT_COMMIT_MSG = sh( script: 'git log -1 --pretty=%B ${GIT_COMMIT}', returnStdout: true ).trim() PROTECTED_BRANCH = [ 'dev', 'test', 'master' ].contains(env.BRANCH_NAME) FROM_GIT = JOB_NAME.startsWith('gitea/') RUN_TESTS = !PROTECTED_BRANCH && FROM_GIT } setEnv() } } stage('Install') { environment { NODE_ENV = "" } parallel { stage('Backend') { steps { sh 'npm install --no-audit --prefer-offline' } } stage('Frontend') { when { expression { return FROM_GIT } } steps { sh 'npm install --no-audit --prefer-offline --prefix=front' } } stage('Print') { when { expression { return FROM_GIT } } steps { sh 'npm install --no-audit --prefer-offline --prefix=print' } } } } stage('Test') { when { expression { return 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 { return PROTECTED_BRANCH && FROM_GIT } } environment { CREDENTIALS = credentials('docker-registry') } steps { sh 'gulp build' dockerBuild() } } stage('Deploy') { when { expression { return 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 { return FROM_GIT } } environment { DOCKER_HOST = "${env.SWARM_HOST}" } steps { 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) { 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 { sendEmail() } } }