Update Jenkinsfile

This commit is contained in:
jose-rZM
2025-12-15 13:55:47 +01:00
parent 1085b42e54
commit 741fc78b96

261
Jenkinsfile vendored
View File

@@ -1,199 +1,128 @@
pipeline { pipeline {
agent none agent none
options { options {
timestamps() timestamps()
}
environment {
CI = 'true'
NODE_OPTIONS = '--max_old_space_size=2048'
APP_VERSION = "1.0.${BUILD_NUMBER}"
DOCKER_BUILDKIT = '1'
}
stages {
stage('Init') {
agent any
steps {
script {
env.COMMIT_AUTHOR = sh(
script: "git show -s --format=%an HEAD",
returnStdout: true
).trim()
env.COMMIT_SHORT = sh(
script: "git rev-parse --short HEAD",
returnStdout: true
).trim()
}
}
} }
environment { stage('Backend: lint & test') {
CI = 'true' agent {
NODE_OPTIONS = '--max_old_space_size=2048' docker {
image 'python:3.11-slim'
APP_VERSION = "1.0.${BUILD_NUMBER}" args '-u root'
}
}
steps {
dir('backend') {
sh '''
set -e
python -m venv .venv
. .venv/bin/activate
pip install --upgrade pip
pip install -r requirements-dev.txt
ruff check app tests
pytest
'''
}
}
} }
stages { stage('Frontend: check & build') {
agent {
stage('Init') { docker {
agent any image 'node:20-slim'
steps {
script {
env.COMMIT_AUTHOR = sh(
script: "git show -s --format=%an HEAD",
returnStdout: true
).trim()
env.COMMIT_SHORT = sh(
script: "git rev-parse --short HEAD",
returnStdout: true
).trim()
}
echo "Last commit: ${env.COMMIT_AUTHOR}"
} }
} }
steps {
dir('frontend') {
/* ========================= sh '''
BACKEND (Python) set -e
========================= */ npm install --no-progress --no-audit --prefer-offline
npm run check
stage('Backend: deps') { npm run build
agent { '''
docker {
image 'python:3.12'
args '-u root'
}
}
steps {
dir('backend') {
sh '''
set -e
python --version
python -m venv .venv
. .venv/bin/activate
pip install --upgrade pip
pip install -r requirements-dev.txt
'''
}
} }
} }
}
stage('Backend: lint & test') { stage('Docker: build images') {
agent {
docker {
image 'python:3.12'
args '-u root'
}
}
steps {
dir('backend') {
sh '''
set -e
. .venv/bin/activate
ruff check app tests
pytest
'''
}
}
}
/* =========================
FRONTEND (Node)
========================= */
stage('Frontend: deps') {
agent {
docker {
image 'node:20'
}
}
steps {
dir('frontend') {
sh '''
set -e
node --version
npm --version
npm install --no-progress --no-audit --prefer-offline
'''
}
}
}
stage('Frontend: check & test') {
agent {
docker {
image 'node:20'
}
}
steps {
dir('frontend') {
sh '''
set -e
npm run check
'''
}
}
}
stage('Frontend: build') {
agent {
docker {
image 'node:20'
}
}
steps {
dir('frontend') {
sh '''
set -e
npm run build
'''
}
}
}
/* =========================
DOCKER
========================= */
stage('Docker: build images') {
when {
expression {
fileExists('backend/Dockerfile') &&
fileExists('frontend/Dockerfile')
}
}
agent any agent any
steps { steps {
sh ''' sh '''
set -e set -e
docker version
docker build \ docker build \
--build-arg APP_VERSION=${APP_VERSION} \ --build-arg APP_VERSION=${APP_VERSION} \
--build-arg GIT_COMMIT=${COMMIT_SHORT} \ --build-arg GIT_COMMIT=${COMMIT_SHORT} \
--build-arg COMMIT_AUTHOR="${COMMIT_AUTHOR}" \ --build-arg COMMIT_AUTHOR="${COMMIT_AUTHOR}" \
--build-arg BUILD_NUMBER=${BUILD_NUMBER} \ --build-arg BUILD_NUMBER=${BUILD_NUMBER} \
-t cafeteria-backend:${BUILD_NUMBER} ./backend -t cafeteria-backend:latest \
-t cafeteria-backend:${BUILD_NUMBER} \
./backend
docker build \ docker build \
-t cafeteria-frontend:${BUILD_NUMBER} ./frontend -t cafeteria-frontend:latest \
-t cafeteria-frontend:${BUILD_NUMBER} \
./frontend
''' '''
} }
} }
stage('Deploy frontend & backend') { stage('Deploy frontend & backend') {
agent any agent any
steps { steps {
sh ''' sh '''
set -e set -e
echo "Deploying build ${BUILD_NUMBER}" docker rm -f cafeteria-backend cafeteria-frontend 2>/dev/null || true
docker rm -f cafeteria-backend cafeteria-frontend 2>/dev/null || true docker run -d \
--name cafeteria-backend \
-p 8000:8000 \
cafeteria-backend:latest
docker run -d \
docker run -d \ --name cafeteria-frontend \
--name cafeteria-backend \ -p 3000:80 \
-p 8000:8000 \ cafeteria-frontend:latest
cafeteria-backend:${BUILD_NUMBER} '''
docker run -d \
--name cafeteria-frontend \
-p 3000:80 \
cafeteria-frontend:${BUILD_NUMBER}
'''
}
}
}
post {
always {
script {
node {
cleanWs()
}
}
} }
} }
} }
post {
always {
sh '''
docker image prune -a -f || true
docker builder prune -a -f || true
'''
cleanWs()
}
}
}