Skip to content
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

build: propagate SOURCE_DATE_EPOCH from the client env #1926

Merged
merged 1 commit into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions cmd/nerdctl/builder_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,32 @@ CMD ["echo", "nerdctl-build-notag-string"]
base.Cmd("images").AssertOutContains("<none>")
base.Cmd("image", "prune", "--force", "--all").AssertOK()
}

// TestBuildSourceDateEpoch tests that $SOURCE_DATE_EPOCH is propagated from the client env
// https://github.com/docker/buildx/pull/1482
func TestBuildSourceDateEpoch(t *testing.T) {
testutil.RequiresBuild(t)
testutil.DockerIncompatible(t) // Needs buildx v0.10 (https://github.com/docker/buildx/pull/1489)
base := testutil.NewBase(t)
imageName := testutil.Identifier(t)
defer base.Cmd("rmi", imageName).AssertOK()

dockerfile := fmt.Sprintf(`FROM %s
ARG SOURCE_DATE_EPOCH
RUN echo $SOURCE_DATE_EPOCH >/source-date-epoch
CMD ["cat", "/source-date-epoch"]
`, testutil.CommonImage)

buildCtx, err := createBuildContext(dockerfile)
assert.NilError(t, err)
defer os.RemoveAll(buildCtx)

const sourceDateEpochEnvStr = "1111111111"
t.Setenv("SOURCE_DATE_EPOCH", sourceDateEpochEnvStr)
base.Cmd("build", "-t", imageName, buildCtx).AssertOK()
base.Cmd("run", "--rm", imageName).AssertOutExactly(sourceDateEpochEnvStr + "\n")

const sourceDateEpochArgStr = "2222222222"
base.Cmd("build", "-t", imageName, "--build-arg", "SOURCE_DATE_EPOCH="+sourceDateEpochArgStr, buildCtx).AssertOK()
base.Cmd("run", "--rm", imageName).AssertOutExactly(sourceDateEpochArgStr + "\n")
}
10 changes: 10 additions & 0 deletions pkg/cmd/builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,10 @@ func generateBuildctlArgs(ctx context.Context, client *containerd.Client, option
buildctlArgs = append(buildctlArgs, "--opt=platform="+strings.Join(options.Platform, ","))
}

seenBuildArgs := make(map[string]struct{})
for _, ba := range strutil.DedupeStrSlice(options.BuildArgs) {
arr := strings.Split(ba, "=")
seenBuildArgs[arr[0]] = struct{}{}
if len(arr) == 1 && len(arr[0]) > 0 {
// Avoid masking default build arg value from Dockerfile if environment variable is not set
// https://github.com/moby/moby/issues/24101
Expand Down Expand Up @@ -304,6 +306,14 @@ func generateBuildctlArgs(ctx context.Context, client *containerd.Client, option
}
}

// Propagate SOURCE_DATE_EPOCH from the client env
// https://github.com/docker/buildx/pull/1482
if v := os.Getenv("SOURCE_DATE_EPOCH"); v != "" {
if _, ok := seenBuildArgs["SOURCE_DATE_EPOCH"]; !ok {
buildctlArgs = append(buildctlArgs, "--opt=build-arg:SOURCE_DATE_EPOCH="+v)
}
}

for _, l := range strutil.DedupeStrSlice(options.Label) {
buildctlArgs = append(buildctlArgs, "--opt=label:"+l)
}
Expand Down