salix/Jenkinsfile

188 lines
5.9 KiB
Groovy

#!/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 {
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.FROM_GIT = env.JOB_NAME.startsWith('gitea/')
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()
}
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 {
expression { return env.FROM_GIT }
expression { return !env.PROTECTED_BRANCH }
}
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 {
expression { return env.FROM_GIT }
expression { return env.PROTECTED_BRANCH }
}
environment {
CREDENTIALS = credentials('docker-registry')
}
steps {
nodejs('node-v20') {
sh 'gulp build'
}
dockerBuild()
}
}
stage('Deploy') {
when {
expression { return env.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'
}
nodejs('node-v20') {
sh 'npx myt push $NODE_ENV --force --commit'
}
}
}
stage('Docker') {
when {
expression { return env.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 {
success {
script {
if (env.FROM_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
)
}
}
}
unsuccessful {
sendEmail()
}
always {
script {
if (env.FROM_GIT && !env.PROTECTED_BRANCH) {
try {
junit 'junitresults.xml'
junit 'junit.xml'
} catch (e) {
echo e.toString()
}
}
}
}
}
}