diff --git a/Jenkinsfile b/Jenkinsfile index 8f95057..8469ab7 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -10,10 +10,6 @@ pipeline { MANIFEST_DIR = "manifest" } - parameters { - string(name: 'IMAGE_TAG', defaultValue: '', description: 'Custom image tag (optional)') - } - stages { stage('Prepare Workspace') { steps { @@ -38,7 +34,7 @@ pipeline { stage('Build and Push Docker Images') { steps { script { - def tag = params.IMAGE_TAG ?: "build-${env.BUILD_NUMBER}" + def tag = "build-${env.BUILD_NUMBER}" echo "Building and pushing images with tag: ${tag}" withCredentials([usernamePassword(credentialsId: env.DOCKER_CRED_ID, usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) { @@ -60,43 +56,35 @@ pipeline { } } - stage('Update Manifest for Current Branch') { + stage('Update All Manifest Branches') { steps { script { - def currentBranch = env.BRANCH_NAME ?: sh(script: "git rev-parse --abbrev-ref HEAD", returnStdout: true).trim() - echo "🔹 Current branch detected: ${currentBranch}" - - def envMap = [ - "develop": "dev", - "staging": "stag", - "main": "prod" - ] - def overlayEnv = envMap.get(currentBranch, currentBranch) - - if (!["dev", "stag", "prod"].contains(overlayEnv)) { - error "Invalid branch/environment: ${overlayEnv}. Only dev, stag, or prod allowed." - } + def envList = ["dev", "stag", "prod"] withCredentials([usernamePassword(credentialsId: env.MANIFEST_CRED_ID, usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) { - sh """ - echo "Cloning manifest repo..." - rm -rf ${MANIFEST_DIR} - git clone https://\$GIT_USER:\$GIT_PASS@${env.MANIFEST_REPO.replace('https://', '')} ${MANIFEST_DIR} - """ + envList.each { overlayEnv -> + echo "🔹 Updating manifest for environment: ${overlayEnv}" - dir(env.MANIFEST_DIR) { sh """ - git checkout ${overlayEnv} || git checkout -b ${overlayEnv} - - echo "Updating image tags inside overlays/${overlayEnv}/patch-deployment.yaml..." - 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 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" - - git push https://\$GIT_USER:\$GIT_PASS@${env.MANIFEST_REPO.replace('https://', '')} ${overlayEnv} + 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) { + 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} + """ + } } } } @@ -112,7 +100,7 @@ pipeline { post { success { - echo "Pipeline completed successfully for branch: ${env.BRANCH_NAME}" + echo "Pipeline completed successfully for all branches (dev, stag, prod)" } failure { echo "Pipeline failed. Check logs for details."