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 { stages {
stage('Checkout Source Code') { stage('Checkout Source Code') {
steps { steps {
echo "Cloning main app repository..." echo "📦 Cloning main application repository..."
checkout scm checkout scm
} }
} }
@ -22,7 +21,7 @@ pipeline {
stage('Build & Push Backend') { stage('Build & Push Backend') {
steps { steps {
script { script {
echo "Building backend image..." echo "⚙️ Building backend image..."
withCredentials([usernamePassword(credentialsId: 'gitops-dockerhub', usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) { withCredentials([usernamePassword(credentialsId: 'gitops-dockerhub', usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) {
sh ''' sh '''
docker login -u $DOCKER_USER -p $DOCKER_PASS docker login -u $DOCKER_USER -p $DOCKER_PASS
@ -41,7 +40,7 @@ pipeline {
stage('Build & Push Frontend') { stage('Build & Push Frontend') {
steps { steps {
script { script {
echo "Building frontend image..." echo "⚙️ Building frontend image..."
withCredentials([usernamePassword(credentialsId: 'gitops-dockerhub', usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) { withCredentials([usernamePassword(credentialsId: 'gitops-dockerhub', usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) {
sh ''' sh '''
docker login -u $DOCKER_USER -p $DOCKER_PASS docker login -u $DOCKER_USER -p $DOCKER_PASS
@ -60,21 +59,31 @@ pipeline {
stage('Update GitOps Repo') { stage('Update GitOps Repo') {
steps { steps {
script { 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')]) { withCredentials([usernamePassword(credentialsId: 'gitea-token-gitops', usernameVariable: 'GITEA_USER', passwordVariable: 'GITEA_PASS')]) {
sh ''' 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 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 $GITOPS_BRANCH https://$GITEA_USER:$GITEA_PASS@git.winteraccess.id/syifa/datasiswa-gitops.git gitops
cd gitops cd gitops
# Update image tag di patch deployment # Update image tag dengan yq
sed -i "s|${REGISTRY}/${BACKEND_NAME}:[^ ]*|${BACKEND_TAG}|g" ${DEPLOY_OVERLAY}/patch-deployment.yaml echo "🔧 Updating image tags..."
sed -i "s|${REGISTRY}/${FRONTEND_NAME}:[^ ]*|${FRONTEND_TAG}|g" ${DEPLOY_OVERLAY}/patch-deployment.yaml 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.name "jenkins"
git config user.email "jenkins@gitea.local" git config user.email "jenkins@gitea.local"
git add . 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 git push origin $GITOPS_BRANCH
''' '''
} }
@ -85,14 +94,14 @@ pipeline {
post { post {
success { success {
echo "✅ Deployment successful to ${DEPLOY_OVERLAY}" echo "✅ GitOps update successful — manifests updated in ${DEPLOY_OVERLAY}"
} }
failure { failure {
echo "❌ Deployment failed, check logs." echo "❌ Pipeline failed, check Jenkins logs for details."
} }
always { always {
cleanWs() cleanWs()
echo "🧹 Workspace cleaned." echo "🧹 Workspace cleaned up."
} }
} }
} }