Employee-app/Jenkinsfile

110 lines
3.7 KiB
Groovy

pipeline {
agent any
environment {
REGISTRY = "docker.io/adelyao"
APP_NAME = "employee"
MANIFEST_REPO = "https://git.winteraccess.id/adel/Employee-manifest.git"
MANIFEST_CRED_ID = "GIT_CRED_ID"
DOCKER_CRED_ID = "DOCKER_CRED_ID"
MANIFEST_DIR = "manifest"
}
stages {
stage('Prepare Workspace') {
steps {
cleanWs()
checkout scm
}
}
stage('Install yq') {
steps {
sh '''
if ! command -v yq &> /dev/null; then
echo "Installing yq..."
wget https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 -O /usr/local/bin/yq
chmod +x /usr/local/bin/yq
fi
yq --version
'''
}
}
stage('Build and Push Docker Images') {
steps {
script {
def tag = "build-${env.BUILD_NUMBER}"
echo "Building and pushing images with tag: ${tag}"
withCredentials([usernamePassword(credentialsId: env.DOCKER_CRED_ID, usernameVariable: 'DOCKER_USER', passwordVariable: 'DOCKER_PASS')]) {
sh """
echo "\$DOCKER_PASS" | docker login -u "\$DOCKER_USER" --password-stdin
docker build -t ${REGISTRY}/${APP_NAME}-be:${tag} ./employee-be
docker push ${REGISTRY}/${APP_NAME}-be:${tag}
docker build -t ${REGISTRY}/${APP_NAME}-fe:${tag} ./employee-fe
docker push ${REGISTRY}/${APP_NAME}-fe:${tag}
docker logout
"""
}
env.IMAGE_TAG_FINAL = tag
}
}
}
stage('Update All Manifest Branches') {
steps {
script {
def envList = ["dev", "stag", "prod"]
withCredentials([usernamePassword(credentialsId: env.MANIFEST_CRED_ID, usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) {
envList.each { overlayEnv ->
echo "🔹 Updating manifest for environment: ${overlayEnv}"
sh """
echo "Cloning branch ${overlayEnv}..."
rm -rf ${MANIFEST_DIR}
git clone --single-branch --branch ${overlayEnv} https://\$GIT_USER:\$GIT_PASS@${env.MANIFEST_REPO.replace('https://', '')} ${MANIFEST_DIR} || \
(git clone https://\$GIT_USER:\$GIT_PASS@${env.MANIFEST_REPO.replace('https://', '')} ${MANIFEST_DIR} && cd ${MANIFEST_DIR} && git checkout -b ${overlayEnv})
"""
dir(env.MANIFEST_DIR) {
sh """
echo "Updating image tags for ${overlayEnv}..."
yq e -i '(.spec.template.spec.containers[] | select(.name == "backend") | .image) = "${REGISTRY}/${APP_NAME}-be:${overlayEnv}-${env.IMAGE_TAG_FINAL}"' overlays/${overlayEnv}/patch-deployment.yaml
yq e -i '(.spec.template.spec.containers[] | select(.name == "frontend") | .image) = "${REGISTRY}/${APP_NAME}-fe:${overlayEnv}-${env.IMAGE_TAG_FINAL}"' overlays/${overlayEnv}/patch-deployment.yaml
git reset -- overlays/*/
git add overlays/${overlayEnv}/
git commit -m "chore(${overlayEnv}): update image tags to ${overlayEnv}-${env.IMAGE_TAG_FINAL}" || echo "No changes to commit"
git push https://\$GIT_USER:\$GIT_PASS@${env.MANIFEST_REPO.replace('https://', '')} ${overlayEnv}
"""
}
}
}
}
}
}
stage('ArgoCD Sync (optional)') {
steps {
echo "If ArgoCD auto-sync is enabled, updates will deploy automatically."
}
}
}
post {
success {
echo "Pipeline completed successfully for all branches (dev, stag, prod)"
}
failure {
echo "Pipeline failed. Check logs for details."
}
}
}