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

115
Jenkinsfile vendored
View File

@ -2,57 +2,84 @@ pipeline {
agent any
environment {
REGISTRY = 'docker.io/adelyao'
APP_NAME = 'employee'
MANIFEST_REPO = 'https://git.winteraccess.id/adel/Employee-manifest.git'
GIT_USER = credentials('gitea-username')
GIT_PASS = credentials('gitea-password')
REGISTRY = "docker.io/adelyao"
APP_NAME = "employee"
MANIFEST_REPO = "https://git.winteraccess.id/adel/Employee-manifest.git"
MANIFEST_CRED_ID = "GIT_CRED_ID"
DOCKER_CRED_ID = "DOCKER_CRED_ID"
MANIFEST_DIR = "manifest"
}
stages {
stage('Determine Environment') {
stage('Prepare Workspace') {
steps {
script {
if (env.BRANCH_NAME == 'main' || env.BRANCH_NAME == 'dev') {
overlayEnv = 'dev'
} else if (env.BRANCH_NAME == 'staging' || env.BRANCH_NAME == 'stag') {
overlayEnv = 'stag'
} else if (env.BRANCH_NAME == 'production' || env.BRANCH_NAME == 'prod') {
overlayEnv = 'prod'
} else {
error("Unknown branch: ${env.BRANCH_NAME}")
cleanWs()
checkout scm
}
}
env.IMAGE_TAG_FINAL = "${overlayEnv}-${env.BRANCH_NAME}-build-${env.BUILD_NUMBER}"
echo "Environment selected: ${overlayEnv}"
}
stage('Install yq') {
steps {
sh '''
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
fi
yq --version
'''
}
}
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}
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}"
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 {
withCredentials([usernamePassword(credentialsId: env.DOCKER_CRED_ID, usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) {
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 "\$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
@ -75,16 +102,30 @@ pipeline {
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.'
echo "Pipeline completed successfully for all branches (dev, stag, prod)"
}
failure {
echo 'Pipeline failed. Please check logs.'
echo "Pipeline failed. Check logs for details."
}
}
}