Skip to content

fix: include .git in Docker build context for version embedding#1621

Open
joanestebanr wants to merge 9 commits into
release/0.10from
feat/fix_version_1618
Open

fix: include .git in Docker build context for version embedding#1621
joanestebanr wants to merge 9 commits into
release/0.10from
feat/fix_version_1618

Conversation

@joanestebanr
Copy link
Copy Markdown
Collaborator

🔄 Changes Summary

  • Revert fix: embed version info in Docker builds #1619 (which tried to fix version embedding via build-args — the approach worked locally but not in the GitHub Actions workflow since docker/build-push-action was not updated to pass the version build-args).
  • Remove .git/ from .dockerignore so that git describe and git rev-parse run correctly inside the builder stage, embedding the correct Version, GitRev and GitBranch values in the binary without any changes to the Makefile, Dockerfile, or CI workflow.

⚠️ Breaking Changes

  • None

📋 Config Updates

  • None

✅ Testing

  • 🖱️ Manual: make build-docker && docker run aggkit:local version shows correct Version, Git revision and Git branch fields.

🐞 Issues

📝 Notes

  • .git/ was excluded from the Docker context for security and context-size reasons. Since the builder stage only uses git metadata to set ldflags and the .git directory is not copied into the final runtime image, the security concern does not apply.

🤖 Generated with Claude Code

joanestebanr and others added 2 commits May 20, 2026 16:25
Removing .git/ from .dockerignore allows git describe/rev-parse to run
inside the builder stage, so the binary gets correct Version, GitRev
and GitBranch values without requiring build-args.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@claude
Copy link
Copy Markdown

claude Bot commented May 20, 2026

Claude finished @joanestebanr's task in 1m 57s —— View job


PR Review Complete ✅


🚀 What's New

