94 lines
3.2 KiB
YAML
94 lines
3.2 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 chown deploy:docker "$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: 0
|
|
|
|
- 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 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"
|
|
sudo docker logs cd-server --no-pager || true
|
|
exit 1
|
|
fi
|
|
sleep 2
|
|
done
|
|
|
|
echo "[deploy] Deployment completed successfully"
|