All checks were successful
Deploy to Frontend Servers / deploy (push) Successful in 1m0s
A previous deploy failed at the vite chunk-writing stage with "ENOSPC: no space left on device". The cleanup step ran at the start of the job but left enough stale data behind that the runner filled up before `npm run build` could finish. - Drop the act workspace retention from 60min to 10min. Closely-spaced pushes used to keep multiple stale jobs around; 10min still preserves any currently-running job because its mtime keeps advancing. - Drop _work / setup-node / npm cacache retention from 24h to 60min. - Drop the `until=24h` filter on docker prune so dangling images, containers, and builder cache get reclaimed every run. - Add a second "Ensure free space before build" guard right before the Build step. If <3GB is free, aggressively prune act caches, npm cacache, and docker volumes before vite starts writing chunks.
152 lines
5.4 KiB
YAML
152 lines
5.4 KiB
YAML
name: Deploy to Frontend Servers
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- main
|
|
|
|
jobs:
|
|
deploy:
|
|
runs-on: self-hosted
|
|
|
|
steps:
|
|
- name: Free disk space
|
|
run: |
|
|
set +e
|
|
echo "=== Disk before cleanup ==="
|
|
df -h
|
|
# Stale act runner workspaces. Closely-spaced pushes (e.g. 3 commits
|
|
# within 30min) used to leak workspaces because the old 60min
|
|
# threshold left them in place. 10min is tight but still keeps any
|
|
# currently-running job's dir (its mtime updates as it writes).
|
|
if [ -d "$HOME/.cache/act" ]; then
|
|
du -sh "$HOME/.cache/act" 2>/dev/null
|
|
find "$HOME/.cache/act" -mindepth 1 -maxdepth 1 -type d -mmin +10 -exec rm -rf {} + 2>/dev/null
|
|
fi
|
|
# Stale runner workspaces and node setup/npm caches: 60min is plenty
|
|
# since each job re-fetches deps via `npm ci`.
|
|
for dir in "$HOME/actions-runner/_work" "$HOME/.cache/setup-node" "$HOME/.npm/_cacache"; do
|
|
if [ -d "$dir" ]; then
|
|
find "$dir" -mindepth 1 -maxdepth 2 -mmin +60 -exec rm -rf {} + 2>/dev/null
|
|
fi
|
|
done
|
|
# Docker leftovers: drop the `until=24h` filter so any dangling images
|
|
# / containers / builder cache get reclaimed every run.
|
|
if command -v docker >/dev/null 2>&1; then
|
|
docker image prune -af 2>/dev/null
|
|
docker container prune -f 2>/dev/null
|
|
docker builder prune -af 2>/dev/null
|
|
fi
|
|
# Stale /tmp files older than 2h, keep currently-running runner files.
|
|
find /tmp -mindepth 1 -maxdepth 1 -mmin +120 \
|
|
-not -name 'runner*' -not -name 'act*' \
|
|
-exec rm -rf {} + 2>/dev/null
|
|
echo "=== Disk after cleanup ==="
|
|
df -h
|
|
exit 0
|
|
|
|
- name: Checkout code
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Setup Node.js
|
|
uses: actions/setup-node@v4
|
|
with:
|
|
node-version: "22"
|
|
cache: npm
|
|
|
|
- name: Install dependencies
|
|
run: npm ci
|
|
|
|
- name: Type check
|
|
run: npx tsc --noEmit
|
|
|
|
- name: Format check
|
|
run: npm run format:check
|
|
|
|
- name: Test
|
|
run: npm test
|
|
|
|
- name: Ensure free space before build
|
|
run: |
|
|
# Second-pass guard right before the heaviest step. `npm ci` plus
|
|
# tsc/test pull in tons of files since the first cleanup ran, and
|
|
# vite chunk writes need at least a few GB free — ENOSPC here is what
|
|
# killed past runs.
|
|
set +e
|
|
AVAIL_MB=$(df -Pm . | awk 'NR==2 {print $4}')
|
|
echo "Available on workspace volume: ${AVAIL_MB} MB"
|
|
if [ "${AVAIL_MB:-0}" -lt 3072 ]; then
|
|
echo "<3GB free — running an aggressive cleanup before build."
|
|
rm -rf "$HOME/.cache/act"/*/hostexecutor/node_modules/.cache 2>/dev/null
|
|
rm -rf "$HOME/.npm/_cacache" 2>/dev/null
|
|
if command -v docker >/dev/null 2>&1; then
|
|
docker system prune -af --volumes 2>/dev/null
|
|
fi
|
|
df -h
|
|
fi
|
|
exit 0
|
|
|
|
- name: Build
|
|
run: npm run build
|
|
env:
|
|
VITE_API_URL: ""
|
|
VITE_API_PREFIX: "/apnew"
|
|
VITE_DISABLE_ADMIN: "true"
|
|
|
|
- name: Setup SSH key
|
|
run: |
|
|
mkdir -p ~/.ssh
|
|
echo "${{ secrets.DEPLOY_KEY }}" > ~/.ssh/deploy_key
|
|
chmod 600 ~/.ssh/deploy_key
|
|
ssh-keyscan -H ${{ secrets.FRONTEND_1_HOST }} >> ~/.ssh/known_hosts 2>/dev/null
|
|
ssh-keyscan -H ${{ secrets.FRONTEND_2_HOST }} >> ~/.ssh/known_hosts 2>/dev/null
|
|
|
|
- name: Deploy to both servers
|
|
run: |
|
|
set -euo pipefail
|
|
deploy_to() {
|
|
local HOST=$1
|
|
echo ">>> 部署到 $HOST"
|
|
rsync -avz --delete \
|
|
-e "ssh -i ~/.ssh/deploy_key -o StrictHostKeyChecking=no" \
|
|
dist/ \
|
|
ec2-user@${HOST}:/var/www/ark-library/
|
|
echo ">>> $HOST 部署完成"
|
|
}
|
|
deploy_to "${{ secrets.FRONTEND_1_HOST }}" &
|
|
PID1=$!
|
|
deploy_to "${{ secrets.FRONTEND_2_HOST }}" &
|
|
PID2=$!
|
|
FAIL=0
|
|
wait $PID1 || { echo "ERROR: frontend-1 部署失败"; FAIL=1; }
|
|
wait $PID2 || { echo "ERROR: frontend-2 部署失败"; FAIL=1; }
|
|
[ $FAIL -eq 0 ] || exit 1
|
|
echo "=== 两台都部署完成 ==="
|
|
|
|
- name: Verify both servers match local build
|
|
run: |
|
|
set -euo pipefail
|
|
LOCAL=$(sha256sum dist/index.html | awk '{print $1}')
|
|
SUM1=$(ssh -i ~/.ssh/deploy_key -o StrictHostKeyChecking=no \
|
|
ec2-user@${{ secrets.FRONTEND_1_HOST }} \
|
|
"sha256sum /var/www/ark-library/index.html | awk '{print \$1}'")
|
|
SUM2=$(ssh -i ~/.ssh/deploy_key -o StrictHostKeyChecking=no \
|
|
ec2-user@${{ secrets.FRONTEND_2_HOST }} \
|
|
"sha256sum /var/www/ark-library/index.html | awk '{print \$1}'")
|
|
echo "local: $LOCAL"
|
|
echo "frontend-1: $SUM1"
|
|
echo "frontend-2: $SUM2"
|
|
if [ "$SUM1" != "$LOCAL" ]; then
|
|
echo "ERROR: frontend-1 不是本次构建的版本"
|
|
exit 1
|
|
fi
|
|
if [ "$SUM2" != "$LOCAL" ]; then
|
|
echo "ERROR: frontend-2 不是本次构建的版本"
|
|
exit 1
|
|
fi
|
|
echo "✓ 两台都已经更新到本次构建的版本。"
|
|
|
|
- name: Cleanup SSH key
|
|
if: always()
|
|
run: rm -f ~/.ssh/deploy_key
|