Skip to content

build: pin image deps and base by digest - #685

Merged
uniqueg merged 4 commits into
devfrom
build/629-pin-image-deps
Nov 4, 2026
Merged

uniqueg merged 4 commits into
devfrom
build/629-pin-image-deps

Conversation

@uniqueg

@uniqueg uniqueg commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

Summary

Closes #629.

  • docker/Dockerfile: one FROM python:<version>-slim-trixie@sha256:... AS python-<version>-slim-trixie stage per Python version, selected with PY_IMAGE. The builder installs pip and the build tools, then FOCA's dependencies, with --require-hashes from docker/requirements/, and FOCA itself without dependencies or build isolation.
  • scripts/compile-image-requirements.sh [--check | --upgrade]: compiles docker/requirements/py<version>.txt and build.txt from pyproject.toml with uv.
  • CI job Image constraints (in the gate): fails if the pins don't fit pyproject.toml. Hashes aren't compared, as pinned versions can gain wheels at any time.
  • Dependabot: pip for /docker/requirements, docker digests of the Python images, grouped, with Python versions left alone.
  • Release assets foca-image-requirements-py<version>.txt: no new workflow permissions, as Publish release already has contents: write.
  • Docs: contributing, deployment, migration guide. SBOM: sbom: true stays on the image builds; verified only, as the issue says.

.dockerignore: allowlist, with only docker/requirements/ added; no secrets in the context.

Verification

  • ruff, mypy, strict docs build, full pytest with MongoDB, 100% coverage. tests/test_image_requirements.py covers stages, digests, workflow matrices, hashes and the --check script with a stand-in for uv.
  • scripts/compile-image-requirements.sh --check passes on the files, and failed by hand after making a pyproject.toml requirement impossible.
  • Reviewer: exact pip sequence in a Python 3.13 venv; compiled files for arm64 equal those for x86_64.
  • The image build itself isn't run locally: CI's image jobs (Integration, Scan image) do that.

Not verified until they run: Dependabot on several FROM python:<tag>@sha256 lines and the ignore rule for tags like 3.13-slim-trixie, and on hashed files without a header. The release step runs only on a tag: per the release rules, this needs a dry run before the next tag, as the release workflow changed. Please watch the first runs.

Declined review findings

  • Dependencies without wheels are built with unpinned build tools (PyYAML 6.0.3 on Python 3.15): only --only-binary would avoid that, and it would stop the build for that version. Documented.
  • Stages, script, matrices and files must match: a test guards this.

Open questions

None.

🤖 Generated with Claude Code

Summary by Sourcery

Make FOCA image builds reproducible by pinning base images and dependencies, and automate validation and maintenance of those constraints.

New Features:

  • Add reproducible FOCA image builds using digest-pinned Python base images and hashed, version-specific dependencies.
  • Publish the image requirements as release assets for reuse outside the image build.

Bug Fixes:

  • Ensure image dependency pins remain compatible with the project requirements through continuous integration checks.

Enhancements:

  • Add tooling and tests to maintain synchronized Python image stages, dependency files, and workflow matrices.
  • Configure Dependabot to update image digests and image dependency pins while preserving supported Python versions.

CI:

  • Add an Image constraints CI gate that validates compiled image requirements against pyproject.toml.

Documentation:

  • Document image dependency updates, digest pinning, and reuse of release-published image requirements.
  • Document the updated Python image build constraints in the migration guide.

Tests:

  • Add coverage for image stages, digest pinning, dependency hashes, workflow matrices, and requirement consistency checks.

Chores:

  • Restrict the Docker build context while allowing the hashed image requirements files.

uniqueg and others added 2 commits November 4, 2026 08:29
The builder stage installs dependencies and build tools with hashes, from
files compiled from `pyproject.toml`, and base images are given by digest.
CI fails if the pins don't fit, Dependabot updates them, and releases have
the files as assets.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`packaging` is a build tool too, so it counted as installed and didn't
reach `/install`. Test the check of the script, and name the job after
what it checks.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@uniqueg uniqueg added this to the v0.21.0 milestone Nov 4, 2026
@sourcery-ai

