From 990abb35cb3b84f35ede297320d2a17a40f77de5 Mon Sep 17 00:00:00 2001 From: Syifa Date: Wed, 22 Oct 2025 11:15:58 +0700 Subject: [PATCH] update Jenkinsfile for pipeline gitops workflow --- Jenkinsfile | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 6809545..c393075 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -11,10 +11,9 @@ pipeline { } stages { - stage('Checkout Source Code') { steps { - echo "Cloning main app repository..." + echo "๐Ÿ“ฆ Cloning main application repository..." checkout scm } } @@ -22,7 +21,7 @@ pipeline { stage('Build & Push Backend') { steps { script { - echo "Building backend image..." + echo "โš™๏ธ Building backend image..." withCredentials([usernamePassword(credentialsId: 'gitops-dockerhub', usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) { sh ''' docker login -u $DOCKER_USER -p $DOCKER_PASS @@ -41,7 +40,7 @@ pipeline { stage('Build & Push Frontend') { steps { script { - echo "Building frontend image..." + echo "โš™๏ธ Building frontend image..." withCredentials([usernamePassword(credentialsId: 'gitops-dockerhub', usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) { sh ''' docker login -u $DOCKER_USER -p $DOCKER_PASS @@ -60,21 +59,31 @@ pipeline { stage('Update GitOps Repo') { steps { script { - echo "Updating manifests in GitOps repo..." + echo "๐Ÿ“ Updating deployment manifests in GitOps repo..." withCredentials([usernamePassword(credentialsId: 'gitea-token-gitops', usernameVariable: 'GITEA_USER', passwordVariable: 'GITEA_PASS')]) { sh ''' + # Install yq kalau belum terpasang + if ! command -v yq &> /dev/null; then + echo "๐Ÿ“ฅ Installing yq..." + wget -qO /usr/local/bin/yq https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 + chmod +x /usr/local/bin/yq + fi + + # Clone repo GitOps rm -rf gitops git -c http.sslVerify=false clone -b $GITOPS_BRANCH https://$GITEA_USER:$GITEA_PASS@git.winteraccess.id/syifa/datasiswa-gitops.git gitops cd gitops - # Update image tag di patch deployment - sed -i "s|${REGISTRY}/${BACKEND_NAME}:[^ ]*|${BACKEND_TAG}|g" ${DEPLOY_OVERLAY}/patch-deployment.yaml - sed -i "s|${REGISTRY}/${FRONTEND_NAME}:[^ ]*|${FRONTEND_TAG}|g" ${DEPLOY_OVERLAY}/patch-deployment.yaml + # Update image tag dengan yq + echo "๐Ÿ”ง Updating image tags..." + yq e -i ".spec.template.spec.containers[] |= select(.name == \\"backend\\").image = env(BACKEND_TAG)" ${DEPLOY_OVERLAY}/patch-deployment.yaml + yq e -i ".spec.template.spec.containers[] |= select(.name == \\"frontend\\").image = env(FRONTEND_TAG)" ${DEPLOY_OVERLAY}/patch-deployment.yaml + # Commit & push perubahan git config user.name "jenkins" git config user.email "jenkins@gitea.local" git add . - git commit -m "Update ${DEPLOY_OVERLAY} โ†’ backend:${BUILD_NUMBER}, frontend:${BUILD_NUMBER}" || echo "No changes" + git commit -m "Update ${DEPLOY_OVERLAY}: backend=${BUILD_NUMBER}, frontend=${BUILD_NUMBER}" || echo "No changes to commit" git push origin $GITOPS_BRANCH ''' } @@ -85,14 +94,14 @@ pipeline { post { success { - echo "โœ… Deployment successful to ${DEPLOY_OVERLAY}" + echo "โœ… GitOps update successful โ€” manifests updated in ${DEPLOY_OVERLAY}" } failure { - echo "โŒ Deployment failed, check logs." + echo "โŒ Pipeline failed, check Jenkins logs for details." } always { cleanWs() - echo "๐Ÿงน Workspace cleaned." + echo "๐Ÿงน Workspace cleaned up." } } }