12 Commits

Author SHA1 Message Date
jose-rZM
de9c135d4d Update env vars 2025-12-20 19:14:26 +01:00
jose-rZM
fc1a8d8ac9 Add cleanup stage 2025-12-20 18:58:29 +01:00
jose-rZM
256f1073da Add node name 2025-12-20 18:52:28 +01:00
jose-rZM
ef1f4aed71 Remove build step 2025-12-20 18:51:38 +01:00
jose-rZM
48263e6f01 Delete unused network 2025-12-20 18:45:02 +01:00
jose-rZM
1474ae1885 Add node name 2025-12-20 18:35:38 +01:00
jose-rZM
df6d68c8a0 Add node 2025-12-20 18:33:35 +01:00
jose-rZM
0e31fb2c16 Remove container name property to avoid collisions 2025-12-20 18:27:04 +01:00
jose-rZM
e7d01b1130 Fix typo 2025-12-20 18:26:25 +01:00
jose-rZM
c4b9481b1c Use docker compose 2025-12-20 18:18:41 +01:00
jose-rZM
6de3411fb5 Add Jenkins setup 2025-12-20 18:18:29 +01:00
jose-rZM
3886e3f1a0 Use two different jenkinsfile 2025-12-18 17:56:43 +01:00
6 changed files with 288 additions and 185 deletions

185
Jenkinsfile vendored
View File

@@ -1,185 +0,0 @@
pipeline {
agent none
options {
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()
}
}
}
stage('Backend: lint & test') {
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
ruff check app tests
pytest
'''
}
}
}
stage('Frontend: check & build') {
agent {
docker {
image 'node:20-slim'
}
}
steps {
dir('frontend') {
sh '''
set -e
npm install --no-progress --no-audit --prefer-offline
npm run check
npm run build
'''
}
}
}
stage('Docker: build images') {
agent any
steps {
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:latest \
-t cafeteria-backend:${BUILD_NUMBER} \
./backend
docker build \
-t cafeteria-frontend:latest \
-t cafeteria-frontend:${BUILD_NUMBER} \
./frontend
'''
}
}
stage('Deploy frontend & backend') {
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
: "${BACKEND_PORT:?Missing BACKEND_PORT}"
: "${BACKEND_HEALTH_URL:?Missing BACKEND_HEALTH_URL}"
: "${FRONTEND_PORT:?Missing FRONTEND_PORT}"
: "${FRONTEND_HEALTH_URL:?Missing FRONTEND_HEALTH_URL}"
# Backend
docker tag cafeteria-backend:latest cafeteria-backend:previous || true
docker rm -f cafeteria-backend 2>/dev/null || true
docker run -d \
--name cafeteria-backend \
-p "$BACKEND_PORT":"$BACKEND_PORT" \
cafeteria-backend:latest
sleep 5
if ! curl -fs "$BACKEND_HEALTH_URL"; then
echo "Backend failed, rollback"
docker rm -f cafeteria-backend
docker run -d \
--name cafeteria-backend \
--network ci-net \
-e JENKINS_BASE_URL \
-e JENKINS_JOB_NAME \
-e JENKINS_USER \
-e JENKINS_TOKEN \
-p "$BACKEND_PORT":"$BACKEND_PORT" \
cafeteria-backend:previous
exit 1
fi
# Frontend
docker tag cafeteria-frontend:latest cafeteria-frontend:previous || true
docker rm -f cafeteria-frontend 2>/dev/null || true
docker run -d \
--name cafeteria-frontend \
-p "$FRONTEND_PORT":80 \
cafeteria-frontend:latest
sleep 3
if ! curl -fs "$FRONTEND_HEALTH_URL"; then
echo "Frontend failed, rollback"
docker rm -f cafeteria-frontend
docker run -d \
--name cafeteria-frontend \
-p "$FRONTEND_PORT":80 \
cafeteria-frontend:previous
exit 1
fi
'''
}
}
}
}
post {
always {
sh '''
docker image prune -a -f || true
docker builder prune -a -f || true
'''
cleanWs()
}
}
}

130
Jenkinsfile.cd Normal file
View File

@@ -0,0 +1,130 @@
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
'''
}
}
}
stage('Cleanup') {
agent any
steps {
cleanWs()
}
}
}
}

88
Jenkinsfile.ci Normal file
View File

@@ -0,0 +1,88 @@
pipeline {
agent none
options {
timestamps()
}
environment {
NODE_OPTIONS = '--max_old_space_size=2048'
}
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()
}
echo "CI build for commit ${env.COMMIT_SHORT} by ${env.COMMIT_AUTHOR}"
}
}
/* =========================
BACKEND CI
========================= */
stage('Backend: lint & test') {
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
ruff check app tests
pytest
'''
}
}
}
/* =========================
FRONTEND CI
========================= */
stage('Frontend: check & build') {
agent {
docker {
image 'node:20-slim'
}
}
steps {
dir('frontend') {
sh '''
set -e
npm install --no-progress --no-audit --prefer-offline
npm run check
npm run build
'''
}
}
}
stage('Cleanup') {
agent any
steps {
cleanWs()
}
}
}
}

37
docker-compose.yml Normal file
View File

@@ -0,0 +1,37 @@
version: "3.9"
services:
backend:
build:
context: ./backend
image: cafeteria-backend:${BACKEND_TAG}
ports:
- "8000:8000"
environment:
JENKINS_BASE_URL: ${JENKINS_BASE_URL}
JENKINS_JOB_NAME: ${JENKINS_JOB_NAME}
JENKINS_USER: ${JENKINS_USER}
JENKINS_TOKEN: ${JENKINS_TOKEN}
restart: unless-stopped
healthcheck:
test:
[
"CMD",
"python",
"-c",
"import urllib.request; urllib.request.urlopen('http://localhost:8000/health').read()"
]
interval: 10s
timeout: 3s
retries: 5
frontend:
build:
context: ./frontend
image: cafeteria-frontend:${FRONTEND_TAG}
ports:
- "80:80"
depends_on:
backend:
condition: service_healthy
restart: unless-stopped

View File

@@ -0,0 +1,12 @@
FROM jenkins/jenkins:lts
USER root
RUN apt-get update \
&& apt-get install -y docker.io curl \
&& curl -L https://github.com/docker/compose/releases/download/v2.27.0/docker-compose-linux-x86_64 \
-o /usr/local/bin/docker-compose \
&& chmod +x /usr/local/bin/docker-compose \
&& rm -rf /var/lib/apt/lists/*
USER jenkins

View File

@@ -0,0 +1,21 @@
version: "3.9"
services:
jenkins:
build:
context: .
dockerfile: Dockerfile.jenkins
container_name: jenkins
ports:
- "8080:8080"
- "50000:50000"
volumes:
- jenkins_home:/var/jenkins_home
- /var/run/docker.sock:/var/run/docker.sock
group_add:
- "${DOCKER_GID}"
restart: unless-stopped
volumes:
jenkins_home: