Update: Change jenkins pipeline code for multibranch

This commit is contained in:
adelyaou 2025-10-23 17:08:30 +07:00
parent 2ea637d07f
commit 00fce47934
1 changed files with 36 additions and 47 deletions

81
Jenkinsfile vendored
View File

@ -7,11 +7,12 @@ pipeline {
MANIFEST_REPO = "https://git.winteraccess.id/adel/Employee-manifest.git"
MANIFEST_CRED_ID = "GIT_CRED_ID"
DOCKER_CRED_ID = "DOCKER_CRED_ID"
BRANCH = "main"
BRANCHES = "dev stag prod"
MANIFEST_DIR = "manifest"
}
parameters {
string(name: 'IMAGE_TAG', defaultValue: '', description: 'Image tag (e.g., commit SHA or build number)')
string(name: 'IMAGE_TAG', defaultValue: '', description: 'Custom image tag (optional)')
}
stages {
@ -19,7 +20,7 @@ pipeline {
stage('Prepare Workspace') {
steps {
cleanWs()
checkout scm // ✅ otomatis clone repo Gitea tempat Jenkinsfile berada
checkout scm
}
}
@ -30,8 +31,6 @@ pipeline {
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
else
echo "yq already installed"
fi
yq --version
'''
@ -42,15 +41,20 @@ pipeline {
steps {
script {
def tag = params.IMAGE_TAG ?: "build-${env.BUILD_NUMBER}"
echo "Building and pushing image with tag: ${tag}"
echo "Building and pushing images with tag: ${tag}"
withCredentials([usernamePassword(credentialsId: env.DOCKER_CRED_ID, usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) {
sh """
echo "$DOCKER_PASS" | docker login -u "$DOCKER_USER" --password-stdin
# Build & push backend
docker build -t ${REGISTRY}/${APP_NAME}-be:${tag} ./employee-be
docker build -t ${REGISTRY}/${APP_NAME}-fe:${tag} ./employee-fe
docker push ${REGISTRY}/${APP_NAME}-be:${tag}
# Build & push frontend
docker build -t ${REGISTRY}/${APP_NAME}-fe:${tag} ./employee-fe
docker push ${REGISTRY}/${APP_NAME}-fe:${tag}
docker logout
"""
}
@ -60,47 +64,32 @@ pipeline {
}
}
stage('Checkout Manifest Repo') {
stage('Update Manifests for All Environments') {
steps {
dir('manifest') {
checkout([$class: 'GitSCM',
branches: [[name: env.BRANCH]],
userRemoteConfigs: [[
url: env.MANIFEST_REPO,
credentialsId: env.MANIFEST_CRED_ID
]]
])
sh "git checkout ${env.BRANCH} || git checkout -b ${env.BRANCH}"
}
}
}
stage('Update Image Tags in Manifests') {
steps {
dir('manifest') {
script {
echo "Updating manifests to tag: ${env.IMAGE_TAG_FINAL}"
sh """
yq e -i '.spec.template.spec.containers[0].image = "${REGISTRY}/${APP_NAME}-be:${env.IMAGE_TAG_FINAL}"' overlays/dev/patch-deployment.yaml
yq e -i '.spec.template.spec.containers[0].image = "${REGISTRY}/${APP_NAME}-fe:${env.IMAGE_TAG_FINAL}"' overlays/dev/patch-deployment.yaml
"""
}
}
}
}
stage('Commit & Push Manifest Updates') {
steps {
dir('manifest') {
withCredentials([usernamePassword(credentialsId: env.MANIFEST_CRED_ID, usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) {
sh """
git config user.email "jenkins@local"
git config user.name "jenkins"
git add -A
git diff --staged --quiet || (git commit -m "chore: update image to ${env.IMAGE_TAG_FINAL}" && git push https://${GIT_USER}:${GIT_PASS}@${env.MANIFEST_REPO.replace('https://', '')} ${env.BRANCH})
rm -rf ${MANIFEST_DIR}
git clone https://${GIT_USER}:${GIT_PASS}@${env.MANIFEST_REPO.replace('https://', '')} ${MANIFEST_DIR}
"""
echo "Manifest repo updated successfully"
}
dir(env.MANIFEST_DIR) {
env.BRANCHES.split().each { envName ->
echo "🔹 Updating manifests for environment: ${envName}"
sh """
git checkout ${envName} || git checkout -b ${envName}
# Update backend & frontend image tags di overlay sesuai environment
yq e -i '.spec.template.spec.containers[0].image = "${REGISTRY}/${APP_NAME}-be:${envName}-${env.IMAGE_TAG_FINAL}"' overlays/${envName}/backend/patch-deployment.yaml
yq e -i '.spec.template.spec.containers[0].image = "${REGISTRY}/${APP_NAME}-fe:${envName}-${env.IMAGE_TAG_FINAL}"' overlays/${envName}/frontend/patch-deployment.yaml
git add overlays/${envName}/backend/patch-deployment.yaml overlays/${envName}/frontend/patch-deployment.yaml
git commit -m "chore(${envName}): update image tags to ${envName}-${env.IMAGE_TAG_FINAL}" || echo "No changes for ${envName}"
git push https://${GIT_USER}:${GIT_PASS}@${env.MANIFEST_REPO.replace('https://', '')} ${envName}
"""
}
}
}
}
@ -108,17 +97,17 @@ pipeline {
stage('ArgoCD Sync (optional)') {
steps {
echo "If ArgoCD auto-sync is enabled, deployment will update automatically."
echo "If ArgoCD auto-sync is enabled, updates will deploy automatically."
}
}
}
post {
success {
echo "GitOps pipeline completed successfully!"
echo "GitOps pipeline completed successfully for dev, stag, and prod!"
}
failure {
echo "Pipeline failed. Check previous logs."
echo "Pipeline failed. Check logs for details."
}
}
}