ci: speed up docker build with BuildKit cache mount and layer caching
This commit is contained in:
15
.drone.yml
15
.drone.yml
@@ -162,6 +162,8 @@ steps:
|
||||
volumes:
|
||||
- name: dockersock
|
||||
path: /var/run/docker.sock
|
||||
environment:
|
||||
DOCKER_BUILDKIT: "1"
|
||||
commands:
|
||||
- |
|
||||
echo "====== 开始构建 Docker 镜像 ======"
|
||||
@@ -174,11 +176,20 @@ steps:
|
||||
fi
|
||||
echo "版本: $APP_VERSION"
|
||||
|
||||
# 拉取之前的镜像作为缓存来源(加速 layer 复用)
|
||||
echo "正在拉取缓存镜像..."
|
||||
docker pull "$IMAGE_NAME:latest" 2>/dev/null || true
|
||||
docker pull "$LOCAL_HARBOR_REG/$LOCAL_HARBOR_PROJ/$IMAGE_NAME:latest" 2>/dev/null || true
|
||||
|
||||
# 使用 BuildKit 构建,启用缓存
|
||||
docker build \
|
||||
-t "$IMAGE_NAME:$BUILD_VERSION" \
|
||||
-t "$IMAGE_NAME:latest" \
|
||||
--cache-from "$IMAGE_NAME:latest" \
|
||||
--cache-from "$LOCAL_HARBOR_REG/$LOCAL_HARBOR_PROJ/$IMAGE_NAME:latest" \
|
||||
--build-arg BUILDKIT_INLINE_CACHE=1 \
|
||||
--build-arg VERSION="$APP_VERSION" \
|
||||
--build-arg BUILD_TIME="$(TZ=Asia/Shanghai date '+%Y-%m-%d %H:%M')" \
|
||||
-t "$IMAGE_NAME:$BUILD_VERSION" \
|
||||
-t "$IMAGE_NAME:latest" \
|
||||
-f par-api/Dockerfile \
|
||||
.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user