Home > front end >  How to skip irrelevant steps on multi-stage docker build
How to skip irrelevant steps on multi-stage docker build

Time:04-27

I am attempting to run a multi-step build process with two parallel steps (test and release) and running into issues where Google Cloud Build does not skip the irrelevant intermediary steps when running a targeted multi-stage.

Dockerfile

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"]
        }
    ]
}

Step Outcomes:

  • Install: Runs only the base step from Dockerfile.
  • Test: Runs only the test step from Dockerfile after utilizing cache from Install step.
  • Build: Runs both the test and release steps from Dockerfile after utilizing cache from Install step.

My intended outcome is:

  • Install: Runs only the base step from Dockerfile.
  • Test: Runs only the test step from Dockerfile after utilizing cache from Install step.
  • Build: Runs only therelease step from Dockerfile after utilizing cache from Install step.

Have I misconfigured the configurations in any way or does Google Cloud Build not support skipping steps as part of a targetted multi-stage build?

EDIT: The following cloudbuild config works, thanks to the provided Answer:

{
    "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"]
        }
    ]
}

CodePudding user response:

Building the dependency graph of stages is a feature of buildkit. By default, google's cloud builder image is still using the classic docker builder, but you may be able to switch to buildkit with:

  env:
    - "DOCKER_BUILDKIT=1"

within your build steps. However, at last check, upgrading the docker engine is still pending.

  • Related