Skip to content

Commit

Permalink
buildctl: propagate SOURCE_DATE_EPOCH from client env to build arg
Browse files Browse the repository at this point in the history
Fix issue 4227

Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
  • Loading branch information
AkihiroSuda committed Sep 15, 2023
1 parent 915d245 commit b40359f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
10 changes: 9 additions & 1 deletion cmd/buildctl/build/opt.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package build

func ParseOpt(opts []string) (map[string]string, error) {
return attrMap(opts)
m := loadOptEnv()
m2, err := attrMap(opts)
if err != nil {
return nil, err
}
for k, v := range m2 {
m[k] = v
}
return m, nil
}
15 changes: 15 additions & 0 deletions cmd/buildctl/build/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/pkg/errors"

"github.com/moby/buildkit/client"
"github.com/moby/buildkit/util/bklog"
)

// loadGithubEnv verify that url and token attributes exists in the
Expand All @@ -31,3 +32,17 @@ func loadGithubEnv(cache client.CacheOptionsEntry) (client.CacheOptionsEntry, er
}
return cache, nil
}

// loadOptEnv loads opt values from the environment.
// The returned map is always non-nil.
func loadOptEnv() map[string]string {
m := make(map[string]string)
propagatableEnvs := []string{"SOURCE_DATE_EPOCH"}
for _, env := range propagatableEnvs {
if v, ok := os.LookupEnv(env); ok {
bklog.L.Debugf("Propagating %s from the client env to the build arg", env)
m["build-arg:"+env] = v
}
}
return m
}
6 changes: 4 additions & 2 deletions docs/build-repro.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ Minimal support is also available on older BuildKit when using Dockerfile 1.5 fr
buildctl build --frontend dockerfile.v0 --opt build-arg:SOURCE_DATE_EPOCH=$(git log -1 --pretty=%ct) ...
```

The `buildctl` CLI does not automatically propagate the `$SOURCE_DATE_EPOCH` environment value from the client host to the `SOURCE_DATE_EPOCH` build arg.
However, higher level build tools, such as Docker Buildx (>= 0.10), may automatically capture the environment value.
The `buildctl` CLI (<= 0.12) does not automatically propagate the `$SOURCE_DATE_EPOCH` environment value from the client host to the `SOURCE_DATE_EPOCH` build arg.
<!-- TODO: s/master/v0.13/ -->
In the `master` branch of BuildKit, the `buildctl` CLI is updated to automatically capture the environment value.
Docker Buildx (>= 0.10) automatically captures the environment value too.

The build arg value is used for:
- the `created` timestamp in the [OCI Image Config](https://github.com/opencontainers/image-spec/blob/main/config.md#properties)
Expand Down

0 comments on commit b40359f

Please sign in to comment.