This PR fixes version embedding in Docker builds by removing .git/ from .dockerignore, allowing git commands (git describe, git rev-parse) to run correctly inside the Docker builder stage. This reverts the previous approach (#1619) that attempted to use build-args, which worked locally but failed in GitHub Actions CI workflows.

🐛 Bug Fixes

📋 Config Updates

None.

⚠️ Breaking Changes

None.


Code Review Feedback

🟢 Approach & Solution Quality

Positive aspects:

  1. Simplicity: This approach is much simpler than the previous build-args solution - it requires no changes to CI workflows or Makefile build-arg passing
  2. Consistency: Version detection now works the same way in Docker builds as it does in local builds via version.mk
  3. Multi-stage isolation: The .git directory is only present in the builder stage and is NOT copied to the final runtime image (confirmed in Dockerfile:56-58)
  4. Clean revert: Properly reverts the complex build-args approach that didn't work in CI

The core fix:

  • Removed .git/ from .dockerignore (.dockerignore:2)
  • version.mk uses shell commands that now work inside Docker builder:
    • git describe --tags --always (version.mk:1)
    • git rev-parse --short HEAD (version.mk:2)
    • git rev-parse --abbrev-ref HEAD (version.mk:3)

🟡 Security Considerations

The PR description claims "security concern does not apply" since .git is only in the builder stage. This is mostly correct but requires nuance:

✅ What's protected:

  • The .git directory does NOT appear in the final runtime image (Alpine-based, stage 2)
  • Multi-stage builds properly isolate the build environment from runtime
  • Only compiled binaries are copied to the final image (Dockerfile:57-58)

🟡 Considerations to be aware of:

  1. Build context size: Including .git/ increases the Docker build context size sent to the Docker daemon. For repos with large git histories, this could slow down builds. However, this is a build-time cost, not a security issue.
  2. CI/CD secrets: If the git repository contains sensitive information in its history (even in deleted commits), it would be present in the builder stage. This is generally not a concern for public repos, but worth noting for private repositories.
  3. Layer caching: The .git directory changes frequently, which could invalidate Docker layer caching. However, the Dockerfile structure (COPY . . at line 16 happens after dependency downloads) already handles this reasonably well.

Verdict: The security claim is valid - no .git data leaks into the runtime image. ✅

🟢 Correctness

Changes verified:

  1. .dockerignore (line 2): .git/ removed - correct ✅
  2. Dockerfile: No build-args for VERSION/GITREV/GITBRANCH (reverted) - correct ✅
  3. Makefile (line 90): build-docker target simplified, no longer passes build-args - correct ✅
  4. version.mk: Changed from ?= to := operators - this is semantically important:
    • ?= (conditional assignment): Only sets if variable is not already defined
    • := (simple assignment): Always evaluates and sets the variable
    • This change ensures the git commands always run, even if VERSION/GITREV/GITBRANCH are set in the environment ✅

🟢 CI/CD Compatibility

Workflow analysis:

  • build-aggkit-image.yml:31 uses make build-docker which no longer requires build-args ✅
  • build-push-docker-image.yml:84-92 uses docker/build-push-action with only INCLUDE_SHELL build-arg ✅
  • No workflow changes needed - this was the main advantage over fix: embed version info in Docker builds #1619

🟢 Testing & Verification

The PR description mentions manual testing:

make build-docker && docker run aggkit:local version

This should show correct Version, Git revision, and Git branch fields. ✅

Suggested additional verification (for reviewers):

# Verify .git is not in final image
docker build -t aggkit:test .
docker run aggkit:test ls -la / | grep -i git
# Should NOT show .git directory

# Verify version info is embedded
docker run aggkit:test version
# Should show correct git metadata

🟡 Alternative Approaches Considered

The PR mentions that #1619's approach "worked locally but not in the GitHub Actions workflow since docker/build-push-action was not updated to pass the version build-args."

Why the build-args approach failed in CI:
Looking at build-push-docker-image.yml:84-92, the docker/build-push-action would have needed:

build-args: |
  VERSION=${{ env.VERSION }}
  GITREV=${{ env.GITREV }}
  GITBRANCH=${{ env.GITBRANCH }}
  INCLUDE_SHELL=${{ matrix.variant.include_shell }}

But these variables weren't computed or passed, so the builder stage received empty values.

Why the current approach is better:

  • Self-contained: Git metadata is extracted from the source itself
  • No workflow changes needed
  • Works identically in local dev and CI environments

🟢 Code Quality

  1. Makefile simplification (lines 88-90): Clean removal of build-arg passing ✅
  2. version.mk clarity (lines 1-3): Using := instead of ?= makes the intent clearer ✅
  3. Dockerfile: No changes needed - already well-structured with multi-stage build ✅

🔴 Potential Issues Found

None identified. The implementation is sound and achieves the stated goal.


Summary

This PR successfully fixes the version embedding issue through a simple and elegant solution. The approach of including .git in the Docker build context is:

  • Secure: .git is isolated to the builder stage only
  • Simple: No CI workflow changes needed
  • Consistent: Works the same locally and in CI
  • Correct: Properly embeds version metadata in binaries

The revert of #1619 was the right call, and this solution is superior to the build-args approach.

Recommendation: Approve and merge


Additional Notes

  • The go.mod/go.sum changes appear to be dependency updates unrelated to the version embedding fix (likely from the revert commit)
  • Consider adding a CI test that verifies the version information is correctly embedded in the Docker-built binary
  • Documentation of this build approach in the README or developer docs might help future contributors understand why .git is not in .dockerignore

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 549aea0c97

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread go.mod Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR restores Docker image version embedding by making git metadata available inside the Docker build, reverting the prior build-arg-based approach (PR #1619).

Changes:

  • Removes .git/ from .dockerignore so git describe / git rev-parse work during the builder stage.
  • Reverts Docker build targets and Dockerfile away from passing version build args / declaring version ARGs.
  • Updates go.mod / go.sum with several indirect golang.org/x/* version changes (appears unrelated to the stated goal).

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
version.mk Reverts VERSION/GITREV/GITBRANCH to be computed via git at build time.
Makefile Removes VERSION/GITREV/GITBRANCH build-args and drops the post-build version verification.
Dockerfile Removes VERSION/GITREV/GITBRANCH ARG declarations from the builder stage.
.dockerignore Stops excluding .git/ so git metadata is available inside Docker builds.
go.mod Downgrades several indirect golang.org/x/* dependencies.
go.sum Updates sums consistent with the indirect dependency downgrades.
Comments suppressed due to low confidence (1)

go.mod:220

  • Additional golang.org/x/* indirect dependency downgrades are included here (x/sys, x/telemetry, x/text, x/tools). If the intent is only to change Docker build context/version embedding, these dependency changes should likely be dropped or called out explicitly with rationale.
	golang.org/x/oauth2 v0.34.0 // indirect
	golang.org/x/sys v0.41.0 // indirect
	golang.org/x/telemetry v0.0.0-20260109210033-bd525da824e2 // indirect
	golang.org/x/text v0.34.0 // indirect
	golang.org/x/time v0.12.0 // indirect
	golang.org/x/tools v0.41.0 // indirect

Comment thread .dockerignore
Comment thread Makefile
Comment thread go.mod Outdated
Upgrades golang.org/x/crypto, net, sys, text, tools, mod and telemetry
to their latest versions to address known security vulnerabilities.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@joanestebanr joanestebanr self-assigned this May 21, 2026
@joanestebanr joanestebanr added the bug Something isn't working label May 21, 2026
joanestebanr and others added 6 commits May 21, 2026 10:39
After each platform/variant build, pulls the image by digest and runs
`aggkit version`, failing the job if the Version field is empty.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Restore .git/ to .dockerignore but re-include only the files required
by git describe and git rev-parse (HEAD, packed-refs, refs/), so the
auth token that actions/checkout writes to .git/config is never sent
to the Docker daemon or stored in build cache.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Mirrors the same check added to release.yml: after each platform/variant
build, pulls the image by digest and runs `aggkit version`, failing the
job if the Version field is empty.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Mirrors the same check added to release.yml and build-push-docker-image.yml:
after the make build-docker step, runs `aggkit version` and fails if the
Version field is empty.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Rather than trying to re-include individual git metadata files via
negation patterns, keep .git/ accessible for git describe/rev-parse
and only exclude .git/config where actions/checkout stores its auth
token.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@sonarqubecloud
Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants