salix/Jenkinsfile

187 lines
5.6 KiB
Plaintext
Raw Normal View History

2017-05-12 10:41:35 +00:00
#!/usr/bin/env groovy
2024-01-30 20:49:58 +00:00
def PROTECTED_BRANCH
def FROM_GIT
def RUN_TESTS
2019-01-11 12:55:16 +00:00
pipeline {
2019-01-11 13:15:34 +00:00
agent any
options {
disableConcurrentBuilds()
}
2024-01-30 22:25:14 +00:00
tools {
nodejs 'node-v20'
}
2019-01-11 12:55:16 +00:00
environment {
2019-12-23 13:33:54 +00:00
PROJECT_NAME = 'salix'
STACK_NAME = "${env.PROJECT_NAME}-${env.BRANCH_NAME}"
2019-01-08 14:54:02 +00:00
}
2019-01-11 12:55:16 +00:00
stages {
2019-01-11 13:15:34 +00:00
stage('Checkout') {
2019-01-11 13:01:48 +00:00
steps {
2019-01-11 13:02:34 +00:00
script {
switch (env.BRANCH_NAME) {
case 'dev':
env.NODE_ENV = 'dev'
env.BACK_REPLICAS = 1
2019-02-13 13:01:37 +00:00
break
case 'test':
2019-01-11 13:02:34 +00:00
env.NODE_ENV = 'test'
2020-11-30 18:20:52 +00:00
env.BACK_REPLICAS = 2
2019-02-13 13:01:37 +00:00
break
case 'master':
env.NODE_ENV = 'production'
env.BACK_REPLICAS = 4
2024-01-30 18:05:55 +00:00
break
2019-01-11 13:02:34 +00:00
}
def packageJson = readJSON file: 'package.json'
env.VERSION = packageJson.version
env.GIT_COMMIT_MSG = sh(
script: 'git log -1 --pretty=%B ${GIT_COMMIT}',
returnStdout: true
).trim()
2024-01-30 20:49:58 +00:00
PROTECTED_BRANCH = [
'dev',
'test',
'master'
].contains(env.BRANCH_NAME)
FROM_GIT = JOB_NAME.startsWith('gitea/')
RUN_TESTS = !PROTECTED_BRANCH && FROM_GIT
2019-01-11 13:02:34 +00:00
}
2021-04-29 22:39:44 +00:00
setEnv()
2019-01-11 12:55:16 +00:00
}
2018-10-24 12:10:41 +00:00
}
2019-02-11 23:22:59 +00:00
stage('Install') {
environment {
NODE_ENV = ""
}
parallel {
stage('Backend') {
steps {
sh 'npm install --no-audit --prefer-offline'
}
}
stage('Frontend') {
when {
expression { return FROM_GIT }
}
steps {
sh 'npm install --no-audit --prefer-offline --prefix=front'
}
}
stage('Print') {
when {
expression { return FROM_GIT }
}
steps {
sh 'npm install --no-audit --prefer-offline --prefix=print'
}
2019-01-11 13:06:13 +00:00
}
2019-01-26 13:51:36 +00:00
}
}
2022-05-12 14:18:08 +00:00
stage('Test') {
when {
2024-01-30 20:49:58 +00:00
expression { return RUN_TESTS }
}
2019-01-26 13:53:32 +00:00
environment {
2019-01-26 14:01:06 +00:00
NODE_ENV = ""
2023-04-28 17:53:13 +00:00
TZ = 'Europe/Madrid'
2019-01-26 13:53:32 +00:00
}
2022-05-12 14:18:08 +00:00
parallel {
stage('Backend') {
2022-05-12 14:18:08 +00:00
steps {
sh 'npm run test:back:ci'
2022-05-12 14:18:08 +00:00
}
2022-05-13 10:25:35 +00:00
}
stage('Frontend') {
2022-11-14 09:13:26 +00:00
steps {
sh 'jest --ci --reporters=default --reporters=jest-junit --maxWorkers=6'
2022-11-14 09:13:26 +00:00
}
}
2022-10-27 07:26:02 +00:00
}
2019-01-26 13:51:36 +00:00
}
2019-02-11 23:24:08 +00:00
stage('Build') {
when {
2024-01-30 20:49:58 +00:00
expression { return PROTECTED_BRANCH && FROM_GIT }
}
2019-01-26 13:51:36 +00:00
environment {
2021-04-29 22:39:44 +00:00
CREDENTIALS = credentials('docker-registry')
2019-01-26 13:51:36 +00:00
}
steps {
sh 'gulp build'
2021-04-29 22:39:44 +00:00
dockerBuild()
2019-01-02 00:16:28 +00:00
}
2019-01-11 08:35:26 +00:00
}
2019-01-11 13:15:34 +00:00
stage('Deploy') {
when {
2024-01-30 20:49:58 +00:00
expression { return 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'
}
sh 'npx myt push $NODE_ENV --force --commit'
}
2019-03-20 11:21:32 +00:00
}
stage('Docker') {
when {
2024-01-30 20:49:58 +00:00
expression { return FROM_GIT }
}
environment {
DOCKER_HOST = "${env.SWARM_HOST}"
}
steps {
sh "docker stack deploy --with-registry-auth --compose-file docker-compose.yml ${env.STACK_NAME}"
}
}
2019-03-20 11:21:32 +00:00
}
}
2018-10-24 12:41:38 +00:00
}
2019-01-11 12:55:16 +00:00
post {
always {
script {
2024-01-30 20:49:58 +00:00
if (RUN_TESTS) {
try {
junit 'junitresults.xml'
junit 'junit.xml'
} catch (e) {
echo e.toString()
}
}
}
}
success {
script {
2024-01-30 20:49:58 +00:00
if (env.BRANCH_NAME == 'master' && FROM_GIT) {
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()
}
2019-01-08 15:40:46 +00:00
}
2019-01-08 15:44:43 +00:00
}