ci: add Docker build + GitLab Container Registry push before deploy

This commit is contained in:
asepharyana
2026-07-02 02:11:46 +07:00
parent 0da9839b3a
commit 90b317731b
+95 -18
View File
@@ -1,49 +1,126 @@
# ─── BETE GitLab CI/CD Pipeline ───────────────────────────────────────────────
# Simply SSH into VPS → git pull → docker compose up -d --build
# 1. Build 4 Docker images (frontend, backend, discord-gateway, proxy)
# 2. Push to GitLab Container Registry
# 3. Deploy to VPS — pull images, docker compose up
#
# Required CI/CD Variables (set in GitLab → Settings → CI/CD → Variables):
#
# VPS_HOST - VPS IP/hostname (e.g. 45.127.35.244)
# VPS_USERNAME - SSH user (e.g. root)
# VPS_HOST - VPS IP/hostname
# VPS_USERNAME - SSH user
# VPS_SSH_KEY - SSH private key (type: file)
#
# Notes:
# - VPS must have git access to this repo (deploy key or SSH agent)
# - .env file must already exist on VPS at $APP_DIR/.env
# - VPS_SSH_KEY must be "File" type so GitLab writes it to disk
# ENV_FILE - Full .env file content (type: env_var) — optional if .env already on VPS
#
# ──────────────────────────────────────────────────────────────────────────────
stages:
- build
- deploy
variables:
APP_DIR: /opt/imphenbot
SSH_HOST: "${VPS_USERNAME}@${VPS_HOST}"
REGISTRY: $CI_REGISTRY
REGISTRY_PROJECT: $CI_REGISTRY/$CI_PROJECT_PATH
IMAGE_TAG_COMMIT: $CI_COMMIT_SHA
IMAGE_TAG_LATEST: latest
# Frontend build args
VITE_BE_API_URL: https://imphnen.asepharyana.my.id
VITE_BE_WS_URL: wss://imphnen.asepharyana.my.id
# Deploy target
SSH_HOST: "${VPS_USERNAME}@${VPS_HOST}"
APP_DIR: /opt/imphenbot
# ── Build stage ───────────────────────────────────────────────────────────────
.docker-build:
stage: build
image: docker:27-cli
tags:
- docker
services:
- docker:27-dind
before_script:
- echo "$CI_JOB_TOKEN" | docker login "$CI_REGISTRY" -u "$CI_REGISTRY_USER" --password-stdin
script:
# Build image
- |
docker build \
--file infra/docker/Dockerfile.$SERVICE_NAME \
--tag $REGISTRY_PROJECT/bete-$SERVICE_NAME:$IMAGE_TAG_COMMIT \
--tag $REGISTRY_PROJECT/bete-$SERVICE_NAME:$IMAGE_TAG_LATEST \
--build-arg BUILDKIT_INLINE_CACHE=1 \
--build-arg VITE_BE_API_URL=$VITE_BE_API_URL \
--build-arg VITE_BE_WS_URL=$VITE_BE_WS_URL \
--cache-from $REGISTRY_PROJECT/bete-$SERVICE_NAME:latest \
.
# Push to GitLab Container Registry
- docker push $REGISTRY_PROJECT/bete-$SERVICE_NAME:$IMAGE_TAG_COMMIT
- docker push $REGISTRY_PROJECT/bete-$SERVICE_NAME:$IMAGE_TAG_LATEST
build-frontend:
extends: .docker-build
variables:
SERVICE_NAME: frontend
only:
- master
build-backend:
extends: .docker-build
variables:
SERVICE_NAME: backend
only:
- master
build-discord-gateway:
extends: .docker-build
variables:
SERVICE_NAME: discord-gateway
only:
- master
build-proxy:
extends: .docker-build
variables:
SERVICE_NAME: proxy
only:
- master
# ── Deploy stage ──────────────────────────────────────────────────────────────
deploy-vps:
stage: deploy
image: debian:stable-slim
only:
- master
needs:
- build-frontend
- build-backend
- build-discord-gateway
- build-proxy
before_script:
- apt-get update -qq && apt-get install -y -qq openssh-client git
- apt-get update -qq && apt-get install -y -qq openssh-client
- mkdir -p ~/.ssh
- cp "$VPS_SSH_KEY" ~/.ssh/id_rsa
- chmod 600 ~/.ssh/id_rsa
- ssh-keyscan -H "$VPS_HOST" >> ~/.ssh/known_hosts 2>/dev/null
script:
# Login to GitLab Container Registry, pull images, update docker-compose, restart
- |
ssh "$SSH_HOST" "
set -eu
echo '→ Pulling latest code...'
cd $APP_DIR
git fetch --depth 1 origin master
git checkout master
git reset --hard origin/master
echo '→ Rebuilding and restarting containers...'
docker compose -f infra/docker/docker-compose.yml up -d --build --remove-orphans
cd $APP_DIR
echo '→ Logging in to GitLab Container Registry...'
echo '$CI_JOB_TOKEN' | docker login $CI_REGISTRY -u '$CI_REGISTRY_USER' --password-stdin
echo '→ Updating docker-compose image references...'
sed -i 's|ghcr.io/\${OWNER:-mytheclipse}|$REGISTRY_PROJECT|g' infra/docker/docker-compose.yml
sed -i 's|\${OWNER:-mytheclipse}|$CI_PROJECT_NAMESPACE|g' infra/docker/docker-compose.yml
echo '→ Pulling latest images...'
docker compose -f infra/docker/docker-compose.yml pull
echo '→ Restarting containers...'
docker compose -f infra/docker/docker-compose.yml up -d --remove-orphans
echo '→ Cleaning up...'
docker image prune -f