[REFACTOR] 簡化 CI/CD 部署腳本 - 從根本解決 tar 問題

問題根源:
- tar 的 'file changed as we read it' 是警告 (exit 1),非錯誤
- 之前用 PIPESTATUS、pipefail toggle、wait_for_container 等 workaround
  處理,導致腳本越來越複雜且脆弱

根本解法:
1. tar --warning=no-file-changed: 讓 tar 忽略此警告並維持 exit 0
2. 三個容器共享同一份 volume,只需同步一次到主容器
3. 移除所有 workaround 程式碼 (PIPESTATUS/pipefail/wait_for_container)

Step 2 從 ~50 行縮減至 ~15 行。
This commit is contained in:
sky121113 2026-04-21 16:59:27 +08:00
parent a15eeabde7
commit 99463a762e

View File

@ -40,51 +40,15 @@ jobs:
WWWGROUP=1000 WWWUSER=1000 docker compose -f compose.yaml up -d --build --wait
- name: Step 2 - Deploy Code
shell: bash
run: |
# 等待容器進入 running 狀態 (最多 30 秒)
wait_for_container() {
local container=$1
local max_attempts=15
local attempt=0
while [ $attempt -lt $max_attempts ]; do
local state=$(docker inspect --format='{{.State.Status}}' "$container" 2>/dev/null)
if [ "$state" = "running" ]; then
return 0
fi
attempt=$((attempt + 1))
echo " Waiting for $container (state=$state, attempt $attempt/$max_attempts)..."
sleep 2
done
echo "!!! Error: $container did not reach running state (last state=$state)"
return 1
}
echo ">>> Syncing code to star-cloud-laravel..."
# 三個容器共享同一份 volume (.:/var/www/html)
# 只需同步至主容器一次queue/mqtt-worker 會自動看到變更。
#
# 注意tar 在檔案讀取過程中若被修改會回傳 exit 1 (警告,非錯誤)。
# Gitea Actions 預設啟用 pipefail會讓此警告變成管線失敗故需暫時關閉。
set +o pipefail
tar --exclude='.git' --exclude='node_modules' --exclude='vendor' --exclude='storage' --exclude='bootstrap/cache' -cf - . \
| docker exec -i star-cloud-laravel tar -xf - -C /var/www/html/
local_tar=${PIPESTATUS[0]} local_docker=${PIPESTATUS[1]}
set -o pipefail
# --warning=no-file-changed: 抑制 "file changed as we read it" 警告並維持 exit 0
# 三個容器 (laravel, queue, mqtt-worker) 共享同一份 volume (.:/var/www/html),只需同步一次
tar --warning=no-file-changed \
--exclude='.git' --exclude='node_modules' --exclude='vendor' \
--exclude='storage' --exclude='bootstrap/cache' \
-cf - . | docker exec -i star-cloud-laravel tar -xf - -C /var/www/html/
echo " Result: tar=$local_tar, docker=$local_docker"
if [ "$local_tar" -gt 1 ] || [ "$local_docker" -ne 0 ]; then
echo "!!! Error: Sync failed (tar=$local_tar, docker=$local_docker)"
exit 1
fi
# 程式碼同步後queue/mqtt-worker 可能因檔案變動而短暫重啟,等待它們恢復
echo ">>> Waiting for worker containers to stabilize..."
wait_for_container star-cloud-queue || exit 1
wait_for_container star-cloud-mqtt-worker || exit 1
echo ">>> All containers are running."
# 設定權限
docker exec star-cloud-laravel sh -c "
chown -R 1000:1000 /var/www/html &&
mkdir -p /.npm && chown -R 1000:1000 /.npm &&