datasiswa-app/Jenkinsfile

108 lines
4.6 KiB
Groovy

pipeline {
agent any
environment {
REGISTRY = "docker.io/syifamaulidya"
BACKEND_NAME = "backend-app"
FRONTEND_NAME = "frontend-app"
GITOPS_REPO = "https://git.winteraccess.id/syifa/datasiswa-gitops.git"
GITOPS_BRANCH = "main"
DEPLOY_OVERLAY = "overlays/dev"
}
stages {
stage('Checkout Source Code') {
steps {
echo "📦 Cloning main application repository..."
checkout scm
}
}
stage('Build & Push Backend') {
steps {
script {
echo "⚙️ Building backend image..."
withCredentials([usernamePassword(credentialsId: 'gitops-dockerhub', usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) {
sh '''
docker login -u $DOCKER_USER -p $DOCKER_PASS
cd backend
docker build -t $REGISTRY/$BACKEND_NAME:$BUILD_NUMBER .
docker push $REGISTRY/$BACKEND_NAME:$BUILD_NUMBER
docker tag $REGISTRY/$BACKEND_NAME:$BUILD_NUMBER $REGISTRY/$BACKEND_NAME:latest
docker push $REGISTRY/$BACKEND_NAME:latest
'''
}
env.BACKEND_TAG = "${REGISTRY}/${BACKEND_NAME}:${BUILD_NUMBER}"
}
}
}
stage('Build & Push Frontend') {
steps {
script {
echo "⚙️ Building frontend image..."
withCredentials([usernamePassword(credentialsId: 'gitops-dockerhub', usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) {
sh '''
docker login -u $DOCKER_USER -p $DOCKER_PASS
cd frontend
docker build -t $REGISTRY/$FRONTEND_NAME:$BUILD_NUMBER .
docker push $REGISTRY/$FRONTEND_NAME:$BUILD_NUMBER
docker tag $REGISTRY/$FRONTEND_NAME:$BUILD_NUMBER $REGISTRY/$FRONTEND_NAME:latest
docker push $REGISTRY/$FRONTEND_NAME:latest
'''
}
env.FRONTEND_TAG = "${REGISTRY}/${FRONTEND_NAME}:${BUILD_NUMBER}"
}
}
}
stage('Update GitOps Repo') {
steps {
script {
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 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 to commit"
git push origin $GITOPS_BRANCH
'''
}
}
}
}
}
post {
success {
echo "✅ GitOps update successful — manifests updated in ${DEPLOY_OVERLAY}"
}
failure {
echo "❌ Pipeline failed, check Jenkins logs for details."
}
always {
cleanWs()
echo "🧹 Workspace cleaned up."
}
}
}