salix/Jenkinsfile

154 lines
4.7 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 'master':
env.NODE_ENV = 'production'
env.BACK_REPLICAS = 4
break
case 'test':
env.NODE_ENV = 'test'
env.BACK_REPLICAS = 2
break
}
}
configFileProvider([
configFile(fileId: "salix.groovy",
variable: 'GROOVY_FILE')
]) {
load env.GROOVY_FILE
}
setEnv()
}
}
stage('Install') {
environment {
NODE_ENV = ""
}
steps {
sh "docker network ls | grep testing-\${env.STACK_NAME} | awk '{print \$1}' | xargs -r docker network rm"
sh "docker-compose -p testing-${env.STACK_NAME} -f docker-compose.test.yml rm --stop"
sh "docker-compose -p testing-${env.STACK_NAME} -f docker-compose.test.yml build --force-rm db"
}
}
stage('Test') {
when { not { anyOf {
branch 'test'
branch 'master'
}}}
environment {
NODE_ENV = ""
TZ = 'Europe/Madrid'
}
parallel {
stage('Frontend') {
steps {
sh "docker-compose -p testing-${env.STACK_NAME} -f docker-compose.test.yml run --rm front"
}
}
stage('Backend') {
steps {
sh "docker-compose -p testing-${env.STACK_NAME} -f docker-compose.test.yml run --rm back"
}
}
}
}
stage('ClearTesting') {
when { not { anyOf {
branch 'test'
branch 'master'
}}}
environment {
NODE_ENV = ""
TZ = 'Europe/Madrid'
}
steps {
sh "docker-compose -p testing-${env.STACK_NAME} -f docker-compose.test.yml rm -s -f"
// sh "docker-compose -p salix-testing -f docker-compose.test.yml run --rm front"
}
}
stage('Build') {
when { anyOf {
branch 'test'
branch 'master'
}}
environment {
CREDENTIALS = credentials('docker-registry')
}
steps {
nodejs('node-v20') {
sh 'gulp build'
}
dockerBuild()
}
}
stage('Deploy') {
when { anyOf {
branch 'test'
branch 'master'
}}
environment {
DOCKER_HOST = "${env.SWARM_HOST}"
}
steps {
sh "docker stack deploy --with-registry-auth --compose-file docker-compose.yml ${env.STACK_NAME}"
}
}
stage('Database') {
when { anyOf {
branch 'test'
branch 'master'
}}
steps {
configFileProvider([
configFile(fileId: "config.${env.NODE_ENV}.ini",
variable: 'MYSQL_CONFIG')
]) {
sh 'cp "$MYSQL_CONFIG" db/config.$NODE_ENV.ini'
}
sh 'db/import-changes.sh -f $NODE_ENV'
}
}
}
post {
always {
script {
if (!['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()
}
}
}
}
}