update Jenkinsfile for gitops workflow

This commit is contained in:
Syifa 2025-10-23 17:58:45 +07:00
parent 6643739c61
commit d2b6391742
1 changed files with 41 additions and 29 deletions

70
Jenkinsfile vendored
View File

@ -6,8 +6,6 @@ pipeline {
BACKEND_NAME = "backend-app" BACKEND_NAME = "backend-app"
FRONTEND_NAME = "frontend-app" FRONTEND_NAME = "frontend-app"
GITOPS_REPO = "https://git.winteraccess.id/syifa/datasiswa-gitops.git" GITOPS_REPO = "https://git.winteraccess.id/syifa/datasiswa-gitops.git"
GITOPS_BRANCH = "dev"
DEPLOY_OVERLAY = "overlays/dev"
} }
stages { stages {
@ -56,36 +54,50 @@ pipeline {
} }
} }
stage('Update GitOps Repo') { stage('Update GitOps Repos (dev, staging, production)') {
steps { steps {
script { 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')]) { withCredentials([usernamePassword(credentialsId: 'gitea-token-gitops', usernameVariable: 'GITEA_USER', passwordVariable: 'GITEA_PASS')]) {
sh ''' branches.each { envSet ->
# Install yq lokal (tanpa root) sh """
if ! command -v ./yq &> /dev/null; then echo "=============================="
echo "Installing yq locally..." echo "Updating GitOps for branch: ${envSet.name}"
wget -qO ./yq https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 echo "=============================="
chmod +x ./yq
fi
# Clone repo GitOps # Install yq kalau belum ada
rm -rf gitops if ! command -v ./yq &> /dev/null; then
git -c http.sslVerify=false clone -b $GITOPS_BRANCH https://$GITEA_USER:$GITEA_PASS@git.winteraccess.id/syifa/datasiswa-gitops.git gitops echo "Installing yq locally..."
cd gitops wget -qO ./yq https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64
chmod +x ./yq
fi
# Update image tag pakai yq lokal # Clone branch GitOps yang sesuai
echo "Updating image tags..." rm -rf gitops
../yq e -i ".spec.template.spec.containers[] |= select(.name == \\"backend\\").image = env(BACKEND_TAG)" ${DEPLOY_OVERLAY}/patch-deployment.yaml git -c http.sslVerify=false clone -b ${envSet.name} https://$GITEA_USER:$GITEA_PASS@git.winteraccess.id/syifa/datasiswa-gitops.git gitops
../yq e -i ".spec.template.spec.containers[] |= select(.name == \\"frontend\\").image = env(FRONTEND_TAG)" ${DEPLOY_OVERLAY}/patch-deployment.yaml cd gitops
# Commit & push perubahan # Update image tags
git config user.name "jenkins" echo "Updating ${envSet.overlay}/patch-deployment.yaml..."
git config user.email "jenkins@gitea.local" ../yq e -i ".spec.template.spec.containers[] |= select(.name == \\"backend\\").image = env(BACKEND_TAG)" ${envSet.overlay}/patch-deployment.yaml
git add . ../yq e -i ".spec.template.spec.containers[] |= select(.name == \\"frontend\\").image = env(FRONTEND_TAG)" ${envSet.overlay}/patch-deployment.yaml
git commit -m "Update ${DEPLOY_OVERLAY}: backend=${BUILD_NUMBER}, frontend=${BUILD_NUMBER}" || echo "No changes to commit"
git push origin $GITOPS_BRANCH # 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 { post {
success { success {
echo "GitOps update successful — manifests updated in ${DEPLOY_OVERLAY}" echo "✅ GitOps update successful — all branches (dev, staging, production) updated!"
} }
failure { failure {
echo "Pipeline failed, check Jenkins logs for details." echo "Pipeline failed, check Jenkins logs for details."
} }
always { always {
cleanWs() cleanWs()
echo "Workspace cleaned up." echo "🧹 Workspace cleaned up."
} }
} }
} }