Files
TallerCiCd/Jenkinsfile.cd

148 lines
3.2 KiB
Plaintext

pipeline {
agent none
options {
timestamps()
}
parameters {
string(
name: 'JENKINS_BASE_URL',
defaultValue: 'https://openbokeron.org/jenkins',
description: 'Base URL del Jenkins objetivo'
)
string(
name: 'JENKINS_JOB_NAME',
defaultValue: 'TallerCiCd',
description: 'Nombre del job que se consulta en Jenkins'
)
string(
name: 'VITE_API_BASE',
defaultValue: '/taller/api',
description: 'Base path/API para el frontend (build time)'
)
}
environment {
NODE_OPTIONS = '--max_old_space_size=2048'
APP_VERSION = "1.0.${BUILD_NUMBER}"
DOCKER_BUILDKIT = '1'
JENKINS_BASE_URL = "${params.JENKINS_BASE_URL}"
JENKINS_JOB_NAME = "${params.JENKINS_JOB_NAME}"
VITE_API_BASE = "${params.VITE_API_BASE}"
}
stages {
/* =========================
TESTS
========================= */
stage('Backend: test (main)') {
when {
branch 'main'
}
agent {
docker {
image 'python:3.11-slim'
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
pytest
'''
}
}
}
/* =========================
DOCKER BUILD
========================= */
stage('Docker: build images') {
when {
branch 'main'
}
agent any
steps {
script {
env.COMMIT_SHORT = sh(
script: "git rev-parse --short HEAD",
returnStdout: true
).trim()
env.COMMIT_AUTHOR = sh(
script: "git show -s --format=%an HEAD",
returnStdout: true
).trim()
}
sh '''
set -e
docker build \
--build-arg APP_VERSION=${APP_VERSION} \
--build-arg GIT_COMMIT=${COMMIT_SHORT} \
--build-arg COMMIT_AUTHOR="${COMMIT_AUTHOR}" \
--build-arg BUILD_NUMBER=${BUILD_NUMBER} \
-t cafeteria-backend:${BUILD_NUMBER} \
-t cafeteria-backend:latest \
./backend
docker build \
--build-arg VITE_API_BASE=${VITE_API_BASE} \
-t cafeteria-frontend:${BUILD_NUMBER} \
-t cafeteria-frontend:latest \
./frontend
'''
}
}
/* =========================
DEPLOY
========================= */
stage('Deploy (docker compose)') {
when {
branch 'main'
}
agent any
steps {
withCredentials([
usernamePassword(
credentialsId: 'jenkins-api-token',
usernameVariable: 'JENKINS_USER',
passwordVariable: 'JENKINS_TOKEN'
)
]) {
sh '''
set -e
echo "Deploying backend ${BUILD_NUMBER}"
BACKEND_TAG=${BUILD_NUMBER} FRONTEND_TAG=${BUILD_NUMBER} docker-compose up -d
'''
}
}
}
stage('Cleanup') {
agent any
steps {
cleanWs()
}
}
}
}