salix/Jenkinsfile

168 lines
5.1 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.FRONT_PORT = SALIX_MASTER_FRONT_PORT
env.BACK_PORT = SALIX_MASTER_BACK_PORT
break
case 'test':
env.FRONT_PORT = SALIX_TEST_FRONT_PORT
env.BACK_PORT = SALIX_TEST_BACK_PORT
break
}
*/
switch (env.BRANCH_NAME) {
case 'master':
env.NODE_ENV = 'production'
break
case 'test':
env.NODE_ENV = 'test'
break
}
}
configFileProvider([
configFile(fileId: "salix.groovy",
variable: 'ENV_FILE')
]) {
load "${env.ENV_FILE}"
}
configFileProvider([
configFile(fileId: "salix.${env.NODE_ENV}.groovy",
variable: 'LOCAL_ENV_FILE')
]) {
load "${env.LOCAL_ENV_FILE}"
}
sh 'printenv'
}
}
stage('Install') {
environment {
NODE_ENV = ""
}
steps {
nodejs('node-lts') {
sh 'npm install --no-audit'
sh 'gulp install'
}
}
}
stage('Test') {
when { anyOf {
branch 'dev'
}}
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 backTestDockerOnce --junit --random'
}
}
}
}
}
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()
}
}
}
}
}