datasiswa-app/Jenkinsfile

115 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"
}
stages {
stage('Checkout Source Code') {
steps {
echo "Cloning main app 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('Set Environment') {
steps {
script {
if (env.BRANCH_NAME == 'dev') {
env.DEPLOY_OVERLAY = 'overlays/dev'
} else if (env.BRANCH_NAME == 'staging') {
env.DEPLOY_OVERLAY = 'overlays/staging'
} else if (env.BRANCH_NAME == 'production') {
env.DEPLOY_OVERLAY = 'overlays/production'
} else {
error("Branch tidak dikenali! Harus dev / staging / production.")
}
echo "Environment selected → ${env.DEPLOY_OVERLAY}"
}
}
}
stage('Update GitOps Repo') {
steps {
script {
echo "Updating manifests in GitOps repo..."
withCredentials([usernamePassword(credentialsId: 'gitea-token-gitops', usernameVariable: 'GITEA_USER', passwordVariable: 'GITEA_PASS')]) {
sh '''
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 tag image backend & frontend
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
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 push origin $GITOPS_BRANCH
'''
}
}
}
}
}
post {
success {
echo "Deployment successful to ${DEPLOY_OVERLAY}"
}
failure {
echo "Deployment failed, check logs."
}
always {
cleanWs()
echo "Pipeline finished and workspace cleaned."
}
}
}