salix/Jenkinsfile

151 lines
4.4 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 {
if (!env.GIT_COMMITTER_EMAIL) {
env.COMMITTER_EMAIL = sh(
script: 'git --no-pager show -s --format="%ae"',
returnStdout: true
).trim()
} else {
env.COMMITTER_EMAIL = env.GIT_COMMITTER_EMAIL;
}
switch (env.BRANCH_NAME) {
case 'master':
env.NODE_ENV = 'production'
break
case 'test':
env.NODE_ENV = 'test'
break
}
}
configFileProvider([
configFile(fileId: "salix.groovy",
variable: 'GROOVY_FILE')
]) {
load env.GROOVY_FILE
}
sh 'printenv'
}
}
stage('Install') {
environment {
NODE_ENV = ""
}
steps {
nodejs('node-lts') {
sh 'npm install --no-audit --prefer-offline'
sh 'gulp install --ci'
}
}
}
stage('Test') {
when { not { anyOf {
branch 'test'
branch 'master'
}}}
environment {
NODE_ENV = ""
}
parallel {
stage('Frontend') {
steps {
nodejs('node-lts') {
sh 'jest --ci --reporters=default --reporters=jest-junit --maxWorkers=1'
}
}
}
/* stage('Backend') {
steps {
nodejs('node-lts') {
sh 'gulp backTestOnce --ci'
}
}
} */
}
}
stage('Build') {
when { anyOf {
branch 'test'
branch 'master'
}}
environment {
CREDS = credentials('docker-registry')
}
steps {
nodejs('node-lts') {
sh 'gulp build'
}
sh 'docker login --username $CREDS_USR --password $CREDS_PSW $REGISTRY'
sh 'docker-compose build --parallel'
sh 'docker-compose push'
}
}
stage('Deploy') {
when { anyOf {
branch 'test'
branch 'master'
}}
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) 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()
}
}
}
}
}