update: jenkinsfile code for multibranch pipeline

This commit is contained in:
adelyaou 2025-10-24 11:11:50 +07:00
parent 513a72f4fd
commit f0f871369f
1 changed files with 40 additions and 24 deletions

64
Jenkinsfile vendored
View File

@ -21,7 +21,7 @@ pipeline {
stage('Install yq') { stage('Install yq') {
steps { steps {
sh ''' sh '''
if ! command -v yq &> /dev/null; then if ! [ -x /usr/local/bin/yq ]; then
echo "Installing yq..." echo "Installing yq..."
wget https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 -O /usr/local/bin/yq wget https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 -O /usr/local/bin/yq
chmod +x /usr/local/bin/yq chmod +x /usr/local/bin/yq
@ -34,11 +34,13 @@ pipeline {
stage('Build and Push Docker Images') { stage('Build and Push Docker Images') {
steps { steps {
script { script {
def tag = "build-${env.BUILD_NUMBER}" def gitBranch = env.BRANCH_NAME ?: sh(script: "git rev-parse --abbrev-ref HEAD", returnStdout: true).trim()
echo "Building and pushing images with tag: ${tag}" 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')]) { withCredentials([usernamePassword(credentialsId: env.DOCKER_CRED_ID, usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) {
sh """ sh """
set -e
echo "\$DOCKER_PASS" | docker login -u "\$DOCKER_USER" --password-stdin echo "\$DOCKER_PASS" | docker login -u "\$DOCKER_USER" --password-stdin
docker build -t ${REGISTRY}/${APP_NAME}-be:${tag} ./employee-be docker build -t ${REGISTRY}/${APP_NAME}-be:${tag} ./employee-be
@ -60,40 +62,54 @@ pipeline {
steps { steps {
script { script {
def envList = ["dev", "stag", "prod"] def envList = ["dev", "stag", "prod"]
def parallelStages = [:]
withCredentials([usernamePassword(credentialsId: env.MANIFEST_CRED_ID, usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) { envList.each { overlayEnv ->
envList.each { overlayEnv -> parallelStages[overlayEnv] = {
echo "🔹 Updating manifest for environment: ${overlayEnv}" echo "Starting manifest update for environment: ${overlayEnv}"
sh """ withCredentials([usernamePassword(credentialsId: env.MANIFEST_CRED_ID, usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) {
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 """ sh """
echo "Updating image tags for ${overlayEnv}..." set -e
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 echo "Cloning manifest branch: ${overlayEnv}"
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 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 reset -- overlays/*/ (git clone https://\$GIT_USER:\$GIT_PASS@${env.MANIFEST_REPO.replace('https://', '')} ${MANIFEST_DIR}-${overlayEnv} && cd ${MANIFEST_DIR}-${overlayEnv} && git checkout -b ${overlayEnv})
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}
""" """
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)') { stage('ArgoCD Sync (optional)') {
steps { steps {
echo "If ArgoCD auto-sync is enabled, updates will deploy automatically." echo "If ArgoCD auto-sync is enabled, updates will deploy automatically."
} }
} }
} }