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" } 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...' // 1. Stop and remove the old running containers (if they exist) // The || true prevents the build from failing if the container doesn't exist yet 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 'Starting new containers named ${VUE_CONTAINER_NAME} and ${FLASK_CONTAINER_NAME}... from images ${FRONTEND_IMAGE} and ${BACKEND_IMAGE}' // 2. Start the newly built images // You must customize the ports (-p) and environment variables (-e) as needed sh """ docker run -d \\ --name ${VUE_CONTAINER_NAME} \\ --network chore-app-net \\ -p 443:443 \\ ${VUE_IMAGE} """ sh """ docker run -d \\ --name ${FLASK_CONTAINER_NAME} \\ --network chore-app-net \\ ${FLASK_IMAGE} """ echo 'Deployment complete!' } } stage('Cleanup') { steps { // 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}'" } } } }