Update .gitlab-ci.yml file

This commit is contained in:
kris
2026-01-09 04:19:24 +00:00
parent 29933a5df9
commit b7a3882f20
+84 -85
View File
@@ -31,20 +31,22 @@ build_staging:
stage: build stage: build
rules: rules:
- if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "staging"' - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "staging"'
script: script: |
- set -e set -e
- docker info docker info
- echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin "$CI_REGISTRY"
- echo "✅ Build image: $IMAGE_NAME" echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin "$CI_REGISTRY"
- docker build -t "$IMAGE_NAME" -f Dockerfile .
- echo "✅ Push image: $IMAGE_NAME" echo "✅ Build image: $IMAGE_NAME"
- docker push "$IMAGE_NAME" docker build -t "$IMAGE_NAME" -f Dockerfile .
echo "✅ Push image: $IMAGE_NAME"
docker push "$IMAGE_NAME"
echo "✅ Tag latest: $IMAGE_LATEST"
docker tag "$IMAGE_NAME" "$IMAGE_LATEST"
docker push "$IMAGE_LATEST"
- echo "✅ Tag latest: $IMAGE_LATEST"
- docker tag "$IMAGE_NAME" "$IMAGE_LATEST"
- docker push "$IMAGE_LATEST"
# ========================= # =========================
# MIGRATE (AUTO) - JOIN COMPOSE NETWORK # MIGRATE (AUTO) - JOIN COMPOSE NETWORK
@@ -56,76 +58,72 @@ migrate_staging:
needs: needs:
- job: build_staging - job: build_staging
artifacts: false artifacts: false
script: script: |
- set -e set -e
- echo "✅ Running migrations (staging) ..." echo "✅ Running migrations (staging) ..."
# ✅ masuk deploy dir (ada .env + docker-compose) cd "$DEPLOY_DIR"
- cd "$DEPLOY_DIR" test -f "$COMPOSE_FILE" || (echo "❌ $COMPOSE_FILE not found in $DEPLOY_DIR" && exit 1)
- test -f "$COMPOSE_FILE" || (echo "❌ $COMPOSE_FILE not found in $DEPLOY_DIR" && exit 1) test -f .env || (echo "❌ .env not found in $DEPLOY_DIR" && exit 1)
- test -f .env || (echo "❌ .env not found in $DEPLOY_DIR" && exit 1)
# ✅ load env dari server # ✅ load env dari server
- set -a set -a
- . ./.env . ./.env
- set +a set +a
# ✅ pastikan DB env ada # ✅ validasi
- test -n "$DB_HOST" || (echo "❌ DB_HOST empty" && exit 1) test -n "$DB_HOST" || (echo "❌ DB_HOST empty" && exit 1)
- test -n "$DB_PORT" || (echo "❌ DB_PORT empty" && exit 1) test -n "$DB_PORT" || (echo "❌ DB_PORT empty" && exit 1)
- test -n "$DB_USER" || (echo "❌ DB_USER empty" && exit 1) test -n "$DB_USER" || (echo "❌ DB_USER empty" && exit 1)
- test -n "$DB_PASSWORD" || (echo "❌ DB_PASSWORD empty" && exit 1) test -n "$DB_PASSWORD" || (echo "❌ DB_PASSWORD empty" && exit 1)
- test -n "$DB_NAME" || (echo "❌ DB_NAME empty" && exit 1) test -n "$DB_NAME" || (echo "❌ DB_NAME empty" && exit 1)
# ✅ generate DATABASE_URL export DATABASE_URL="postgres://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}?sslmode=${DB_SSLMODE:-disable}"
- export DATABASE_URL="postgres://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}?sslmode=${DB_SSLMODE:-disable}" echo "✅ DATABASE_URL=$DATABASE_URL"
- echo "✅ DATABASE_URL=$DATABASE_URL"
# ✅ pastikan postgres container hidup supaya network exist # ✅ Pastikan postgres & redis ON (sesuaikan nama service compose kamu!)
- echo "✅ Ensuring postgres & redis running ..." echo "✅ Ensuring postgres & redis running ..."
- docker compose -f "$COMPOSE_FILE" up -d postgres-sso redis-sso || true docker compose -f "$COMPOSE_FILE" up -d stg-postgres-lti stg-redis-lti || true
# NOTE: ganti postgres-sso/redis-sso sesuai nama service di docker-compose lti kamu:
# kalau lti compose pakai stg-postgres-lti / stg-redis-lti, ganti di line ini.
# ✅ ambil network name compose (1st network) # ✅ Ambil network key dari compose
- export COMPOSE_NETWORK="$(docker compose -f "$COMPOSE_FILE" config | awk '/networks:/ {getline; print $1}' | tr -d ':')" COMPOSE_NETWORK_KEY="$(docker compose -f "$COMPOSE_FILE" config | awk '/networks:/ {getline; print $1}' | tr -d ':')"
- echo "✅ Compose network key: $COMPOSE_NETWORK" echo "✅ Compose network key: $COMPOSE_NETWORK_KEY"
# ✅ ambil nama network aktual di docker (prefix foldername_) # ✅ Cari network name yang dipakai docker
- export NETWORK_NAME="$(docker network ls --format '{{.Name}}' | grep "_${COMPOSE_NETWORK}$" | head -n 1)" NETWORK_NAME="$(docker network ls --format '{{.Name}}' | grep "_${COMPOSE_NETWORK_KEY}$" | head -n 1)"
- test -n "$NETWORK_NAME" || (echo "❌ Cannot find docker network for compose ($COMPOSE_NETWORK)" && exit 1) test -n "$NETWORK_NAME" || (echo "❌ Cannot find docker network for compose ($COMPOSE_NETWORK_KEY)" && exit 1)
- echo "✅ Docker network detected: $NETWORK_NAME"
# ✅ migrations dari repo (CI workspace) echo "✅ Docker network detected: $NETWORK_NAME"
- echo "✅ Checking migrations from repo..."
- ls -lah "$CI_PROJECT_DIR/internal/database/migrations"
# ✅ run migrate (JOIN NETWORK) # ✅ Migrations dari repo (CI workspace)
- echo "✅ Running migrations via migrate/migrate container ..." echo "✅ Checking migrations from repo..."
- set +e ls -lah "$CI_PROJECT_DIR/internal/database/migrations"
- out=$(docker run --rm \
--network "$NETWORK_NAME" \
-v "$CI_PROJECT_DIR/internal/database/migrations:/migrations:ro" \
migrate/migrate:v4.15.2 \
-path=/migrations -database "$DATABASE_URL" up 2>&1)
- code=$?
- set -e
- echo "$out" echo "✅ Running migrations via migrate/migrate container"
set +e
out=$(docker run --rm \
--network "$NETWORK_NAME" \
-v "$CI_PROJECT_DIR/internal/database/migrations:/migrations:ro" \
migrate/migrate:v4.15.2 \
-path=/migrations -database "$DATABASE_URL" up 2>&1)
code=$?
set -e
# ✅ handle no change properly echo "$out"
- |
if echo "$out" | grep -qi "no change"; then
echo "✅ No change (already up to date)"
exit 0
fi
if [ $code -ne 0 ]; then # ✅ Handle no change dengan benar (tidak false-success)
echo "❌ Migration failed with exit code $code" if echo "$out" | grep -qi "no change"; then
exit $code echo "✅ No change (already up to date)"
fi exit 0
fi
if [ $code -ne 0 ]; then
echo "❌ Migration failed with exit code $code"
exit $code
fi
echo "✅ Migration applied successfully"
echo "✅ Migration applied successfully"
# ========================= # =========================
# DEPLOY (AUTO) # DEPLOY (AUTO)
@@ -139,21 +137,22 @@ deploy_staging:
artifacts: false artifacts: false
- job: build_staging - job: build_staging
artifacts: false artifacts: false
script: script: |
- set -e set -e
- docker info docker info
- echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin "$CI_REGISTRY" echo "$CI_REGISTRY_PASSWORD" | docker login -u "$CI_REGISTRY_USER" --password-stdin "$CI_REGISTRY"
- cd "$DEPLOY_DIR" cd "$DEPLOY_DIR"
- test -f "$COMPOSE_FILE" || (echo "❌ $COMPOSE_FILE not found in $DEPLOY_DIR" && exit 1) test -f "$COMPOSE_FILE" || (echo "❌ $COMPOSE_FILE not found in $DEPLOY_DIR" && exit 1)
- test -f .env || (echo "❌ .env not found in $DEPLOY_DIR" && exit 1) test -f .env || (echo "❌ .env not found in $DEPLOY_DIR" && exit 1)
docker compose -f "$COMPOSE_FILE" pull
docker compose -f "$COMPOSE_FILE" up -d --force-recreate
docker image prune -f
- docker compose -f "$COMPOSE_FILE" pull
- docker compose -f "$COMPOSE_FILE" up -d --force-recreate
- docker image prune -f
# ========================= # =========================
# SEED (MANUAL) - OPTIONAL # SEED (MANUAL)
# ========================= # =========================
seed_staging: seed_staging:
stage: seed stage: seed
@@ -164,11 +163,11 @@ seed_staging:
artifacts: false artifacts: false
when: manual when: manual
allow_failure: false allow_failure: false
script: script: |
- set -e set -e
- cd "$DEPLOY_DIR" cd "$DEPLOY_DIR"
- test -f "$COMPOSE_FILE" || (echo "❌ $COMPOSE_FILE not found" && exit 1) test -f "$COMPOSE_FILE" || (echo "❌ $COMPOSE_FILE not found" && exit 1)
- test -f .env || (echo "❌ .env not found" && exit 1) test -f .env || (echo "❌ .env not found" && exit 1)
- docker compose -f "$COMPOSE_FILE" pull seed || true docker compose -f "$COMPOSE_FILE" pull seed || true
- docker compose -f "$COMPOSE_FILE" run --rm seed docker compose -f "$COMPOSE_FILE" run --rm seed