sourcery-ai Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

This PR makes FOCA image builds reproducible by selecting digest-pinned Python base stages and installing version-specific, hash-verified dependencies generated from pyproject.toml. A uv compilation/check workflow, CI gate, tests, Dependabot configuration, release assets, and documentation keep the Dockerfile, requirement files, supported Python versions, and build matrices synchronized.

Sequence diagram for compiling and validating image requirements

sequenceDiagram
    participant Developer
    participant Script as compile-image-requirements.sh
    participant UV as uv
    participant Project as pyproject.toml
    participant Files as docker/requirements
    participant CI

    Developer->>Script: --upgrade
    Script->>Project: Read dependencies and build-system requirements
    loop Python versions 3.11 through 3.15
        Script->>UV: pip compile --generate-hashes
        UV-->>Files: Write py<version>.txt
    end
    Script->>UV: Compile build.txt with pip and build requirements
    UV-->>Files: Write build.txt
    CI->>Script: --check
    Script->>UV: Recompile requirements without changing hashes
    UV-->>Script: Candidate pins
    Script-->>CI: Pass if pins fit pyproject.toml
Loading

Flow diagram for digest-pinned FOCA image construction

flowchart TD
    Select[PY_IMAGE selects one pinned Python stage] --> Builder[Builder stage]
    Builder --> Tools[Install hashed build requirements]
    Tools --> Deps[Install hashed Python-version requirements]
    Deps --> Source[Install FOCA with --no-deps and --no-build-isolation]
    Source --> Final[Copy installed files into final image]
Loading

File-Level Changes

Change Details Files
Reworked the FOCA image build to use digest-pinned, version-specific Python stages and reproducibly install hashed dependencies.
  • Added one digest-pinned Python stage for each supported version and selected the stage through PY_IMAGE.
  • Added compiled, hash-checked runtime and build requirement files; installed build tools, dependencies, and FOCA separately without dependency resolution or build isolation.
  • Included the requirement files in the Docker build context while retaining the context allowlist.
docker/Dockerfile
.dockerignore
docker/requirements/build.txt
docker/requirements/py3.11.txt
docker/requirements/py3.12.txt
docker/requirements/py3.13.txt
docker/requirements/py3.14.txt
docker/requirements/py3.15.txt
Added tooling and CI validation to keep image requirements, supported Python versions, and image build matrices consistent.
  • Added a uv-based script to compile or check per-version hashed requirements and build requirements, preserving compatible pins unless upgrading.
  • Added the Image constraints gate job and made the merge gate depend on it.
  • Added tests covering digest pinning, requirement hashes, Docker context inclusion, stage and workflow alignment, shared build tools, and requirement consistency checks.
scripts/compile-image-requirements.sh
.github/workflows/ci.yml
tests/test_image_requirements.py
Automated maintenance and release distribution of image dependency metadata.
  • Configured Dependabot to update image requirement pins and grouped Python image digest updates while ignoring Python version changes.
  • Added per-Python image requirement files to release assets.
.github/dependabot.yml
.github/workflows/release.yml
Documented image dependency maintenance, reproducible deployment, and migration constraints.
  • Documented how to compile, upgrade, and validate image requirements and digest-pinned base images.
  • Documented use of published hashed requirements and the restricted PY_IMAGE values for image builds.
  • Noted that dependencies without wheels may use unpinned build tools.
docs/contributing/index.md
docs/deployment.md
docs/migration-0.21.md

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@codecov

codecov Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (874a155) to head (4846c49).

Additional details and impacted files
@@            Coverage Diff            @@
##               dev      #685   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           61        61           
  Lines         8071      8071           
=========================================
  Hits          8071      8071           
Flag Coverage Δ
test_unit 100.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@uniqueg
uniqueg marked this pull request as ready for review November 4, 2026 07:57

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 3 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="docker/Dockerfile" line_range="49" />
<code_context>
+# image), then FOCA alone; files copied here must be in the allowlist of
+# `.dockerignore`; dependencies that are also build tools (`packaging`) must be
+# installed again, for the final image
+COPY docker/requirements/ ./requirements/
+ARG PY_IMAGE
+RUN pip install --no-cache-dir --require-hashes -r requirements/build.txt
</code_context>
<issue_to_address>
**Image builds cannot copy requirements**

