From 7e11d3601eff55f8827acad0174e55188bec9be3 Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Tue, 22 Jul 2025 14:23:12 +0200 Subject: [PATCH] history: use built-in build-arg to override the build name Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> --- commands/history/utils.go | 4 ++++ tests/history.go | 40 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/commands/history/utils.go b/commands/history/utils.go index 7925664cf..2e64889fc 100644 --- a/commands/history/utils.go +++ b/commands/history/utils.go @@ -26,6 +26,10 @@ import ( const recordsLimit = 50 func buildName(fattrs map[string]string, ls *localstate.State) string { + if v, ok := fattrs["build-arg:BUILDKIT_BUILD_NAME"]; ok && v != "" { + return v + } + var res string var target, contextPath, dockerfilePath, vcsSource string diff --git a/tests/history.go b/tests/history.go index 4cb100a18..ed2d93728 100644 --- a/tests/history.go +++ b/tests/history.go @@ -20,6 +20,7 @@ var historyTests = []func(t *testing.T, sb integration.Sandbox){ testHistoryLs, testHistoryRm, testHistoryLsStoppedBuilder, + testHistoryBuildNameOverride, } func testHistoryExport(t *testing.T, sb integration.Sandbox) { @@ -136,6 +137,45 @@ func testHistoryLsStoppedBuilder(t *testing.T, sb integration.Sandbox) { require.NoError(t, err, string(bout)) } +func testHistoryBuildNameOverride(t *testing.T, sb integration.Sandbox) { + dir := createTestProject(t) + out, err := buildCmd(sb, withArgs("--build-arg=BUILDKIT_BUILD_NAME=foobar", "--metadata-file", filepath.Join(dir, "md.json"), dir)) + require.NoError(t, err, string(out)) + + dt, err := os.ReadFile(filepath.Join(dir, "md.json")) + require.NoError(t, err) + + type mdT struct { + BuildRef string `json:"buildx.build.ref"` + } + var md mdT + err = json.Unmarshal(dt, &md) + require.NoError(t, err) + + refParts := strings.Split(md.BuildRef, "/") + require.Len(t, refParts, 3) + + cmd := buildxCmd(sb, withArgs("history", "ls", "--filter=ref="+refParts[2], "--format=json")) + bout, err := cmd.Output() + require.NoError(t, err, string(bout)) + + type recT struct { + Ref string `json:"ref"` + Name string `json:"name"` + Status string `json:"status"` + CreatedAt *time.Time `json:"created_at"` + CompletedAt *time.Time `json:"completed_at"` + TotalSteps int32 `json:"total_steps"` + CompletedSteps int32 `json:"completed_steps"` + CachedSteps int32 `json:"cached_steps"` + } + var rec recT + err = json.Unmarshal(bout, &rec) + require.NoError(t, err) + require.Equal(t, md.BuildRef, rec.Ref) + require.Equal(t, "foobar", rec.Name) +} + type buildRef struct { Builder string Node string