Skip to content

Commit

Permalink
Merge pull request #2905 from crazy-max/bake-infer-auth-token
Browse files Browse the repository at this point in the history
bake: infer git auth token from remote files to build request
  • Loading branch information
tonistiigi authored Jan 14, 2025
2 parents 026ac23 + 45fc5ed commit 630066b
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion bake/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -1179,6 +1179,16 @@ func updateContext(t *build.Inputs, inp *Input) {
t.ContextState = &st
}

func isRemoteContext(t build.Inputs, inp *Input) bool {
if build.IsRemoteURL(t.ContextPath) {
return true
}
if inp != nil && build.IsRemoteURL(inp.URL) && !strings.HasPrefix(t.ContextPath, "cwd://") {
return true
}
return false
}

func collectLocalPaths(t build.Inputs) []string {
var out []string
if t.ContextState == nil {
Expand Down Expand Up @@ -1338,7 +1348,23 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
}
bo.Platforms = platforms

bo.SecretSpecs = t.Secrets.ToPB()
secrets := t.Secrets
if isRemoteContext(bi, inp) {
if _, ok := os.LookupEnv("BUILDX_BAKE_GIT_AUTH_TOKEN"); ok {
secrets = append(secrets, &buildflags.Secret{
ID: llb.GitAuthTokenKey,
Env: "BUILDX_BAKE_GIT_AUTH_TOKEN",
})
}
if _, ok := os.LookupEnv("BUILDX_BAKE_GIT_AUTH_HEADER"); ok {
secrets = append(secrets, &buildflags.Secret{
ID: llb.GitAuthHeaderKey,
Env: "BUILDX_BAKE_GIT_AUTH_HEADER",
})
}
}
secrets = secrets.Normalize()
bo.SecretSpecs = secrets.ToPB()
secretAttachment, err := controllerapi.CreateSecrets(bo.SecretSpecs)
if err != nil {
return nil, err
Expand Down

0 comments on commit 630066b

Please sign in to comment.