salix/Jenkinsfile

254 lines
8.4 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
2024-02-01 21:06:30 +00:00
def PROTECTED_BRANCH
def FROM_GIT
def RUN_TESTS
2024-02-04 23:10:05 +00:00
def RUN_BUILD
2024-02-01 21:06:30 +00:00
2024-02-07 12:24:50 +00:00
def BRANCH_ENV = [
test: 'test',
master: 'production'
]
2024-02-06 09:20:04 +00:00
node {
stage('Setup') {
2024-02-07 12:24:50 +00:00
env.BACK_REPLICAS = 1
env.NODE_ENV = BRANCH_ENV[env.BRANCH_NAME] ?: 'dev'
2024-02-06 10:40:13 +00:00
PROTECTED_BRANCH = [
'dev',
'test',
'master'
].contains(env.BRANCH_NAME)
FROM_GIT = env.JOB_NAME.startsWith('gitea/')
RUN_TESTS = !PROTECTED_BRANCH && FROM_GIT
RUN_BUILD = PROTECTED_BRANCH && FROM_GIT
2024-02-06 11:16:49 +00:00
// https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#using-environment-variables
2024-02-06 11:21:06 +00:00
echo "NODE_NAME: ${env.NODE_NAME}"
echo "WORKSPACE: ${env.WORKSPACE}"
2024-02-06 11:16:49 +00:00
2024-02-07 12:56:21 +00:00
configFileProvider([
configFile(fileId: 'salix.properties',
variable: 'PROPS_FILE')
]) {
def props = readProperties file: PROPS_FILE
props.each {key, value -> env."${key}" = value }
props.each {key, value -> echo "${key}: ${value}" }
}
if (PROTECTED_BRANCH) {
configFileProvider([
2024-02-07 12:56:21 +00:00
configFile(fileId: "salix.branch.${env.BRANCH_NAME}",
variable: 'BRANCH_PROPS_FILE')
]) {
2024-02-07 12:56:21 +00:00
def props = readProperties file: BRANCH_PROPS_FILE
2024-02-06 11:17:45 +00:00
props.each {key, value -> env."${key}" = value }
2024-02-06 11:16:49 +00:00
props.each {key, value -> echo "${key}: ${value}" }
}
2024-02-06 09:20:04 +00:00
}
}
2024-02-01 21:13:36 +00:00
}
2019-01-11 12:55:16 +00:00
pipeline {
2019-01-11 13:15:34 +00:00
agent any
options {
disableConcurrentBuilds()
}
2024-02-01 13:07:42 +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 {
2024-02-06 09:20:04 +00:00
stage('Install') {
environment {
NODE_ENV = ''
}
parallel {
stage('Back') {
steps {
sh 'pnpm install --prefer-offline'
}
}
stage('Print') {
when {
expression { FROM_GIT }
}
steps {
sh 'pnpm install --prefer-offline --prefix=print'
}
}
stage('Front') {
when {
expression { FROM_GIT }
}
steps {
sh 'pnpm install --prefer-offline --prefix=front'
}
}
}
}
2024-02-04 20:09:49 +00:00
stage('Stack') {
parallel {
stage('Back') {
2024-02-04 20:17:47 +00:00
stages {
stage('Test') {
when {
expression { RUN_TESTS }
}
environment {
2024-02-06 09:20:04 +00:00
NODE_ENV = ''
2024-02-04 20:17:47 +00:00
}
steps {
sh 'node back/tests.js --ci --junit --network jenkins'
2024-02-04 20:17:47 +00:00
}
post {
always {
2024-02-06 20:51:15 +00:00
junit(
testResults: 'junitresults.xml',
allowEmptyResults: true
)
2024-02-04 20:00:03 +00:00
}
}
}
2024-02-04 20:17:47 +00:00
stage('Build') {
when {
expression { RUN_BUILD }
}
steps {
2024-02-05 10:38:16 +00:00
script {
def packageJson = readJSON file: 'package.json'
env.VERSION = packageJson.version
}
2024-02-04 20:17:47 +00:00
sh 'docker-compose build back'
}
2024-02-04 20:00:03 +00:00
}
}
2024-02-04 20:17:47 +00:00
}
stage('Front') {
2024-02-04 20:17:47 +00:00
when {
expression { FROM_GIT }
}
2024-02-04 20:17:47 +00:00
stages {
stage('Test') {
when {
expression { RUN_TESTS }
}
environment {
2024-02-06 09:20:04 +00:00
NODE_ENV = ''
2024-02-04 20:17:47 +00:00
}
steps {
sh 'jest --ci --reporters=default --reporters=jest-junit --maxWorkers=10'
}
2024-02-06 20:51:15 +00:00
post {
always {
junit(
testResults: 'junit.xml',
allowEmptyResults: true
)
}
}
2024-02-04 20:00:03 +00:00
}
2024-02-04 20:17:47 +00:00
stage('Build') {
when {
expression { RUN_BUILD }
}
steps {
2024-02-05 10:38:16 +00:00
script {
def packageJson = readJSON file: 'package.json'
env.VERSION = packageJson.version
}
2024-02-04 20:17:47 +00:00
sh 'gulp build'
sh 'docker-compose build front'
}
2024-02-04 20:00:03 +00:00
}
2022-11-14 09:13:26 +00:00
}
2024-02-04 20:17:47 +00:00
}
2022-10-27 07:26:02 +00:00
}
2019-01-26 13:51:36 +00:00
}
2024-02-04 20:09:49 +00:00
stage('Push') {
when {
2024-02-04 20:00:03 +00:00
expression { RUN_BUILD }
}
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 {
2024-02-02 13:27:36 +00:00
script {
def packageJson = readJSON file: 'package.json'
env.VERSION = packageJson.version
}
2024-02-04 20:09:49 +00:00
sh 'docker login --username $CREDENTIALS_USR --password $CREDENTIALS_PSW $REGISTRY'
sh 'docker-compose push'
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-02-01 20:49:42 +00:00
expression { 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-02-01 20:49:42 +00:00
expression { FROM_GIT }
}
environment {
DOCKER_HOST = "${env.SWARM_HOST}"
}
steps {
2024-02-02 13:27:36 +00:00
script {
def packageJson = readJSON file: 'package.json'
env.VERSION = packageJson.version
}
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 {
success {
script {
2024-01-30 20:49:58 +00:00
if (env.BRANCH_NAME == 'master' && FROM_GIT) {
2024-02-02 13:27:36 +00:00
env.GIT_COMMIT_MSG = sh(
script: 'git log -1 --pretty=%B ${GIT_COMMIT}',
returnStdout: true
).trim()
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 {
2024-02-02 13:27:36 +00:00
setEnv()
sendEmail()
}
2019-01-08 15:40:46 +00:00
}
2019-01-08 15:44:43 +00:00
}