如何在多阶段 docker 构建中跳过不相关的步骤

How to skip irrelevant steps on multi-stage docker build

我正在尝试 运行 一个包含两个并行步骤(testrelease)的多步骤构建过程,并且 运行 解决 [=80] 的问题=] Cloud Build 在 运行 目标多阶段时不会跳过不相关的中间步骤。

Docker 文件

FROM node:18-slim AS base

WORKDIR /app

COPY package.json yarn.lock /app/
ADD src/ /app/src

RUN yarn install

FROM base AS test
RUN yarn test

FROM base AS release
EXPOSE 3000

ENTRYPOINT node src/index.js

cloudbuild.json

{
    "steps": [
        {
            "name": "gcr.io/cloud-builders/docker",
            "id": "Install",
            "args": [
                "build",
                "--target",
                "base",
                "-t",
                "australia-southeast1-docker.pkg.dev/${PROJECT_ID}/cloud-build-tutorial/${REPO_NAME}-install:${SHORT_SHA}",
                "."
            ]
        },
        {
            "name": "gcr.io/cloud-builders/docker",
            "id": "Test",
            "args": [
                "build",
                "--target",
                "test",
                "-t",
                "australia-southeast1-docker.pkg.dev/${PROJECT_ID}/cloud-build-tutorial/${REPO_NAME}-test:${SHORT_SHA}",
                "."
            ],
            "waitFor": ["Install"]
        },
        {
            "name": "gcr.io/cloud-builders/docker",
            "id": "Build",
            "args": [
                "build",
                "--target",
                "release",
                "-t",
                "australia-southeast1-docker.pkg.dev/${PROJECT_ID}/cloud-build-tutorial/${REPO_NAME}:${SHORT_SHA}",
                "."
            ],
            "waitFor": ["Install"]
        }
    ]
}

步骤结果:

我的预期结果是:

我是否以任何方式配置错误或 Google Cloud Build 不支持跳过步骤作为目标多阶段构建的一部分?

编辑: 由于提供了 Answer:

,以下 cloudbuild 配置有效
{
    "steps": [
        {
            "name": "gcr.io/cloud-builders/docker",
            "id": "Install",
            "args": [
                "build",
                "--target",
                "base",
                "-t",
                "australia-southeast1-docker.pkg.dev/${PROJECT_ID}/cloud-build-tutorial/${REPO_NAME}-install:${SHORT_SHA}",
                "."
            ],
            "env": ["DOCKER_BUILDKIT=1"]
        },
        {
            "name": "gcr.io/cloud-builders/docker",
            "id": "Test",
            "args": [
                "build",
                "--target",
                "test",
                "-t",
                "australia-southeast1-docker.pkg.dev/${PROJECT_ID}/cloud-build-tutorial/${REPO_NAME}-test:${SHORT_SHA}",
                "."
            ],
            "env": ["DOCKER_BUILDKIT=1"],
            "waitFor": ["Install"]
        },
        {
            "name": "gcr.io/cloud-builders/docker",
            "id": "Build",
            "args": [
                "build",
                "--target",
                "release",
                "-t",
                "australia-southeast1-docker.pkg.dev/${PROJECT_ID}/cloud-build-tutorial/${REPO_NAME}:${SHORT_SHA}",
                "."
            ],
            "env": ["DOCKER_BUILDKIT=1"],
            "waitFor": ["Install"]
        }
    ]
}

构建阶段的依赖图是buildkit的一个特性。默认情况下,google 的云构建器镜像仍在使用经典的 docker 构建器,但您可以切换到 buildkit:

  env:
    - "DOCKER_BUILDKIT=1"

在您的构建步骤中。然而,最后检查,upgrading the docker engine is still pending.