From d2b6391742285d042b34cf4e2b53152d3b3c055b Mon Sep 17 00:00:00 2001 From: Syifa Date: Thu, 23 Oct 2025 17:58:45 +0700 Subject: [PATCH] update Jenkinsfile for gitops workflow --- Jenkinsfile | 70 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 29 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 4e1fe24..0c1d1bd 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -6,8 +6,6 @@ pipeline { BACKEND_NAME = "backend-app" FRONTEND_NAME = "frontend-app" GITOPS_REPO = "https://git.winteraccess.id/syifa/datasiswa-gitops.git" - GITOPS_BRANCH = "dev" - DEPLOY_OVERLAY = "overlays/dev" } stages { @@ -56,36 +54,50 @@ pipeline { } } - stage('Update GitOps Repo') { + stage('Update GitOps Repos (dev, staging, production)') { steps { script { - echo "Updating deployment manifests in GitOps repo..." + def branches = [ + [name: "dev", overlay: "overlays/dev"], + [name: "staging", overlay: "overlays/staging"], + [name: "production", overlay: "overlays/production"] + ] + withCredentials([usernamePassword(credentialsId: 'gitea-token-gitops', usernameVariable: 'GITEA_USER', passwordVariable: 'GITEA_PASS')]) { - sh ''' - # Install yq lokal (tanpa root) - if ! command -v ./yq &> /dev/null; then - echo "Installing yq locally..." - wget -qO ./yq https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 - chmod +x ./yq - fi + branches.each { envSet -> + sh """ + echo "==============================" + echo "Updating GitOps for branch: ${envSet.name}" + echo "==============================" - # 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 + # Install yq kalau belum ada + if ! command -v ./yq &> /dev/null; then + echo "Installing yq locally..." + wget -qO ./yq https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 + chmod +x ./yq + fi - # Update image tag pakai yq lokal - 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 + # Clone branch GitOps yang sesuai + rm -rf gitops + git -c http.sslVerify=false clone -b ${envSet.name} https://$GITEA_USER:$GITEA_PASS@git.winteraccess.id/syifa/datasiswa-gitops.git gitops + cd gitops - # 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 to commit" - git push origin $GITOPS_BRANCH - ''' + # Update image tags + echo "Updating ${envSet.overlay}/patch-deployment.yaml..." + ../yq e -i ".spec.template.spec.containers[] |= select(.name == \\"backend\\").image = env(BACKEND_TAG)" ${envSet.overlay}/patch-deployment.yaml + ../yq e -i ".spec.template.spec.containers[] |= select(.name == \\"frontend\\").image = env(FRONTEND_TAG)" ${envSet.overlay}/patch-deployment.yaml + + # Commit & push + git config user.name "jenkins" + git config user.email "jenkins@gitea.local" + git add . + git commit -m "Update ${envSet.overlay}: backend=${BUILD_NUMBER}, frontend=${BUILD_NUMBER}" || echo "No changes to commit" + git push origin ${envSet.name} + + cd .. + rm -rf gitops + """ + } } } } @@ -94,14 +106,14 @@ pipeline { post { success { - echo "GitOps update successful โ€” manifests updated in ${DEPLOY_OVERLAY}" + echo "โœ… GitOps update successful โ€” all branches (dev, staging, production) updated!" } failure { - echo "Pipeline failed, check Jenkins logs for details." + echo "โŒ Pipeline failed, check Jenkins logs for details." } always { cleanWs() - echo "Workspace cleaned up." + echo "๐Ÿงน Workspace cleaned up." } } }