148 lines
5.5 KiB
YAML
148 lines
5.5 KiB
YAML
stages:
|
|
- build
|
|
- push
|
|
- deploy
|
|
|
|
variables:
|
|
DOCKER_DRIVER: overlay2
|
|
DOCKER_IMAGE_BACKEND: $CI_REGISTRY/$CI_PROJECT_PATH/backend
|
|
DOCKER_IMAGE_FRONTEND: $CI_REGISTRY/$CI_PROJECT_PATH/frontend
|
|
GITOPS_REPO: "https://gitlab.com/mauuldya/datasiswa-workflow/datasiswa-gitops.git"
|
|
|
|
# ==========================================
|
|
# 🔧 BEFORE SCRIPT (GLOBAL)
|
|
# ==========================================
|
|
before_script:
|
|
- echo "🔑 Logging in to GitLab Container Registry..."
|
|
- echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER $CI_REGISTRY --password-stdin
|
|
|
|
# ==========================================
|
|
# 🔹 BUILD BACKEND IMAGE
|
|
# ==========================================
|
|
build_backend:
|
|
stage: build
|
|
image: docker:latest
|
|
services:
|
|
- docker:dind
|
|
script:
|
|
- echo "🔧 Building backend image..."
|
|
- docker build -t "$DOCKER_IMAGE_BACKEND:latest" -t "$DOCKER_IMAGE_BACKEND:$CI_COMMIT_SHORT_SHA" ./backend
|
|
only:
|
|
- main
|
|
|
|
# ==========================================
|
|
# 🔹 BUILD FRONTEND IMAGE
|
|
# ==========================================
|
|
build_frontend:
|
|
stage: build
|
|
image: docker:latest
|
|
services:
|
|
- docker:dind
|
|
script:
|
|
- echo "🔧 Building frontend image..."
|
|
- docker build -t "$DOCKER_IMAGE_FRONTEND:latest" -t "$DOCKER_IMAGE_FRONTEND:$CI_COMMIT_SHORT_SHA" ./frontend
|
|
only:
|
|
- main
|
|
|
|
# ==========================================
|
|
# 🔹 PUSH BOTH IMAGES
|
|
# ==========================================
|
|
push_images:
|
|
stage: push
|
|
image: docker:latest
|
|
services:
|
|
- docker:dind
|
|
needs:
|
|
- build_backend
|
|
- build_frontend
|
|
script:
|
|
- echo "🚀 Pushing both images to GitLab Registry..."
|
|
- docker push "$DOCKER_IMAGE_BACKEND:latest"
|
|
- docker push "$DOCKER_IMAGE_BACKEND:$CI_COMMIT_SHORT_SHA"
|
|
- docker push "$DOCKER_IMAGE_FRONTEND:latest"
|
|
- docker push "$DOCKER_IMAGE_FRONTEND:$CI_COMMIT_SHORT_SHA"
|
|
only:
|
|
- main
|
|
|
|
# ==========================================
|
|
# 🔹 DEPLOY TO DEV (GitOps)
|
|
# ==========================================
|
|
deploy_dev:
|
|
stage: deploy
|
|
image: alpine:3.19
|
|
needs: [push_images]
|
|
variables:
|
|
KUSTOMIZE_PATH: "k8s/overlays/dev"
|
|
before_script:
|
|
- apk add --no-cache git bash sed
|
|
- git config --global user.email "gitlab-ci@example.com"
|
|
- git config --global user.name "GitLab CI Bot"
|
|
script:
|
|
- echo "📦 Cloning GitOps repo..."
|
|
- git clone "https://$GITOPS_USERNAME:$GITOPS_TOKEN@gitlab.com/mauuldya/datasiswa-workflow/datasiswa-gitops.git"
|
|
- cd datasiswa-gitops
|
|
- echo "🛠 Updating DEV image tags..."
|
|
- sed -i "s|registry.gitlab.com/.*/backend:.*|$DOCKER_IMAGE_BACKEND:$CI_COMMIT_SHORT_SHA|g" "$KUSTOMIZE_PATH/patch-deployment.yaml"
|
|
- sed -i "s|registry.gitlab.com/.*/frontend:.*|$DOCKER_IMAGE_FRONTEND:$CI_COMMIT_SHORT_SHA|g" "$KUSTOMIZE_PATH/patch-deployment.yaml"
|
|
- git add .
|
|
- git commit -m "🔄 Update DEV images to $CI_COMMIT_SHORT_SHA [skip ci]" || echo "⚠️ No changes to commit"
|
|
- git push "https://$GITOPS_USERNAME:$GITOPS_TOKEN@gitlab.com/mauuldya/datasiswa-workflow/datasiswa-gitops.git" dev
|
|
environment:
|
|
name: dev
|
|
only:
|
|
- main
|
|
|
|
# ==========================================
|
|
# 🔹 DEPLOY TO STAGING
|
|
# ==========================================
|
|
deploy_staging:
|
|
stage: deploy
|
|
image: alpine:3.19
|
|
variables:
|
|
KUSTOMIZE_PATH: "k8s/overlays/staging"
|
|
before_script:
|
|
- apk add --no-cache git bash sed
|
|
- git config --global user.email "gitlab-ci@example.com"
|
|
- git config --global user.name "GitLab CI Bot"
|
|
script:
|
|
- echo "📦 Cloning GitOps repo..."
|
|
- git clone "https://$GITOPS_USERNAME:$GITOPS_TOKEN@gitlab.com/mauuldya/datasiswa-workflow/datasiswa-gitops.git"
|
|
- cd datasiswa-gitops
|
|
- echo "🛠 Updating STAGING image tags..."
|
|
- sed -i "s|registry.gitlab.com/.*/backend:.*|$DOCKER_IMAGE_BACKEND:$CI_COMMIT_SHORT_SHA|g" "$KUSTOMIZE_PATH/patch-deployment.yaml"
|
|
- sed -i "s|registry.gitlab.com/.*/frontend:.*|$DOCKER_IMAGE_FRONTEND:$CI_COMMIT_SHORT_SHA|g" "$KUSTOMIZE_PATH/patch-deployment.yaml"
|
|
- git add .
|
|
- git commit -m "🔄 Update STAGING images to $CI_COMMIT_SHORT_SHA [skip ci]" || echo "⚠️ No changes to commit"
|
|
- git push "https://$GITOPS_USERNAME:$GITOPS_TOKEN@gitlab.com/mauuldya/datasiswa-workflow/datasiswa-gitops.git" staging
|
|
environment:
|
|
name: staging
|
|
only:
|
|
- staging
|
|
|
|
# ==========================================
|
|
# 🔹 DEPLOY TO PRODUCTION
|
|
# ==========================================
|
|
deploy_production:
|
|
stage: deploy
|
|
image: alpine:3.19
|
|
variables:
|
|
KUSTOMIZE_PATH: "k8s/overlays/production"
|
|
before_script:
|
|
- apk add --no-cache git bash sed
|
|
- git config --global user.email "gitlab-ci@example.com"
|
|
- git config --global user.name "GitLab CI Bot"
|
|
script:
|
|
- echo "📦 Cloning GitOps repo..."
|
|
- git clone "https://$GITOPS_USERNAME:$GITOPS_TOKEN@gitlab.com/mauuldya/datasiswa-workflow/datasiswa-gitops.git"
|
|
- cd datasiswa-gitops
|
|
- echo "🛠 Updating PRODUCTION image tags..."
|
|
- sed -i "s|registry.gitlab.com/.*/backend:.*|$DOCKER_IMAGE_BACKEND:$CI_COMMIT_SHORT_SHA|g" "$KUSTOMIZE_PATH/patch-deployment.yaml"
|
|
- sed -i "s|registry.gitlab.com/.*/frontend:.*|$DOCKER_IMAGE_FRONTEND:$CI_COMMIT_SHORT_SHA|g" "$KUSTOMIZE_PATH/patch-deployment.yaml"
|
|
- git add .
|
|
- git commit -m "🚀 Deploy PRODUCTION $CI_COMMIT_SHORT_SHA [skip ci]" || echo "⚠️ No changes to commit"
|
|
- git push "https://$GITOPS_USERNAME:$GITOPS_TOKEN@gitlab.com/mauuldya/datasiswa-workflow/datasiswa-gitops.git" main
|
|
environment:
|
|
name: production
|
|
only:
|
|
- production
|