update Jenkinsfile for pipeline gitops workflow

This commit is contained in:
Syifa 2025-10-22 11:15:58 +07:00
parent 19ba4d1c64
commit 990abb35cb
1 changed files with 21 additions and 12 deletions

33
Jenkinsfile vendored
View File

@ -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."
}
}
}