Skip to content

Commit aecbdee

Browse files
AkihiroSudaiQQBot
authored andcommitted
buildctl: propagate SOURCE_DATE_EPOCH from client env to build arg
Fix issue 4227 Signed-off-by: Akihiro Suda <[email protected]>
1 parent 85c43ab commit aecbdee

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

cmd/buildctl/build/opt.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
package build
22

33
func ParseOpt(opts []string) (map[string]string, error) {
4-
return attrMap(opts)
4+
m := loadOptEnv()
5+
m2, err := attrMap(opts)
6+
if err != nil {
7+
return nil, err
8+
}
9+
for k, v := range m2 {
10+
m[k] = v
11+
}
12+
return m, nil
513
}

cmd/buildctl/build/util.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/pkg/errors"
77

88
"github.com/moby/buildkit/client"
9+
"github.com/moby/buildkit/util/bklog"
910
)
1011

1112
// loadGithubEnv verify that url and token attributes exists in the
@@ -31,3 +32,17 @@ func loadGithubEnv(cache client.CacheOptionsEntry) (client.CacheOptionsEntry, er
3132
}
3233
return cache, nil
3334
}
35+
36+
// loadOptEnv loads opt values from the environment.
37+
// The returned map is always non-nil.
38+
func loadOptEnv() map[string]string {
39+
m := make(map[string]string)
40+
propagatableEnvs := []string{"SOURCE_DATE_EPOCH"}
41+
for _, env := range propagatableEnvs {
42+
if v, ok := os.LookupEnv(env); ok {
43+
bklog.L.Debugf("Propagating %s from the client env to the build arg", env)
44+
m["build-arg:"+env] = v
45+
}
46+
}
47+
return m
48+
}

docs/build-repro.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ Minimal support is also available on older BuildKit when using Dockerfile 1.5 fr
4747
buildctl build --frontend dockerfile.v0 --opt build-arg:SOURCE_DATE_EPOCH=$(git log -1 --pretty=%ct) ...
4848
```
4949

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

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

0 commit comments

Comments
 (0)