Docker excludes the parent `docker/` directory, so the negated requirements-directory pattern does not re-include its files and this `COPY` fails with a missing source; every image build stops.

Re-include `docker/` before `docker/requirements/` in `.dockerignore`.
</issue_to_address>

### Comment 2
<location path="scripts/compile-image-requirements.sh" line_range="52" />
<code_context>
+        }
+    fi
+    # shellcheck disable=SC2086 # `upgrade` is empty or one option
+    uv pip compile --quiet --generate-hashes --no-header ${upgrade} \
+        --output-file "${output}" "$@"
+    if [ "${mode}" = "--check" ]; then
</code_context>
<issue_to_address>
**Failed upgrades leave mixed pins**

When `uv` fails on a later requirements file after earlier files have compiled, each successful loop iteration writes directly to its requirements file; a later `uv pip compile` failure leaves earlier versions upgraded and later versions unchanged, so the failed run leaves a partially updated set in the working tree.

Compile every output into a staging directory and replace the target files only after all compilations succeed.
</issue_to_address>

### Comment 3
<location path="tests/test_image_requirements.py" line_range="85" />
<code_context>
+        for path in REQUIREMENTS.glob("py*.txt")
+        for pin in re.findall(r"^([a-z0-9._-]+)==", path.read_text(), re.M)
+    } & set(pins)
+    assert not shared or "--ignore-installed" in text
+
+
</code_context>
<issue_to_address>
**Shared package misses final image**

When `--ignore-installed` remains somewhere in the Dockerfile but is removed from the runtime requirements install command, pip skips the globally installed shared package when installing runtime requirements to `/install`, so the final image lacks it.

Assert the flag is on the specific runtime requirements install command, or verify that shared packages reach `/install`.
</issue_to_address>

Linked issues

#629 build: pin image deps and base by digest · 4 of 6 met · 2 to check by hand

# Requirement Status Notes
1 Use hashed constraints for each supported Python version in the image build, and fail CI when pins no longer fit pyproject.toml. ✅ Met The builder installs hashed requirements with --require-hashes, and the Image constraints job runs the check script in the merge gate.
2 Pin base images by digest. ✅ Met Each supported Python base image in docker/Dockerfile is referenced by a SHA-256 digest.
3 Have Dependabot keep dependency pins and base image digests current without changing the deliberately chosen Python versions. 👀 Check by hand (external system) Check Dependabot's first runs to confirm it updates the digests across the Dockerfile stages and the hashed requirement files while leaving the Python tags unchanged.
4 Publish the constraints as release assets, leaving them in the repository if this needs new workflow permissions. ✅ Met The publish job copies each Python-version requirements file into dist with the requested asset name and already has contents: write permission.
5 Verify that SBOMs are already published; do not add new SBOM work. 👀 Check by hand (runtime) The CI and release image build steps retain sbom: true; verify that the resulting published images actually include SBOMs.
6 Document updating constraints in contributing docs and reusing them in deployment docs. ✅ Met The contributing guide documents compiling and updating image requirements, and the deployment guide describes using the release assets.

Sourcery assessment

Approval pending. 3 findings to address first.

Blocking findings: docker/Dockerfile:49, scripts/compile-image-requirements.sh:52, tests/test_image_requirements.py:85


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread docker/Dockerfile Resolved
Comment thread scripts/compile-image-requirements.sh Resolved
Comment thread tests/test_image_requirements.py Resolved
uniqueg and others added 2 commits November 4, 2026 09:03
A failure on a later file left earlier files upgraded. Also checks that
the flag --ignore-installed is on the command installing for the image.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@uniqueg
uniqueg merged commit a61c742 into dev Nov 4, 2026
58 checks passed
@uniqueg
uniqueg deleted the build/629-pin-image-deps branch November 4, 2026 09:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

build: pin image deps and base by digest

1 participant