Skip to content

Commit 404b53b

Browse files
committed
fix:bug
1 parent 6602b81 commit 404b53b

File tree

2 files changed

+76
-7
lines changed

2 files changed

+76
-7
lines changed

.github/workflows/build.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ on: [push, pull_request]
55
jobs:
66
build-php:
77
runs-on: ubuntu-latest
8+
timeout-minutes: 60
89
strategy:
10+
fail-fast: false
11+
max-parallel: 3 # 限制并发数量,避免资源争抢
912
matrix:
1013
php_version: ["7.4", "8.0", "8.1", "8.2", "8.3"]
1114
service: ["php-fpm", "php-worker", "workspace"]
@@ -38,6 +41,27 @@ jobs:
3841
3942
echo "=== 环境检查完成 ==="
4043
44+
- name: Docker optimization
45+
run: |
46+
echo "=== Docker优化配置 ==="
47+
48+
# 清理Docker缓存
49+
docker system prune -f || true
50+
51+
# 预热Docker守护进程
52+
echo "预热Docker守护进程..."
53+
docker run --rm hello-world
54+
55+
# 显示Docker存储信息
56+
echo "Docker存储信息:"
57+
docker system df
58+
59+
# 显示当前运行的容器
60+
echo "当前容器:"
61+
docker ps -a
62+
63+
echo "=== Docker优化完成 ==="
64+
4165
- name: Build the Docker image
4266
env:
4367
PHP_VERSION: ${{ matrix.php_version }}
@@ -51,7 +75,9 @@ jobs:
5175
./build-image.sh
5276
build-mysql:
5377
runs-on: ubuntu-latest
78+
timeout-minutes: 45
5479
strategy:
80+
fail-fast: false
5581
matrix:
5682
mysql_version: ["5.7", "8.0"]
5783
service: ["mysql"]
@@ -82,7 +108,10 @@ jobs:
82108
run: ./build-image.sh
83109
build-other:
84110
runs-on: ubuntu-latest
111+
timeout-minutes: 45
85112
strategy:
113+
fail-fast: false
114+
max-parallel: 2 # 限制并发数量
86115
matrix:
87116
service: ["redis", "mongo", "nginx", "elasticsearch"]
88117
steps:

build-image.sh

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,54 @@ cat .env
148148

149149
# 检查并使用正确的docker compose命令
150150
echo "=== 开始构建Docker镜像 ==="
151-
if docker compose version > /dev/null 2>&1; then
152-
echo "使用 docker compose (plugin) 构建镜像..."
153-
docker compose build ${BUILD_SERVICE}
154-
elif docker-compose --version > /dev/null 2>&1; then
155-
echo "使用 docker-compose (standalone) 构建镜像..."
156-
docker-compose build ${BUILD_SERVICE}
151+
152+
# 构建重试函数
153+
build_with_retry() {
154+
local max_attempts=3
155+
local attempt=1
156+
157+
while [ $attempt -le $max_attempts ]; do
158+
echo "构建尝试 $attempt/$max_attempts..."
159+
160+
if docker compose version > /dev/null 2>&1; then
161+
echo "使用 docker compose (plugin) 构建镜像..."
162+
if timeout 3600 docker compose build --no-cache ${BUILD_SERVICE}; then
163+
echo "✅ 构建成功!"
164+
return 0
165+
fi
166+
elif docker-compose --version > /dev/null 2>&1; then
167+
echo "使用 docker-compose (standalone) 构建镜像..."
168+
if timeout 3600 docker-compose build --no-cache ${BUILD_SERVICE}; then
169+
echo "✅ 构建成功!"
170+
return 0
171+
fi
172+
else
173+
echo "❌ 错误: 找不到 docker compose 或 docker-compose 命令"
174+
exit 1
175+
fi
176+
177+
if [ $attempt -lt $max_attempts ]; then
178+
echo "⚠️ 构建失败,等待30秒后重试..."
179+
sleep 30
180+
181+
# 清理Docker缓存
182+
echo "清理Docker构建缓存..."
183+
docker builder prune -f || true
184+
docker system prune -f || true
185+
fi
186+
187+
attempt=$((attempt + 1))
188+
done
189+
190+
echo "❌ 所有构建尝试都失败了"
191+
return 1
192+
}
193+
194+
# 执行构建
195+
if build_with_retry; then
196+
echo "🎉 Docker镜像构建完成!"
157197
else
158-
echo "❌ 错误: 找不到 docker compose 或 docker-compose 命令"
198+
echo "💥 Docker镜像构建失败!"
159199
exit 1
160200
fi
161201
#####################################

0 commit comments

Comments
 (0)