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

50
Jenkinsfile vendored
View File

@ -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)
branches.each { envSet ->
sh """
echo "=============================="
echo "Updating GitOps for branch: ${envSet.name}"
echo "=============================="
# 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
# Clone repo GitOps
# Clone branch GitOps yang sesuai
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
git -c http.sslVerify=false clone -b ${envSet.name} https://$GITEA_USER:$GITEA_PASS@git.winteraccess.id/syifa/datasiswa-gitops.git gitops
cd gitops
# 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
# 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 perubahan
# Commit & push
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
'''
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."
}
}
}