pipeline { agent { label 'shark-wrangler' } environment { REPO_URL = 'https://github.com/dnhkng/GlaDOS.git' IMAGE_NAME = 'glados-app' // You can customize this if needed CONTAINER_NAME = 'glados-voice' HOST_PORT = 7089 CONTAINER_PORT = 5050 // This is the port exposed by the Dockerfile and used in docker-compose } stages { stage('Checkout') { steps { script { echo "Cloning repository: ${env.REPO_URL}" git url: env.REPO_URL, branch: 'main' // Assuming main branch, adjust if needed } } } stage('Build Docker Image') { steps { script { echo "Building Docker image: ${env.IMAGE_NAME}" // Using the Dockerfile from the cloned repo docker.build(env.IMAGE_NAME, '.') } } } stage('Stop and Remove Previous Container (if exists)') { steps { script { echo "Checking for existing container: ${env.CONTAINER_NAME}" // Use '|| true' to prevent failure if container doesn't exist sh "docker ps -a --filter name=${env.CONTAINER_NAME} -q | xargs -r docker stop || true" sh "docker ps -a --filter name=${env.CONTAINER_NAME} -q | xargs -r docker rm || true" } } } stage('Run Docker Container') { steps { script { echo "Running Docker container: ${env.CONTAINER_NAME}" echo "Mapping host port ${env.HOST_PORT} to container port ${env.CONTAINER_PORT}" // Run in detached mode (-d) // Use the image built in the previous stage // Set the container name // Map the ports sh "docker run -d --restart=always --name ${env.CONTAINER_NAME} -p ${env.HOST_PORT}:${env.CONTAINER_PORT} ${env.IMAGE_NAME}" echo "Container ${env.CONTAINER_NAME} should be running." echo "You can check its status with: docker ps -f name=${env.CONTAINER_NAME}" echo "Access the application (if it's a web service) at http://:${env.HOST_PORT}" } } } } post { always { echo 'Pipeline finished.' // Optional: Clean up workspace // cleanWs() } success { echo "Pipeline executed successfully!" } failure { echo "Pipeline failed!" } } }