From 5d4223e4f81a885984ada66ed96b3655aa1427f1 Mon Sep 17 00:00:00 2001 From: Justin Chadwell Date: Tue, 14 Mar 2023 09:01:04 +0000 Subject: [PATCH 1/2] build: move SOURCE_DATE_EPOCH parsing into option generation This allows the build package code to become more generic, and also ensures that when the environment variables are not propogated (in the case of the remote controller), that we can still correctly set SOURCE_DATE_EPOCH. Signed-off-by: Justin Chadwell --- build/build.go | 7 ------- commands/build.go | 7 +++++++ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/build/build.go b/build/build.go index 3728711d..8e8f7d63 100644 --- a/build/build.go +++ b/build/build.go @@ -604,13 +604,6 @@ func toSolveOpt(ctx context.Context, node builder.Node, multiDriver bool, opt Op } } - // Propagate SOURCE_DATE_EPOCH from the client env - if v := os.Getenv("SOURCE_DATE_EPOCH"); v != "" { - if _, ok := so.FrontendAttrs["build-arg:SOURCE_DATE_EPOCH"]; !ok { - so.FrontendAttrs["build-arg:SOURCE_DATE_EPOCH"] = v - } - } - // set platforms if len(opt.Platforms) != 0 { pp := make([]string, len(opt.Platforms)) diff --git a/commands/build.go b/commands/build.go index 3d33ab65..fef0b48c 100644 --- a/commands/build.go +++ b/commands/build.go @@ -100,6 +100,13 @@ func (o *buildOptions) toControllerOptions() (controllerapi.BuildOptions, error) Opts: &o.CommonOptions, } + // TODO: extract env var parsing to a method easily usable by library consumers + if v := os.Getenv("SOURCE_DATE_EPOCH"); v != "" { + if _, ok := opts.BuildArgs["SOURCE_DATE_EPOCH"]; !ok { + opts.BuildArgs["SOURCE_DATE_EPOCH"] = v + } + } + inAttests := append([]string{}, o.attests...) if o.provenance != "" { inAttests = append(inAttests, buildflags.CanonicalizeAttest("provenance", o.provenance)) From 780531425b0a19a25d8c7ebad582e225082fd135 Mon Sep 17 00:00:00 2001 From: Justin Chadwell Date: Tue, 14 Mar 2023 09:07:23 +0000 Subject: [PATCH 2/2] bake: move SOURCE_DATE_EPOCH parsing to overrides Previously, when directly modifying the args map when reading targets, we could end up in a scenario where bake tests that compare arg maps would fail if SOURCE_DATE_EPOCH was set in the environment. This patch prevents this failure by setting the SOURCE_DATE_EPOCH at the command level (which isn't injected into tests as well), ensuring that we test correctly even when SOURCE_DATE_EPOCH is set in the environment. Signed-off-by: Justin Chadwell --- bake/bake.go | 13 ------------- commands/bake.go | 13 +++++++++++++ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/bake/bake.go b/bake/bake.go index 1abf5f1b..fc8b9337 100644 --- a/bake/bake.go +++ b/bake/bake.go @@ -140,19 +140,6 @@ func ReadTargets(ctx context.Context, files []File, targets, overrides []string, } } - // Propagate SOURCE_DATE_EPOCH from the client env. - // The logic is purposely duplicated from `build/build`.go for keeping this visible in `bake --print`. - if v := os.Getenv("SOURCE_DATE_EPOCH"); v != "" { - for _, f := range m { - if f.Args == nil { - f.Args = make(map[string]*string) - } - if _, ok := f.Args["SOURCE_DATE_EPOCH"]; !ok { - f.Args["SOURCE_DATE_EPOCH"] = &v - } - } - } - return m, n, nil } diff --git a/commands/bake.go b/commands/bake.go index c7f1461e..9c49327e 100644 --- a/commands/bake.go +++ b/commands/bake.go @@ -149,6 +149,19 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions, cFlags com return err } + if v := os.Getenv("SOURCE_DATE_EPOCH"); v != "" { + // TODO: extract env var parsing to a method easily usable by library consumers + for _, t := range tgts { + if _, ok := t.Args["SOURCE_DATE_EPOCH"]; ok { + continue + } + if t.Args == nil { + t.Args = map[string]*string{} + } + t.Args["SOURCE_DATE_EPOCH"] = &v + } + } + // this function can update target context string from the input so call before printOnly check bo, err := bake.TargetsToBuildOpt(tgts, inp) if err != nil {