Files
chore/Jenkinsfile
2025-12-14 23:33:15 -05:00

107 lines
3.9 KiB
Groovy

pipeline {
agent any
environment {
// Tag images with the build number so they are unique
FRONTEND_IMAGE = "chore-app-frontend:${env.BUILD_ID}"
BACKEND_IMAGE = "chore-app-backend:${env.BUILD_ID}"
FRONTEND_IMAGE_LATEST = "chore-app-frontend:latest"
BACKEND_IMAGE_LATEST = "chore-app-backend:latest"
VUE_CONTAINER_NAME = "chore-app-frontend"
FLASK_CONTAINER_NAME = "chore-app-backend"
NETWORK_NAME = "chore-app-net"
}
stages {
stage('Checkout') {
steps {
// Pulls code from your configured Git repo
checkout scm
}
}
stage('Set Version') {
steps {
script {
// Read BASE_VERSION from Python source
env.BASE_VERSION = sh(
script: "python -c \"import sys; from config.version import BASE_VERSION; print(BASE_VERSION)\"",
returnStdout: true
).trim()
echo "BASE_VERSION set to: ${env.BASE_VERSION}"
}
}
}
stage('Build Frontend (Vue) App') {
steps {
dir('web/vue-app') {
sh 'docker build -t ${FRONTEND_IMAGE} .'
sh 'docker tag ${FRONTEND_IMAGE} ${FRONTEND_IMAGE_LATEST}'
}
}
}
stage('Build Backend (Flask) App') {
steps {
dir('.') {
sh """docker build --build-arg APP_BUILD=${BUILD_NUMBER} -t chore-app-backend:${BASE_VERSION} -t chore-app-backend:${BASE_VERSION}-${BUILD_NUMBER} -t chore-app-backend:latest ."""
}
}
}
stage('Deploy') {
steps {
echo 'Stopping and removing old containers...'
sh "docker stop ${VUE_CONTAINER_NAME} || true"
sh "docker rm ${VUE_CONTAINER_NAME} || true"
sh "docker stop ${FLASK_CONTAINER_NAME} || true"
sh "docker rm ${FLASK_CONTAINER_NAME} || true"
echo 'Cleaning up and creating network...'
sh "docker network rm -f ${NETWORK_NAME} || true"
sh "docker network create ${NETWORK_NAME}"
echo "Starting new containers"
sh """
docker run -d \\
--name ${VUE_CONTAINER_NAME} \\
--network ${NETWORK_NAME} \\
-p 443:443 \\
${FRONTEND_IMAGE_LATEST}
"""
sh """
docker run -d \\
--name ${FLASK_CONTAINER_NAME} \\
--network ${NETWORK_NAME} \\
-e BUILD_NUMBER=${BUILD_NUMBER} \\
-v ${FLASK_CONTAINER_NAME}_data:/app/data \\
${BACKEND_IMAGE_LATEST}
"""
echo 'Deployment complete!'
}
}
stage('Cleanup') {
steps {
echo 'Removing old/dangling Docker images...'
// Remove all images that are not associated with a container
// This targets the untagged images created by previous builds
sh 'docker system prune -a -f'
// OPTIONAL: Remove images older than a certain time (e.g., 24 hours)
// This is useful if you want to keep the most recent successful images
// for quick rollback, but remove others.
// sh 'docker image prune -a -f --filter "until=24h"'
echo 'Docker images pruned.'
// Optional: Stop old containers and run the new ones
// Note: In production, you would push to a registry (DockerHub) instead
sh "echo 'Build Complete. Images ready: ${FRONTEND_IMAGE} and ${BACKEND_IMAGE}'"
}
}
}
}