[FIX] 修復 CI/CD 部署時序競爭 - 容器重啟導致 docker exec 隨機失敗

根因:queue/mqtt-worker 容器可能處於 restarting 狀態,
docker exec 在此狀態下會被拒絕 (exit 1),導致部署隨機失敗。

修復:在 deploy_to 函式中加入 wait_for_container 重試邏輯,
在執行 docker exec 前先等待容器進入 running 狀態 (最多 30 秒)。
This commit is contained in:
sky121113 2026-04-21 16:52:45 +08:00
parent c1bf9294cd
commit 88741dc5ae

View File

@ -42,9 +42,31 @@ jobs:
- 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
}
deploy_to() {
local container=$1
echo ">>> Syncing code to $container..."
# 先確認容器已就緒,避免 restarting 狀態下 docker exec 被拒絕
wait_for_container "$container" || return 1
tar --exclude='.git' --exclude='node_modules' --exclude='vendor' --exclude='storage' --exclude='bootstrap/cache' -cf - . \
| docker exec -i "$container" tar -xf - -C /var/www/html/