From 99463a762e7a47016b95eef7aaec07743787e408 Mon Sep 17 00:00:00 2001 From: sky121113 Date: Tue, 21 Apr 2026 16:59:27 +0800 Subject: [PATCH] =?UTF-8?q?[REFACTOR]=20=E7=B0=A1=E5=8C=96=20CI/CD=20?= =?UTF-8?q?=E9=83=A8=E7=BD=B2=E8=85=B3=E6=9C=AC=20-=20=E5=BE=9E=E6=A0=B9?= =?UTF-8?q?=E6=9C=AC=E8=A7=A3=E6=B1=BA=20tar=20=E5=95=8F=E9=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 問題根源: - 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 行。 --- .gitea/workflows/deploy-demo.yaml | 48 ++++--------------------------- 1 file changed, 6 insertions(+), 42 deletions(-) diff --git a/.gitea/workflows/deploy-demo.yaml b/.gitea/workflows/deploy-demo.yaml index 98bd19f..a37face 100644 --- a/.gitea/workflows/deploy-demo.yaml +++ b/.gitea/workflows/deploy-demo.yaml @@ -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 &&