diff --git a/Jenkinsfile b/Jenkinsfile index 8469ab7..8d1b30d 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -21,7 +21,7 @@ pipeline { stage('Install yq') { steps { sh ''' - if ! command -v yq &> /dev/null; then + if ! [ -x /usr/local/bin/yq ]; then echo "Installing yq..." wget https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 -O /usr/local/bin/yq chmod +x /usr/local/bin/yq @@ -34,11 +34,13 @@ pipeline { stage('Build and Push Docker Images') { steps { script { - def tag = "build-${env.BUILD_NUMBER}" - echo "Building and pushing images with tag: ${tag}" + def gitBranch = env.BRANCH_NAME ?: sh(script: "git rev-parse --abbrev-ref HEAD", returnStdout: true).trim() + def tag = "${gitBranch}-build-${env.BUILD_NUMBER}" + echo "🛠 Building and pushing Docker images with tag: ${tag}" withCredentials([usernamePassword(credentialsId: env.DOCKER_CRED_ID, usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) { sh """ + set -e echo "\$DOCKER_PASS" | docker login -u "\$DOCKER_USER" --password-stdin docker build -t ${REGISTRY}/${APP_NAME}-be:${tag} ./employee-be @@ -60,40 +62,54 @@ pipeline { steps { script { def envList = ["dev", "stag", "prod"] + def parallelStages = [:] - withCredentials([usernamePassword(credentialsId: env.MANIFEST_CRED_ID, usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) { - envList.each { overlayEnv -> - echo "🔹 Updating manifest for environment: ${overlayEnv}" + envList.each { overlayEnv -> + parallelStages[overlayEnv] = { + echo "Starting manifest update for environment: ${overlayEnv}" - sh """ - echo "Cloning branch ${overlayEnv}..." - rm -rf ${MANIFEST_DIR} - git clone --single-branch --branch ${overlayEnv} https://\$GIT_USER:\$GIT_PASS@${env.MANIFEST_REPO.replace('https://', '')} ${MANIFEST_DIR} || \ - (git clone https://\$GIT_USER:\$GIT_PASS@${env.MANIFEST_REPO.replace('https://', '')} ${MANIFEST_DIR} && cd ${MANIFEST_DIR} && git checkout -b ${overlayEnv}) - """ - - dir(env.MANIFEST_DIR) { + withCredentials([usernamePassword(credentialsId: env.MANIFEST_CRED_ID, usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) { sh """ - echo "Updating image tags for ${overlayEnv}..." - yq e -i '(.spec.template.spec.containers[] | select(.name == "backend") | .image) = "${REGISTRY}/${APP_NAME}-be:${overlayEnv}-${env.IMAGE_TAG_FINAL}"' overlays/${overlayEnv}/patch-deployment.yaml - yq e -i '(.spec.template.spec.containers[] | select(.name == "frontend") | .image) = "${REGISTRY}/${APP_NAME}-fe:${overlayEnv}-${env.IMAGE_TAG_FINAL}"' overlays/${overlayEnv}/patch-deployment.yaml - - git reset -- overlays/*/ - git add overlays/${overlayEnv}/ - git commit -m "chore(${overlayEnv}): update image tags to ${overlayEnv}-${env.IMAGE_TAG_FINAL}" || echo "No changes to commit" - - git push https://\$GIT_USER:\$GIT_PASS@${env.MANIFEST_REPO.replace('https://', '')} ${overlayEnv} + set -e + echo "Cloning manifest branch: ${overlayEnv}" + rm -rf ${MANIFEST_DIR}-${overlayEnv} + git clone --single-branch --branch ${overlayEnv} https://\$GIT_USER:\$GIT_PASS@${env.MANIFEST_REPO.replace('https://', '')} ${MANIFEST_DIR}-${overlayEnv} || \ + (git clone https://\$GIT_USER:\$GIT_PASS@${env.MANIFEST_REPO.replace('https://', '')} ${MANIFEST_DIR}-${overlayEnv} && cd ${MANIFEST_DIR}-${overlayEnv} && git checkout -b ${overlayEnv}) """ + + dir("${MANIFEST_DIR}-${overlayEnv}") { + sh """ + set -e + echo "Cleaning up overlays folder to only keep ${overlayEnv}" + find overlays -mindepth 1 -maxdepth 1 -type d ! -name "${overlayEnv}" -exec rm -rf {} + + + echo "Updating image tags for ${overlayEnv}..." + yq e -i '(.spec.template.spec.containers[] | select(.name == "backend") | .image) = "${REGISTRY}/${APP_NAME}-be:${overlayEnv}-${env.IMAGE_TAG_FINAL}"' overlays/${overlayEnv}/patch-deployment.yaml + yq e -i '(.spec.template.spec.containers[] | select(.name == "frontend") | .image) = "${REGISTRY}/${APP_NAME}-fe:${overlayEnv}-${env.IMAGE_TAG_FINAL}"' overlays/${overlayEnv}/patch-deployment.yaml + + git config user.email "jenkins@automation.local" + git config user.name "Jenkins CI" + git add overlays/${overlayEnv}/patch-deployment.yaml + git commit -m "chore(${overlayEnv}): update image tags to ${overlayEnv}-${env.IMAGE_TAG_FINAL}" || echo "No changes to commit" + + echo "Pushing updates to branch ${overlayEnv}" + git push https://\$GIT_USER:\$GIT_PASS@${env.MANIFEST_REPO.replace('https://', '')} ${overlayEnv} || echo "No push needed for ${overlayEnv}" + """ + } + + echo "Finished updating ${overlayEnv}" } } } + + parallel parallelStages } } } stage('ArgoCD Sync (optional)') { steps { - echo "If ArgoCD auto-sync is enabled, updates will deploy automatically." + echo "ℹIf ArgoCD auto-sync is enabled, updates will deploy automatically." } } }