#!/usr/bin/env groovy def PROTECTED_BRANCH def FROM_GIT def RUN_TESTS def RUN_BUILD node { stage('Setup') { 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 RUN_BUILD = 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('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') { environment { TZ = 'Europe/Madrid' } parallel { stage('Back') { stages { stage('Test') { when { expression { RUN_TESTS } } environment { NODE_ENV = '' } steps { sh 'npm run test:back:ci' } post { always { script { try { junit 'junitresults.xml' junit 'junit.xml' } catch (e) { echo e.toString() } } } } } 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' } } 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) 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() } } }