pipeline { agent any environment { DOCKER_HUB_USERNAME = 'aimodocker' // This creates DOCKER_AUTH_USR and DOCKER_AUTH_PSW DOCKER_AUTH = credentials('aimodocker') IMAGE_NAME = 'lcca-service' SERVICE_NAME = 'ahm-app' SECURITY_PREFIX = 'security' // Initialize variables to be updated in script blocks GIT_COMMIT_HASH = "" IMAGE_TAG = "" SECONDARY_TAG = "" } stages { stage('Checkout & Setup') { steps { script { checkout scm GIT_COMMIT_HASH = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim() // Use env.BRANCH_NAME or logic to handle detached HEAD if necessary def branch = env.BRANCH_NAME ?: 'unknown' echo "Current Branch: ${branch}" if (branch == 'main') { IMAGE_TAG = GIT_COMMIT_HASH SECONDARY_TAG = 'latest' } else if (branch == 'lcca_security') { IMAGE_TAG = "${SECURITY_PREFIX}-${GIT_COMMIT_HASH}" SECONDARY_TAG = "${SECURITY_PREFIX}-latest" } else { IMAGE_TAG = "temp-${GIT_COMMIT_HASH}" SECONDARY_TAG = "" // Ensure it's empty for other branches } echo "Primary Tag: ${IMAGE_TAG}" } } } // stage('Run Unit Tests') { // steps { // sh 'poetry run pytest tests/unit' // } // } // stage('Run E2E Tests') { // steps { // sh 'poetry run pytest tests/e2e' // } // } stage('Docker Login') { steps { // Fixed variable names based on the 'DOCKER_AUTH' environment key sh "echo ${DOCKER_AUTH_PSW} | docker login -u ${DOCKER_AUTH_USR} --password-stdin" } } stage('Build & Tag') { steps { script { def fullImageName = "${DOCKER_HUB_USERNAME}/${IMAGE_NAME}" sh "docker build -t ${fullImageName}:${IMAGE_TAG} ." if (SECONDARY_TAG) { sh "docker tag ${fullImageName}:${IMAGE_TAG} ${fullImageName}:${SECONDARY_TAG}" } } } } stage('Push to Docker Hub') { steps { script { def fullImageName = "${DOCKER_HUB_USERNAME}/${IMAGE_NAME}" sh "docker push ${fullImageName}:${IMAGE_TAG}" if (SECONDARY_TAG) { sh "docker push ${fullImageName}:${SECONDARY_TAG}" } } } } } post { always { script { sh 'docker logout' def fullImageName = "${DOCKER_HUB_USERNAME}/${IMAGE_NAME}" // Clean up images to save agent disk space sh "docker rmi ${fullImageName}:${IMAGE_TAG} || true" if (SECONDARY_TAG) { sh "docker rmi ${fullImageName}:${SECONDARY_TAG} || true" } } } success { echo "Successfully processed ${env.BRANCH_NAME}." } } }