Skip to content

fix: exclude .env files from the build context - #315

Merged
uniqueg merged 4 commits into
devfrom
fix/dockerignore-env
Nov 1, 2026
Merged

uniqueg merged 4 commits into
devfrom
fix/dockerignore-env

Conversation

@uniqueg

@uniqueg uniqueg commented Nov 1, 2026 •

Copy link
Copy Markdown
Member

Summary

Closes #312 (finding L3 of the v1.0.0rc1 whole-repo pre-release review).

  • .dockerignore now excludes **/.env*, after the allowlist, so local secret files (.env, .env.local, .envrc, .env-prod, directories named .env…) stay out of the build context, also inside the re-included cloud_registry/ (which the build stage copies into a layer). The final image was not affected (it only installs the wheel).
  • Unlike the issue's sketch, .env.example is not re-included: the build copies no example, and an exception with wildcards (!**/.env.example) makes BuildKit walk every excluded directory (.git, .venv, …). The org rule allows, but doesn't require, keeping examples.
  • .gitignore also ignores .env* (it only had .env), except .env.example.
  • New tests in tests/test_image_build.py check the build context with an emulation of Docker's matching (moby patternmatcher: last match wins, **/ matches zero or more directories, parent-directory matches, character classes): needed files are kept, caches and .env* files are excluded.

No docs change: no doc describes the build context.

Verification

  • TDD: test_context_without_env_files failed before the .dockerignore change.
  • pytest tests/test_image_build.py: 8 passed; the full suite and coverage ran on the other branches of this series (this change adds tests only).
  • pre-commit run --all-files: passed.
  • Not run: a real Docker build of a context with .env files (the Docker socket isn't available in the agent sandbox); the reviewers checked the patterns against moby's matching rules.
  • CI: all checks passed (gate CI passed).

Review

Full lane (security). Round 1: lows only (pattern narrower than .env*, the costly example exception, character classes in the emulation, boundary paths, comment wording); all fixed. Round 2 (adversarial): lows only (copy classes unchanged, .gitignore .env*); fixed.

Declined review findings

Open questions

None.

🤖 Generated with Claude Code

Summary by Sourcery

Prevent local environment files from entering Docker build contexts and add regression coverage for the ignore rules.

Bug Fixes:

  • Exclude local .env* secret files from Docker build contexts, including files under re-included directories.

Enhancements:

  • Extend repository ignore rules to cover .env* files while retaining .env.example in Git.

Tests:

  • Add build-context coverage verifying required files remain included while caches and .env* files are excluded using Docker-compatible matching behavior.

uniqueg and others added 3 commits November 1, 2026 12:55
`.dockerignore` re-includes all of `cloud_registry/`, so a local
`cloud_registry/.env` would reach the build context and the layer and
cache of the build stage (finding L3 of the rc1 pre-release review).
It now excludes `.env` and `.env.*` files in all paths, except
`.env.example`. Tests check the build context with Docker's matching
rules (the last matching pattern decides).

Closes #312

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review findings: `**/.env` and `**/.env.*` missed files such as
`.envrc` or `.env-prod`, so the pattern is now `**/.env*`, as in the
org rule. The `.env.example` exception is dropped: the build copies no
example, and an exception with wildcards makes Docker walk all
excluded directories. The test emulation now supports character
classes (`**/*.py[cod]`), and `.gitignore` also ignores `.env.*`.

Refs #312

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The emulation of Docker's matching copies character classes unchanged,
as moby does (ranges, negations), and `.gitignore` ignores all `.env*`
files (e.g., `.envrc`), as `.dockerignore` does. Review findings of
round 2.

Refs #312

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@uniqueg uniqueg added this to the v1.0.0-rc1 milestone Nov 1, 2026
@sourcery-ai

sourcery-ai Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

The PR prevents .env* files and directories—including those under re-included source paths—from being sent to Docker builds, aligns Git ignore behavior, and adds tests that emulate Docker’s matching semantics to validate both secret exclusion and required build-context files.

File-Level Changes

Change Details Files
Harden Docker build-context filtering against local environment and secret files while preserving required source files.
  • Add a last-match-wins .dockerignore rule excluding .env* paths recursively, including paths beneath re-included directories such as cloud_registry/.
  • Align Git exclusions with the Docker policy by ignoring .env* while retaining .env.example.
  • Keep the existing build-context allowlist behavior and verify required files remain available while caches, documentation, and unrelated artifacts stay excluded.
.dockerignore
.gitignore
Add an executable emulation of Docker ignore matching and regression coverage for the resulting context.
  • Implement matching for recursive **, segment-scoped wildcards, parent-directory exclusions, negation ordering, and character classes.
  • Test required build inputs, excluded artifacts, nested environment files and directories, and filenames that merely contain .env.
  • Document in the test module that local secrets must not enter the image build context.
tests/test_image_build.py

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 1, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (ae9154f) to head (b17a595).

Additional details and impacted files
@@            Coverage Diff            @@
##               dev      #315   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           22        22           
  Lines         2366      2366           
=========================================
  Hits          2366      2366           

☔ 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 1, 2026 12:23

@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 1 issue

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

## Individual Comments

### Comment 1
<location path="tests/test_image_build.py" line_range="313" />
<code_context>
+    for path in [
+        ".env",
+        ".env.local",
+        ".env.example",
+        "cloud_registry/.env",
+        "cloud_registry/.env.production",
</code_context>
<issue_to_address>
**Keep env example available**

The issue asks to re-include `.env.example`, but the test lists it among paths that must be excluded, matching the current behavior. Add the requested exception and test that the example remains in the build context.
</issue_to_address>

Linked issues

#312 Exclude .env files from the image build context · 1 of 2 met

# Requirement Status Notes
1 Exclude .env and .env.* files from the build context, while re-including .env.example. 🟡 Partly met The broader **/.env* pattern excludes the requested secret files, but .env.example is excluded rather than re-included.
2 Test the build-context exclusion in tests/test_image_build.py. ✅ Met The added tests check that .env-named paths are excluded from the context.

Sourcery assessment

Approval pending. 1 finding to address first.

Blocking findings: tests/test_image_build.py:313


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

Comment thread tests/test_image_build.py Resolved
@uniqueg
uniqueg merged commit 7f1a082 into dev Nov 1, 2026
35 of 44 checks passed
@uniqueg
uniqueg deleted the fix/dockerignore-env branch November 1, 2026 12:51
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.

Exclude .env files from the image build context

1 participant