Files
chore/Jenkinsfile
2025-12-10 15:25:04 -05:00

90 lines
3.0 KiB
Groovy

pipeline {
agent any
environment {
// Tag images with the build number so they are unique
FRONTEND_IMAGE = "chore-app-backend:${env.BUILD_ID}"
BACKEND_IMAGE = "chore-app-frontend:${env.BUILD_ID}"
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('Build Frontend (Vue) App') {
steps {
dir('web/vue-app') {
sh 'docker build -t ${FRONTEND_IMAGE} .'
}
}
}
stage('Build Backend (Flask) App') {
steps {
dir('.') {
sh 'docker build -t ${BACKEND_IMAGE} .'
}
}
}
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}
"""
sh """
docker run -d \\
--name ${FLASK_CONTAINER_NAME} \\
--network ${NETWORK_NAME} \\
${BACKEND_IMAGE}
"""
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 -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}'"
}
}
}
}