From b2d6aa9ca62021cca87632e704972ce831327b3d Mon Sep 17 00:00:00 2001 From: Adam Wolfe Gordon Date: Mon, 23 Nov 2026 14:35:26 -0600 Subject: [PATCH] projects: Build a build image for Python functions Previously, our build image for Python functions was a stock Debian container, which meant we had to install the Python toolchain as part of every build. This is slow and breaks in environments where users can't access the Debian package repositories. Create a simple build image by installing the necessary Python tools into the same Debian image we used before. Add a CI job to update the image when the Dockerfile changes. Since we're using a digest for the base (upstream Debian) image, Renovate will update the Dockerfile for us, triggering a new image build. A new base image build will trigger Renovate to update the version in images.yaml, so we get automatic updates with manual approval (and existing versions of the CLI won't have their base image change unexpectedly). Signed-off-by: Adam Wolfe Gordon --- .github/renovate.json5 | 21 ++++++++ .github/workflows/python-build-image.yml | 69 ++++++++++++++++++++++++ hack/python-build-image.dockerfile | 16 ++++++ internal/project/functions/python.go | 10 ++-- 4 files changed, 109 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/python-build-image.yml create mode 100644 hack/python-build-image.dockerfile diff --git a/.github/renovate.json5 b/.github/renovate.json5 index 113c367f..7e1945a6 100644 --- a/.github/renovate.json5 +++ b/.github/renovate.json5 @@ -125,6 +125,27 @@ ], enabled: true, }, + { + // New Python build image tags are only pushed when we change its + // Dockerfile, and Debian base image updates for that Dockerfile are + // what trigger a rebuild. Don't hold either back for the docker + // schedule above. + description: 'Update the Python build image and its base image promptly', + matchDatasources: [ + 'docker', + ], + matchDepNames: [ + 'ghcr.io/crossplane/cli-python-build', + 'docker.io/library/debian', + ], + matchFileNames: [ + 'pkg/images/images.yaml', + 'hack/python-build-image.dockerfile', + ], + schedule: [ + 'at any time', + ], + }, { description: 'Ignore k8s.io/client-go older versions, they switched to semantic version and old tags are still available in the repo', matchDatasources: [ diff --git a/.github/workflows/python-build-image.yml b/.github/workflows/python-build-image.yml new file mode 100644 index 00000000..7b31fd6a --- /dev/null +++ b/.github/workflows/python-build-image.yml @@ -0,0 +1,69 @@ +name: Python Build Image + +on: + push: + branches: + - main + paths: + - hack/python-build-image.dockerfile + - .github/workflows/python-build-image.yml + pull_request: + paths: + - hack/python-build-image.dockerfile + - .github/workflows/python-build-image.yml + workflow_dispatch: {} + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} + +jobs: + build-push: + permissions: + contents: read + packages: write # for pushing to ghcr.io + runs-on: ubuntu-26.04 + + env: + # Only publish from main. + PUSH: ${{ github.event_name != 'pull_request' && github.ref == 'refs/heads/main' }} + + steps: + - name: Checkout + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 + + - name: Setup QEMU + uses: docker/setup-qemu-action@99012661954931238ded8c8b007157a8430204e1 # v4.4.0 + + - name: Setup Docker Buildx + uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1 + + # We use a time-based tag so that Renovate can detect newer versions but + # we don't have to manage semvers. + - name: Set Tag + id: tag + run: echo "tag=$(date -u +%Y%m%d.%H%M%S)" >> "$GITHUB_OUTPUT" + + - name: Login to GHCR + if: env.PUSH == 'true' + uses: docker/login-action@8aa5831fb8d48d8f76594c7ddde61ae58019f550 # v4.8.0 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + # The build container runs on the host's architecture, so we need an image + # for each architecture the CLI supports running builds on. + - name: Build and Push + id: build + uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0 + with: + context: . + file: hack/python-build-image.dockerfile + platforms: linux/amd64,linux/arm64 + push: ${{ env.PUSH == 'true' }} + tags: ghcr.io/crossplane/cli-python-build:${{ steps.tag.outputs.tag }} + labels: | + org.opencontainers.image.source=https://github.com/${{ github.repository }} + org.opencontainers.image.revision=${{ github.sha }} + org.opencontainers.image.description=Build image for Crossplane Python functions diff --git a/hack/python-build-image.dockerfile b/hack/python-build-image.dockerfile new file mode 100644 index 00000000..80b7eeac --- /dev/null +++ b/hack/python-build-image.dockerfile @@ -0,0 +1,16 @@ +# This Dockerfile is used to build the build image used by Python functions. It +# is based on the upstream Debian image and adds the Python toolchain. We build +# and host this image so that project builds don't need to construct the build +# image before building functions. +# +# The image is built and pushed by the python-build-image action whenever this +# file changes on main. The base image is updated by renovate whenever Debian +# publishes a new 13-slim image. + +FROM docker.io/library/debian:13-slim@sha256:a72b4433de1cf7d6bae0c4cee74d0547f58601a643b3375f070c51600d63b6b2 + +RUN apt-get update && \ + apt-get install -y --no-install-recommends python3 python3-venv && \ + python3 -m venv /build && \ + /build/bin/pip install --quiet hatch && \ + rm -rf /var/lib/apt/lists/* diff --git a/internal/project/functions/python.go b/internal/project/functions/python.go index 8c9ca31f..7dba2336 100644 --- a/internal/project/functions/python.go +++ b/internal/project/functions/python.go @@ -50,13 +50,9 @@ const ( pythonVersion = "3.13" // pythonBuildScript is the shell pipeline that runs in the build - // container. The default debian build image installs python and hatch - // first; a prepared build image (e.g. a host CLI's own, with python3, - // python3-venv and /build/bin/hatch baked in) skips straight to the - // build. - // - // TODO(adamwg): We should build an image with python3 and python3-venv - // pre-installed so we don't have to install them for every build. + // container. The default build image has the python toolchain baked in, so + // the build starts right away. An overridden build image without them + // (e.g. plain debian) installs them first. // // The build is reproducible so that rebuilding an unchanged function // yields the identical layer: SOURCE_DATE_EPOCH (1980-01-01, the oldest