Skip to content

history: use built-in build-arg to override the build name #3330

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

Merged
merged 1 commit into from
Jul 22, 2025
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
4 changes: 4 additions & 0 deletions commands/history/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions tests/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down