update Jenkinsfile for gitops workflow
This commit is contained in:
parent
6643739c61
commit
d2b6391742
|
|
@ -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."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue