pipeline { agent none options { timestamps() } environment { NODE_OPTIONS = '--max_old_space_size=2048' APP_VERSION = "1.0.${BUILD_NUMBER}" DOCKER_BUILDKIT = '1' } 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 \ -t cafeteria-frontend:${BUILD_NUMBER} \ -t cafeteria-frontend:latest \ ./frontend ''' } } /* ========================= DEPLOY ========================= */ stage('Deploy (docker compose)') { when { branch 'main' } agent any environment { JENKINS_BASE_URL = 'http://jenkins:8080' JENKINS_JOB_NAME = 'Espetos' } steps { withCredentials([ usernamePassword( credentialsId: 'jenkins-api-token', usernameVariable: 'JENKINS_USER', passwordVariable: 'JENKINS_TOKEN' ) ]) { sh ''' set -e echo "Deploying backend ${BUILD_NUMBER}" echo "BACKEND_TAG=${BUILD_NUMBER}" > .env echo "FRONTEND_TAG=${BUILD_NUMBER}" >> .env docker compose up -d ''' } } } } post { always { cleanWs() } } }