Deploy to LXC / deploy (push) Has been cancelled
The service can take longer than 2s to finish starting after restart (DB pool init, etc), so systemctl is-active would occasionally still report "activating" at the fixed check point -- CI marked the run failed even though the app came up moments later and was actually fine. Poll for up to 15s instead, and dump service status on a real failure so debugging doesn't require SSH access.
76 lines
2.2 KiB
YAML
76 lines
2.2 KiB
YAML
name: Deploy to LXC
|
|
|
|
on:
|
|
push:
|
|
branches: [main]
|
|
|
|
jobs:
|
|
deploy:
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- name: Deploy via SSH
|
|
uses: appleboy/ssh-action@v1
|
|
with:
|
|
host: ${{ secrets.DEPLOY_HOST }}
|
|
username: ${{ secrets.DEPLOY_USER }}
|
|
key: ${{ secrets.DEPLOY_KEY }}
|
|
port: ${{ secrets.DEPLOY_PORT || 22 }}
|
|
script: |
|
|
set -e
|
|
|
|
APP_DIR="${{ secrets.DEPLOY_PATH || '/home/bflr/buildfor_life_repair' }}"
|
|
|
|
# Set up deploy key for private repo access
|
|
mkdir -p ~/.ssh
|
|
echo "${{ secrets.REPO_DEPLOY_KEY }}" > ~/.ssh/repo_deploy_key
|
|
chmod 600 ~/.ssh/repo_deploy_key
|
|
|
|
# Configure SSH to use deploy key for git.b4l.co.th
|
|
if ! grep -q "git.b4l.co.th" ~/.ssh/config 2>/dev/null; then
|
|
cat >> ~/.ssh/config <<EOF
|
|
Host git.b4l.co.th
|
|
HostName git.b4l.co.th
|
|
IdentityFile ~/.ssh/repo_deploy_key
|
|
StrictHostKeyChecking accept-new
|
|
EOF
|
|
chmod 600 ~/.ssh/config
|
|
fi
|
|
|
|
# Clone if first deploy, otherwise pull
|
|
if [ ! -d "$APP_DIR" ]; then
|
|
echo "==> First deploy, cloning..."
|
|
git clone git@git.b4l.co.th:B4L/buildfor_life_repair.git "$APP_DIR"
|
|
cd "$APP_DIR"
|
|
else
|
|
cd "$APP_DIR"
|
|
echo "==> Resetting local changes..."
|
|
git checkout -- .
|
|
echo "==> Pulling latest code..."
|
|
git pull origin main
|
|
fi
|
|
|
|
echo "==> Installing dependencies..."
|
|
npm ci
|
|
|
|
echo "==> Building..."
|
|
npm run build
|
|
|
|
echo "==> Running migrations..."
|
|
npm run db:push
|
|
|
|
echo "==> Restarting service..."
|
|
sudo systemctl restart bflr
|
|
|
|
echo "==> Waiting for startup..."
|
|
for i in $(seq 1 15); do
|
|
if systemctl is-active --quiet bflr; then
|
|
echo "Deploy successful!"
|
|
exit 0
|
|
fi
|
|
sleep 1
|
|
done
|
|
|
|
echo "Service failed to start!"
|
|
sudo systemctl status bflr --no-pager -l || true
|
|
exit 1
|