-
Notifications
You must be signed in to change notification settings - Fork 576
bake: Support preservation of .git
directory for remote context
#3338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
2bb7658
to
5333796
Compare
@tonistiigi for your review. Note that this still requires folks to know about the additional environment variable. An alternative approach that might be less surprising to users but less efficient by default, would be to always keep git directories on the remote bake context. In that case, the I'm happy to refactor if you think that's a better approach. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this supposed to come from the bake definition itself. ReadRemoteFiles
itself should never need git dir, but after it has read the bake definition and it has been parsed it could detect that the target set BUILDKIT_CONTEXT_KEEP_GIT_DIR
build arg and in that case switch the inp.State
over to the one that has git dir enabled?
I like this approach in that it would obviate the need for an additional environment variable. I also like that it comes from the bake definition, like you mentioned. The only downside I see is that we can't reuse the bake input state and thus there will be duplicate clone operations (two distinct source identifiers, one where Maybe we do both? For correctness: detect targets that set For optimization: allow users to set |
This shouldn't be a big problem. |
Ah! In that case, I will attempt to implement the build arg detection. |
When a remote Git URL is used with `buildx bake`, `.git` directories are always omitted (see `bake.ReadRemoteFiles`). Targets that rely on the default bake context should still be able to change this behavior by passing the `BUILDKIT_CONTEXT_KEEP_GIT_DIR=1` build argument. Check for this argument and skip the use of the bake context state if the value is set to true. Signed-off-by: Dan Duvall <[email protected]>
5333796
to
cc9b898
Compare
@tonistiigi I've amended the commit and updated the PR description to reflect the new approach. |
if inp == nil || inp.State == nil { | ||
return | ||
} | ||
|
||
for k, v := range t.NamedContexts { | ||
if v.Path == "." { | ||
t.NamedContexts[k] = build.NamedContext{Path: inp.URL} | ||
continue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tonistiigi I saw this was (seemingly) missing while implementing my change. Without it, the context is simply reassigned below. Let me know if my assumption isn't correct and I'll remove it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM thanks!
// If the target includes a BUILDKIT_CONTEXT_KEEP_GIT_DIR=1 build argument, | ||
// do not reuse the bake context state as it never keeps the .git directory | ||
// (see ReadRemoteFiles). | ||
if v, ok := target.Args["BUILDKIT_CONTEXT_KEEP_GIT_DIR"]; ok && v != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure I get this. Shouldn't this only run in the t.ContextPath == "."
case above?
require.DirExists(t, filepath.Join(dir, ".git")) | ||
addr := gittestutil.GitServeHTTP(git, t) | ||
|
||
out, err := bakeCmd(sb, withDir(dir), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Iiuc this is building from a local directory (that contains .git), not the git address as first parameter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems to be the git address as first arg (see next line). withDir
is used to set working dir when running command.
(updated implementation)
When a remote Git URL is used with
buildx bake
,.git
directories arealways omitted (see
bake.ReadRemoteFiles
).Targets that rely on the default bake context should still be able to
change this behavior by passing the
BUILDKIT_CONTEXT_KEEP_GIT_DIR=1
build argument. Check for this argument and skip the use of the bake
context state if the value is set to true.
(original implementation)
When a remote URL is used with
buildx bake
, the resulting remote context is reused as the default main context of bake targets. Git remote contexts are handled usingdockerui.DetectGitContext
which accepts akeepGitDir
argument and uses that for thellb.KeepGitDir
Git source option.Prior to this change, the value for
keepGitDir
was hardcoded asfalse
, resulting in a main context for targets that never retains the.git
directory from the cloned repo even whereBUILDKIT_CONTEXT_KEEP_GIT_DIR
is defined as a build argument in a bake target definition.Support preservation of the
.git
directory in main target contexts when usingbuildx bake
with remote Git URLs via a new environment variableBUILDX_BAKE_KEEP_GIT_DIR
. This mirrors the behavior ofBUILDKIT_CONTEXT_KEEP_GIT_DIR
for build targets and is keeping with the convention of other environment variables that effect remote bake contexts such asBUILD_BAKE_GIT_SSH
.Note that for a target to retain the
.git
directory, both theBUILDX_BAKE_KEEP_GIT_DIR
environment variable andBUILDKIT_CONTEXT_KEEP_GIT_DIR
build argument must be set to a true value, the latter being specific to each target that inherits the remote Git context.