#!/usr/bin/env groovy pipeline { agent any options { disableConcurrentBuilds() } environment { PROJECT_NAME = 'salix' STACK_NAME = "${env.PROJECT_NAME}-${env.BRANCH_NAME}" } stages { stage('Checkout') { steps { script { def packageJson = readJSON file: 'package.json' env.VERSION = packageJson.version 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 } env.PROTECTED_BRANCH = [ 'dev', 'test', 'master' ].contains(env.BRANCH_NAME) env.GIT_COMMIT_MSG = sh( script: 'git log -1 --pretty=%B ${GIT_COMMIT}', returnStdout: true ).trim() if (env.JOB_NAME.startsWith('gitea/')) env.RUN_SOURCE = 'git' } setEnv() } } stage('Install') { environment { NODE_ENV = "" } parallel { stage('Backend') { steps { nodejs('node-v20') { sh 'npm install --no-audit --prefer-offline' } } } stage('Frontend') { steps { nodejs('node-v20') { sh 'npm install --no-audit --prefer-offline --prefix=front' } } } stage('Print') { steps { nodejs('node-v20') { sh 'npm install --no-audit --prefer-offline --prefix=print' } } } } } stage('Test') { when { environment name: 'RUN_SOURCE', value: 'git' not { anyOf { branch 'dev' branch 'test' branch 'master' }} } environment { NODE_ENV = "" TZ = 'Europe/Madrid' } parallel { stage('Backend') { steps { nodejs('node-v20') { sh 'npm run test:back:ci' } } } stage('Frontend') { steps { nodejs('node-v20') { sh 'jest --ci --reporters=default --reporters=jest-junit --maxWorkers=6' } } } } } stage('Build') { when { environment name: 'RUN_SOURCE', value: 'git' anyOf { branch 'dev' branch 'test' branch 'master' } } environment { CREDENTIALS = credentials('docker-registry') } steps { nodejs('node-v20') { sh 'gulp build' } dockerBuild() } } stage('Deploy') { when { anyOf { branch 'dev' branch 'test' branch 'master' }} 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' } nodejs('node-v20') { sh 'npx myt push $NODE_ENV --force --commit' } } } stage('Docker') { when { environment name: 'RUN_SOURCE', value: 'git' } environment { DOCKER_HOST = "${env.SWARM_HOST}" } steps { sh "docker stack deploy --with-registry-auth --compose-file docker-compose.yml ${env.STACK_NAME}" } } } } } post { success { script { if (env.RUN_SOURCE == 'git' && env.BRANCH_NAME == 'master') { 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 ) } } } always { script { if (env.RUN_SOURCE == 'git' && !['master', 'test'].contains(env.BRANCH_NAME)) { try { junit 'junitresults.xml' junit 'junit.xml' } catch (e) { echo e.toString() } } if (!env.COMMITTER_EMAIL || currentBuild.currentResult == 'SUCCESS') return; try { mail( to: env.COMMITTER_EMAIL, subject: "Pipeline: ${env.JOB_NAME} (${env.BUILD_NUMBER}): ${currentBuild.currentResult}", body: "Check status at ${env.BUILD_URL}" ) } catch (e) { echo e.toString() } } } } }