salix/Jenkinsfile

267 lines
8.8 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
2024-02-04 23:10:05 +00:00
def RUN_BUILD
2024-01-30 20:49:58 +00:00
2024-02-07 12:24:50 +00:00
def BRANCH_ENV = [
test: 'test',
master: 'production',
beta: 'production'
2024-02-07 12:24:50 +00:00
]
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',
'beta'
2024-02-06 10:40:13 +00:00
].contains(env.BRANCH_NAME)
FROM_GIT = env.JOB_NAME.startsWith('gitea/')
RUN_TESTS = !PROTECTED_BRANCH && FROM_GIT
RUN_BUILD = PROTECTED_BRANCH && FROM_GIT
2024-05-27 08:07:44 +00:00
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'
2019-01-08 14:54:02 +00:00
}
2019-01-11 12:55:16 +00:00
stages {
stage('Version') {
when {
expression { RUN_BUILD }
}
steps {
script {
def packageJson = readJSON file: 'package.json'
def version = "${packageJson.version}-build${env.BUILD_ID}"
writeFile(file: 'VERSION.txt', text: version)
}
}
}
2019-02-11 23:22:59 +00:00
stage('Install') {
environment {
2024-02-06 09:20:04 +00:00
NODE_ENV = ''
2019-02-11 23:22:59 +00:00
}
parallel {
2024-02-06 09:20:04 +00:00
stage('Back') {
steps {
2024-02-06 09:20:04 +00:00
sh 'pnpm install --prefer-offline'
2024-04-19 05:27:54 +00:00
sh 'node node_modules/puppeteer/install.mjs'
}
}
2024-02-06 09:20:04 +00:00
stage('Print') {
when {
2024-02-06 09:20:04 +00:00
expression { FROM_GIT }
}
steps {
2024-02-06 09:20:04 +00:00
sh 'pnpm install --prefer-offline --prefix=print'
}
}
2024-02-06 09:20:04 +00:00
stage('Front') {
when {
2024-02-06 09:20:04 +00:00
expression { FROM_GIT }
}
steps {
2024-02-06 09:20:04 +00:00
sh 'pnpm install --prefer-offline --prefix=front'
}
2019-01-11 13:06:13 +00:00
}
2019-01-26 13:51:36 +00:00
}
}
2024-02-04 20:09:49 +00:00
stage('Stack') {
2022-05-12 14:18:08 +00:00
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 --junit'
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 }
}
2024-12-10 10:13:38 +00:00
environment {
VERSION = readFile 'VERSION.txt'
}
2024-02-04 20:17:47 +00:00
steps {
sh 'docker-compose build back'
}
2024-02-04 20:00:03 +00:00
}
2022-05-12 14:18:08 +00:00
}
2022-05-13 10:25:35 +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 }
}
2024-12-10 10:13:38 +00:00
environment {
VERSION = readFile 'VERSION.txt'
}
2024-02-04 20:17:47 +00:00
steps {
sh 'gulp build'
sh 'docker-compose build front'
}
2024-02-04 20:00:03 +00:00
}
2022-11-14 09:13:26 +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')
2024-12-10 10:13:38 +00:00
VERSION = readFile 'VERSION.txt'
2019-01-26 13:51:36 +00:00
}
steps {
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
}
2024-05-27 07:39:51 +00:00
stage('Kubernetes') {
when {
2024-02-01 20:49:42 +00:00
expression { FROM_GIT }
}
2024-12-10 10:13:38 +00:00
environment {
VERSION = readFile 'VERSION.txt'
}
steps {
2024-05-27 07:31:42 +00:00
withKubeConfig([
serverUrl: "$KUBERNETES_API",
credentialsId: 'kubernetes',
namespace: 'salix'
]) {
2024-05-27 07:37:17 +00:00
sh 'kubectl set image deployment/salix-back-$BRANCH_NAME salix-back-$BRANCH_NAME=$REGISTRY/salix-back:$VERSION'
sh 'kubectl set image deployment/salix-front-$BRANCH_NAME salix-front-$BRANCH_NAME=$REGISTRY/salix-front:$VERSION'
2024-05-27 07:31:42 +00:00
}
}
}
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)
2024-02-22 08:36:48 +00:00
setEnv()
rocketSend(
channel: 'vn-database',
message: "*DB version uploaded:* ${message}"
+"\n$COMMITTER_EMAIL ($BRANCH_NAME)"
+"\n$RUN_DISPLAY_URL",
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
}