Update: Change jenkins pipeline code for multibranch
This commit is contained in:
parent
2ea637d07f
commit
00fce47934
|
|
@ -7,11 +7,12 @@ pipeline {
|
||||||
MANIFEST_REPO = "https://git.winteraccess.id/adel/Employee-manifest.git"
|
MANIFEST_REPO = "https://git.winteraccess.id/adel/Employee-manifest.git"
|
||||||
MANIFEST_CRED_ID = "GIT_CRED_ID"
|
MANIFEST_CRED_ID = "GIT_CRED_ID"
|
||||||
DOCKER_CRED_ID = "DOCKER_CRED_ID"
|
DOCKER_CRED_ID = "DOCKER_CRED_ID"
|
||||||
BRANCH = "main"
|
BRANCHES = "dev stag prod"
|
||||||
|
MANIFEST_DIR = "manifest"
|
||||||
}
|
}
|
||||||
|
|
||||||
parameters {
|
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 {
|
stages {
|
||||||
|
|
@ -19,7 +20,7 @@ pipeline {
|
||||||
stage('Prepare Workspace') {
|
stage('Prepare Workspace') {
|
||||||
steps {
|
steps {
|
||||||
cleanWs()
|
cleanWs()
|
||||||
checkout scm // ✅ otomatis clone repo Gitea tempat Jenkinsfile berada
|
checkout scm
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -30,8 +31,6 @@ pipeline {
|
||||||
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
|
||||||
else
|
|
||||||
echo "yq already installed"
|
|
||||||
fi
|
fi
|
||||||
yq --version
|
yq --version
|
||||||
'''
|
'''
|
||||||
|
|
@ -42,15 +41,20 @@ pipeline {
|
||||||
steps {
|
steps {
|
||||||
script {
|
script {
|
||||||
def tag = params.IMAGE_TAG ?: "build-${env.BUILD_NUMBER}"
|
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')]) {
|
withCredentials([usernamePassword(credentialsId: env.DOCKER_CRED_ID, usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) {
|
||||||
sh """
|
sh """
|
||||||
echo "$DOCKER_PASS" | docker login -u "$DOCKER_USER" --password-stdin
|
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}-be:${tag} ./employee-be
|
||||||
docker build -t ${REGISTRY}/${APP_NAME}-fe:${tag} ./employee-fe
|
|
||||||
docker push ${REGISTRY}/${APP_NAME}-be:${tag}
|
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 push ${REGISTRY}/${APP_NAME}-fe:${tag}
|
||||||
|
|
||||||
docker logout
|
docker logout
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
|
|
@ -60,47 +64,32 @@ pipeline {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
stage('Checkout Manifest Repo') {
|
stage('Update Manifests for All Environments') {
|
||||||
steps {
|
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 {
|
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')]) {
|
withCredentials([usernamePassword(credentialsId: env.MANIFEST_CRED_ID, usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) {
|
||||||
sh """
|
sh """
|
||||||
git config user.email "jenkins@local"
|
rm -rf ${MANIFEST_DIR}
|
||||||
git config user.name "jenkins"
|
git clone https://${GIT_USER}:${GIT_PASS}@${env.MANIFEST_REPO.replace('https://', '')} ${MANIFEST_DIR}
|
||||||
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})
|
|
||||||
"""
|
"""
|
||||||
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)') {
|
stage('ArgoCD Sync (optional)') {
|
||||||
steps {
|
steps {
|
||||||
echo "If ArgoCD auto-sync is enabled, deployment will update automatically."
|
echo "If ArgoCD auto-sync is enabled, updates will deploy automatically."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
post {
|
post {
|
||||||
success {
|
success {
|
||||||
echo "GitOps pipeline completed successfully!"
|
echo "GitOps pipeline completed successfully for dev, stag, and prod!"
|
||||||
}
|
}
|
||||||
failure {
|
failure {
|
||||||
echo "Pipeline failed. Check previous logs."
|
echo "Pipeline failed. Check logs for details."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue