From 0dd5332fd15b0e479a3562101616ab8fba7de7a0 Mon Sep 17 00:00:00 2001 From: Joel Scheuner Date: Wed, 18 Nov 2026 13:35:18 +0100 Subject: [PATCH] ci(docker): speed up the image build - Install SAM CLI from its official installer (pinned, SHA-256 checked) and Snowflake CLI into its own venv. Sharing one venv made pip backtrack over conflicting `rich` pins (100 s amd64, 18 min arm64 under QEMU) and pick SAM 1.142.1 and Snowflake CLI 3.16.0. - Build the JS builder stage on $BUILDPLATFORM, so multi-arch builds compile dist/ once instead of once per platform under QEMU. - Check each tool's version in its own layer instead of in a final step that reran on every commit. - Drop the GHA build cache: exporting it took 5-19 min per smoke run, even when every layer was a cache hit. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/docker.yml | 4 --- Dockerfile | 60 ++++++++++++++++++++++++++---------- 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index ba07b96..7cfb911 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -36,8 +36,6 @@ jobs: context: . load: true tags: ${{ env.IMAGE_NAME }}:ci - cache-from: type=gha - cache-to: type=gha,mode=max - name: Use Node.js 22 uses: actions/setup-node@v4 @@ -115,8 +113,6 @@ jobs: provenance: false tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} - cache-from: type=gha - cache-to: type=gha,mode=max - name: Sync README to Docker Hub overview if: github.ref == 'refs/heads/main' diff --git a/Dockerfile b/Dockerfile index 8210e86..1a351c1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,8 @@ # syntax=docker/dockerfile:1 -FROM node:22-bookworm-slim AS builder +# The builder only emits platform-independent JS (dist/), so it runs on the build host's +# platform. A multi-arch build then compiles once instead of once per platform under QEMU. +FROM --platform=$BUILDPLATFORM node:22-bookworm-slim AS builder WORKDIR /app COPY package.json yarn.lock ./ @@ -10,8 +12,7 @@ RUN yarn build FROM node:22-bookworm-slim AS runtime ENV DEBIAN_FRONTEND=noninteractive \ - PYTHONDONTWRITEBYTECODE=1 \ - PATH="/opt/venv/bin:${PATH}" + PYTHONDONTWRITEBYTECODE=1 RUN set -eux; \ apt-get update; \ @@ -26,21 +27,49 @@ RUN set -eux; \ apt-get update; \ apt-get install -y --no-install-recommends terraform; \ apt-get clean; \ - rm -rf /var/lib/apt/lists/* + rm -rf /var/lib/apt/lists/*; \ + terraform version -RUN python3 -m venv /opt/venv \ - && pip install --no-cache-dir --upgrade pip \ - && pip install --no-cache-dir --no-compile \ - aws-sam-cli \ - snowflake-cli \ - && find /opt/venv/lib \ +# SAM CLI comes from its official Linux installer, which bundles its own Python and dependencies. +# The checksums are the ones AWS publishes in the release notes. Snowflake CLI gets a venv of its +# own. Installing both with pip into one venv made the resolver backtrack for minutes (they pin +# conflicting `rich` versions) and settle on old releases. Snowflake's single-file binary is no +# alternative: on first run it unpacks ~400 MB of Python into $HOME. +# python3 and python3-pip also serve `sam build` for Python functions (Debian's Python 3.11; +# other Python runtimes need `sam build --use-container`). +ARG TARGETARCH +ARG SAM_CLI_VERSION=1.172.1 +ARG SAM_CLI_SHA256_AMD64=aa6a5a785c576f5d8e1b783fe1633cb6eea1cbab382fa5aca1b9e17a9250cc07 +ARG SAM_CLI_SHA256_ARM64=fcfc7cd414a75cc17a00c81110b2955ab52b6fae1dd8b7415d637ca95ca931df +RUN set -eux; \ + case "$TARGETARCH" in \ + amd64) sam_arch=x86_64; sam_sha="$SAM_CLI_SHA256_AMD64" ;; \ + arm64) sam_arch=arm64; sam_sha="$SAM_CLI_SHA256_ARM64" ;; \ + *) echo "unsupported TARGETARCH '$TARGETARCH'" >&2; exit 1 ;; \ + esac; \ + cd /tmp; \ + curl -fsSLo sam.zip "https://github.com/aws/aws-sam-cli/releases/download/v${SAM_CLI_VERSION}/aws-sam-cli-linux-${sam_arch}.zip"; \ + echo "${sam_sha} sam.zip" | sha256sum -c -; \ + unzip -q sam.zip -d sam-installer; \ + ./sam-installer/install; \ + rm -rf sam.zip sam-installer; \ + sam --version + +ARG SNOWFLAKE_CLI_VERSION=3.34.0 +RUN python3 -m venv /opt/snowflake-cli \ + && /opt/snowflake-cli/bin/pip install --no-cache-dir --no-compile "snowflake-cli==${SNOWFLAKE_CLI_VERSION}" \ + && find /opt/snowflake-cli/lib \ \( -type d \( -name __pycache__ -o -name tests -o -name test \) -o -type f \( -name '*.pyc' -o -name '*.pyo' \) \) \ - -prune -exec rm -rf '{}' + + -prune -exec rm -rf '{}' + \ + && ln -s /opt/snowflake-cli/bin/snow /usr/local/bin/snow \ + && snow --version # lstk wraps terraform, cdk, and sam for the deployer tool. aws-cdk >= 2.1138 honors the # CDK_S3_FORCE_PATH_STYLE that lstk sets, so S3 assets reach host.docker.internal path-style. RUN npm install -g aws-cdk@2.1145.0 @localstack/lstk@1.4.2 \ - && npm cache clean --force + && npm cache clean --force \ + && lstk --version \ + && cdk --version WORKDIR /app RUN mkdir -p /tmp/dockerode-deps \ @@ -52,12 +81,9 @@ RUN mkdir -p /tmp/dockerode-deps \ COPY --from=builder /app/dist ./dist COPY --from=builder /app/package.json ./package.json +# Each tool layer checks its own version above, so this step only checks the per-commit files +# and stays cheap when the tool layers come from cache. RUN set -eux; \ - lstk --version; \ - terraform version; \ - sam --version; \ - cdk --version; \ - snow --version; \ node dist/cli.js version; \ node -e "require('dockerode'); console.log('dockerode ok')"