update: jenkinsfile code for multibranch pipeline

This commit is contained in:
adelyaou 2025-10-24 13:41:07 +07:00
parent a2a9ac5ac2
commit 3d41ff783c
1 changed files with 122 additions and 81 deletions

203
Jenkinsfile vendored
View File

@ -1,90 +1,131 @@
pipeline { pipeline {
agent any agent any
environment { environment {
REGISTRY = 'docker.io/adelyao' REGISTRY = "docker.io/adelyao"
APP_NAME = 'employee' APP_NAME = "employee"
MANIFEST_REPO = 'https://git.winteraccess.id/adel/Employee-manifest.git' MANIFEST_REPO = "https://git.winteraccess.id/adel/Employee-manifest.git"
GIT_USER = credentials('gitea-username') MANIFEST_CRED_ID = "GIT_CRED_ID"
GIT_PASS = credentials('gitea-password') DOCKER_CRED_ID = "DOCKER_CRED_ID"
MANIFEST_DIR = "manifest"
}
stages {
stage('Prepare Workspace') {
steps {
cleanWs()
checkout scm
}
} }
stages { stage('Install yq') {
stage('Determine Environment') { steps {
steps { sh '''
script { if ! [ -x /usr/local/bin/yq ]; then
if (env.BRANCH_NAME == 'main' || env.BRANCH_NAME == 'dev') { echo "Installing yq..."
overlayEnv = 'dev' wget https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 -O /usr/local/bin/yq
} else if (env.BRANCH_NAME == 'staging' || env.BRANCH_NAME == 'stag') { chmod +x /usr/local/bin/yq
overlayEnv = 'stag' fi
} else if (env.BRANCH_NAME == 'production' || env.BRANCH_NAME == 'prod') { yq --version
overlayEnv = 'prod' '''
} else { }
error("Unknown branch: ${env.BRANCH_NAME}")
}
env.IMAGE_TAG_FINAL = "${overlayEnv}-${env.BRANCH_NAME}-build-${env.BUILD_NUMBER}"
echo "Environment selected: ${overlayEnv}"
}
}
}
stage('Build and Push Docker Images') {
steps {
script {
sh """
docker build -t ${REGISTRY}/${APP_NAME}-be:${IMAGE_TAG_FINAL} ./backend
docker push ${REGISTRY}/${APP_NAME}-be:${IMAGE_TAG_FINAL}
docker build -t ${REGISTRY}/${APP_NAME}-fe:${IMAGE_TAG_FINAL} ./frontend
docker push ${REGISTRY}/${APP_NAME}-fe:${IMAGE_TAG_FINAL}
"""
}
}
}
stage('Update Manifest Repository') {
steps {
script {
sh """
set -e
echo "Cloning manifest repo..."
rm -rf Employee-manifest
git clone -b ${overlayEnv} https://${GIT_USER}:${GIT_PASS}@${MANIFEST_REPO.replace('https://', '')} Employee-manifest
cd Employee-manifest
echo "Cleaning overlays folder, keeping only ${overlayEnv}..."
for d in overlays/*; do
[ -d "\$d" ] || continue
if [ "\$(basename \"\$d\")" != "${overlayEnv}" ]; then
echo "Removing \$d"
rm -rf "\$d"
fi
done
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}"
"""
}
}
}
} }
post { stage('Build and Push Docker Images') {
success { steps {
echo 'Pipeline completed successfully.' script {
} def gitBranch = env.BRANCH_NAME ?: sh(script: "git rev-parse --abbrev-ref HEAD", returnStdout: true).trim()
failure { def tag = "${gitBranch}-build-${env.BUILD_NUMBER}"
echo 'Pipeline failed. Please check logs.' 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
docker push ${REGISTRY}/${APP_NAME}-be:${tag}
docker build -t ${REGISTRY}/${APP_NAME}-fe:${tag} ./employee-fe
docker push ${REGISTRY}/${APP_NAME}-fe:${tag}
docker logout
"""
}
env.IMAGE_TAG_FINAL = tag
} }
}
} }
stage('Update All Manifest Branches') {
steps {
script {
def envList = ["dev", "stag", "prod"]
def parallelStages = [:]
envList.each { overlayEnv ->
parallelStages[overlayEnv] = {
echo "Starting manifest update for environment: ${overlayEnv}"
withCredentials([usernamePassword(credentialsId: env.MANIFEST_CRED_ID, usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) {
sh """
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 overlays folder, keeping only ${overlayEnv}..."
for d in overlays/*; do
[ -d "\$d" ] || continue
if [ "\$(basename \"\$d\")" != "${overlayEnv}" ]; then
echo "Removing \$d"
rm -rf "\$d"
fi
done
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."
}
}
}
post {
success {
echo "Pipeline completed successfully for all branches (dev, stag, prod)"
}
failure {
echo "Pipeline failed. Check logs for details."
}
}
} }