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

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2025-07-22 14:34:36 +02:00
parent 3f4bf829d8
commit 7e11d3601e
2 changed files with 44 additions and 0 deletions
+4
View File
@@ -26,6 +26,10 @@ import (
const recordsLimit = 50 const recordsLimit = 50
func buildName(fattrs map[string]string, ls *localstate.State) string { 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 res string
var target, contextPath, dockerfilePath, vcsSource string var target, contextPath, dockerfilePath, vcsSource string
+40
View File
@@ -20,6 +20,7 @@ var historyTests = []func(t *testing.T, sb integration.Sandbox){
testHistoryLs, testHistoryLs,
testHistoryRm, testHistoryRm,
testHistoryLsStoppedBuilder, testHistoryLsStoppedBuilder,
testHistoryBuildNameOverride,
} }
func testHistoryExport(t *testing.T, sb integration.Sandbox) { 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)) 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 { type buildRef struct {
Builder string Builder string
Node string Node string