Files
Lumina-MSK/.gitea/workflows/deploy-cd-server.yaml
DatTT127 5df5f2da24
All checks were successful
Backend EC - Modal Deployment / build-and-push (push) Successful in 10m7s
Backend EC - Modal Deployment / notify-deploy (push) Successful in 3s
update ecr
2026-07-20 00:59:34 +07:00

124 lines
4.4 KiB
YAML

name: Deploy CD Server
on:
push:
branches: ['main']
paths:
- 'workspace/sprint_1_2/CODEBASE/deps/implementation/CD_server/**'
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 1
- name: Prepare VM directories
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ vars.LIGHTSAIL_DEPLOY_HOST }}
username: ${{ secrets.LIGHTSAIL_USERNAME }}
key: ${{ secrets.LIGHTSAIL_SSH_KEY }}
port: ${{ vars.LIGHTSAIL_PORT }}
script: |
set -euo pipefail
DEPLOY_DIR="/opt/cd-server"
sudo mkdir -p "$DEPLOY_DIR"
sudo rm -rf "$DEPLOY_DIR"/*
sudo chown $USER:$USER "$DEPLOY_DIR"
sudo chmod 755 "$DEPLOY_DIR"
echo "[prepare] $DEPLOY_DIR ready"
- name: Copy CD server code to VM
uses: appleboy/scp-action@v0.1.7
with:
host: ${{ vars.LIGHTSAIL_DEPLOY_HOST }}
username: ${{ secrets.LIGHTSAIL_USERNAME }}
key: ${{ secrets.LIGHTSAIL_SSH_KEY }}
port: ${{ vars.LIGHTSAIL_PORT }}
source: "workspace/sprint_1_2/CODEBASE/deps/implementation/CD_server/"
target: "/opt/cd-server/"
strip_components: 6
- name: Deploy to Lightsail VM
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ vars.LIGHTSAIL_DEPLOY_HOST }}
username: ${{ secrets.LIGHTSAIL_USERNAME }}
key: ${{ secrets.LIGHTSAIL_SSH_KEY }}
port: ${{ vars.LIGHTSAIL_PORT }}
script: |
set -euo pipefail
DEPLOY_DIR="/opt/cd-server"
SERVICE_NAME="cd-server"
echo "[deploy] Creating deploy user if missing"
if ! id -u deploy &>/dev/null; then
sudo useradd -m -s /bin/bash deploy
fi
echo "[deploy] Adding deploy user to docker group"
sudo usermod -aG docker deploy || true
echo "[deploy] Ensuring $DEPLOY_DIR ownership"
sudo chown -R deploy:docker "$DEPLOY_DIR"
echo "[deploy] Creating .env file"
sudo -u deploy bash -c "echo 'WEBHOOK_SECRET=${{ secrets.WEBHOOK_SECRET }}' >> $DEPLOY_DIR/.env"
sudo -u deploy bash -c "echo 'AWS_ECR_REGION=${{ secrets.AWS_ECR_REGION }}' >> $DEPLOY_DIR/.env"
sudo chmod 600 "$DEPLOY_DIR/.env"
echo "[deploy] Building CD server Docker image"
sudo -u deploy docker compose -f "$DEPLOY_DIR/docker-compose.yml" build --no-cache
echo "[deploy] Starting CD server container"
sudo -u deploy docker compose -f "$DEPLOY_DIR/docker-compose.yml" up -d --force-recreate
echo "[deploy] Waiting for container to be running"
for i in {1..15}; do
if docker ps --filter "name=$SERVICE_NAME" --filter "status=running" --format "{{.ID}}" | grep -q .; then
echo "[deploy] Container is running"
break
fi
if [ "$i" -eq 15 ]; then
echo "[deploy] Container failed to start"
docker ps -a --filter "name=$SERVICE_NAME" --format "table {{.Names}}\t{{.Status}}"
docker logs "$SERVICE_NAME" || true
exit 1
fi
sleep 2
done
echo "[deploy] Waiting for service to be healthy"
for i in {1..30}; do
if curl -s http://127.0.0.1:3333/health > /dev/null; then
echo "[deploy] Service is healthy"
break
fi
if [ "$i" -eq 30 ]; then
echo "[deploy] Service failed health check"
docker logs "$SERVICE_NAME" || true
exit 1
fi
sleep 2
done
echo "[deploy] Waiting for Caddy HTTPS endpoint"
for i in {1..30}; do
if curl -sk https://deploy.lumina-msk.io.vn/health > /dev/null 2>&1; then
echo "[deploy] Caddy HTTPS endpoint is healthy"
break
fi
if [ "$i" -eq 30 ]; then
echo "[deploy] Caddy HTTPS endpoint failed health check"
docker logs caddy || true
exit 1
fi
sleep 2
done
echo "[deploy] Deployment completed successfully"