update: jenkinsfile code for multibranch pipeline

This commit is contained in:
adelyaou 2025-10-24 14:28:10 +07:00
parent 3d41ff783c
commit b6150a7e32
1 changed files with 12 additions and 11 deletions

23
Jenkinsfile vendored
View File

@ -58,7 +58,7 @@ pipeline {
} }
} }
stage('Update All Manifest Branches') { stage('Update and Push All Manifest Branches') {
steps { steps {
script { script {
def envList = ["dev", "stag", "prod"] def envList = ["dev", "stag", "prod"]
@ -66,13 +66,13 @@ pipeline {
envList.each { overlayEnv -> envList.each { overlayEnv ->
parallelStages[overlayEnv] = { parallelStages[overlayEnv] = {
echo "Starting manifest update for environment: ${overlayEnv}" echo "Updating manifest for environment: ${overlayEnv}"
withCredentials([usernamePassword(credentialsId: env.MANIFEST_CRED_ID, usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) { withCredentials([usernamePassword(credentialsId: env.MANIFEST_CRED_ID, usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) {
sh """ sh """
set -e set -e
echo "Cloning manifest branch: ${overlayEnv}"
rm -rf ${MANIFEST_DIR}-${overlayEnv} rm -rf ${MANIFEST_DIR}-${overlayEnv}
echo "Cloning manifest branch: ${overlayEnv}"
git clone --single-branch --branch ${overlayEnv} https://\$GIT_USER:\$GIT_PASS@${env.MANIFEST_REPO.replace('https://', '')} ${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}) (git clone https://\$GIT_USER:\$GIT_PASS@${env.MANIFEST_REPO.replace('https://', '')} ${MANIFEST_DIR}-${overlayEnv} && cd ${MANIFEST_DIR}-${overlayEnv} && git checkout -b ${overlayEnv})
""" """
@ -80,7 +80,7 @@ pipeline {
dir("${MANIFEST_DIR}-${overlayEnv}") { dir("${MANIFEST_DIR}-${overlayEnv}") {
sh """ sh """
set -e set -e
echo "Cleaning overlays folder, keeping only ${overlayEnv}..." echo "Cleaning overlays folder (keeping only ${overlayEnv})..."
for d in overlays/*; do for d in overlays/*; do
[ -d "\$d" ] || continue [ -d "\$d" ] || continue
if [ "\$(basename \"\$d\")" != "${overlayEnv}" ]; then if [ "\$(basename \"\$d\")" != "${overlayEnv}" ]; then
@ -89,21 +89,22 @@ pipeline {
fi fi
done done
echo "Updating image tags for ${overlayEnv}..." echo "Updating image tags in 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 == "backend") | .image) = "${REGISTRY}/${APP_NAME}-be:${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 yq e -i '(.spec.template.spec.containers[] | select(.name == "frontend") | .image) = "${REGISTRY}/${APP_NAME}-fe:${env.IMAGE_TAG_FINAL}"' overlays/${overlayEnv}/patch-deployment.yaml
git config user.email "jenkins@automation.local" git config user.email "jenkins@automation.local"
git config user.name "Jenkins CI" git config user.name "Jenkins CI"
git add 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 commit -m "chore(${overlayEnv}): update image tags to ${env.IMAGE_TAG_FINAL}" || echo "No changes to commit"
echo "Pushing updates to branch ${overlayEnv}" 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}" git push https://\$GIT_USER:\$GIT_PASS@${env.MANIFEST_REPO.replace('https://', '')} ${overlayEnv} || echo "No push needed for ${overlayEnv}"
""" """
} }
echo "Finished updating ${overlayEnv}" echo "Finished updating branch ${overlayEnv}"
} }
} }
} }
@ -115,14 +116,14 @@ pipeline {
stage('ArgoCD Sync (optional)') { stage('ArgoCD Sync (optional)') {
steps { steps {
echo "If ArgoCD auto-sync is enabled, updates will deploy automatically." echo "If ArgoCD auto-sync is enabled, each environment will sync automatically."
} }
} }
} }
post { post {
success { success {
echo "Pipeline completed successfully for all branches (dev, stag, prod)" echo "Pipeline completed successfully for all environment branches (dev, stag, prod)."
} }
failure { failure {
echo "Pipeline failed. Check logs for details." echo "Pipeline failed. Check logs for details."