Skip to content

Commit 4bf7b02

Browse files
committed
history: input:context attribute support
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
1 parent 00b17d5 commit 4bf7b02

File tree

4 files changed

+107
-37
lines changed

4 files changed

+107
-37
lines changed

commands/history/inspect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ workers0:
243243
}
244244
delete(attrs, "filename")
245245

246-
out.Name = buildName(rec.FrontendAttrs, st)
246+
out.Name = BuildName(rec.FrontendAttrs, st)
247247
out.Ref = rec.Ref
248248

249249
out.Context = context

commands/history/ls.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func runLs(ctx context.Context, dockerCli command.Cli, opts lsOptions) error {
8585

8686
for i, rec := range out {
8787
st, _ := ls.ReadRef(rec.node.Builder, rec.node.Name, rec.Ref)
88-
rec.name = buildName(rec.FrontendAttrs, st)
88+
rec.name = BuildName(rec.FrontendAttrs, st)
8989
out[i] = rec
9090
}
9191

commands/history/utils.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ import (
1919
"github.com/docker/cli/cli/command"
2020
controlapi "github.com/moby/buildkit/api/services/control"
2121
"github.com/moby/buildkit/frontend/dockerfile/dfgitutil"
22+
"github.com/moby/buildkit/util/gitutil"
2223
"github.com/pkg/errors"
2324
"golang.org/x/sync/errgroup"
2425
)
2526

2627
const recordsLimit = 50
2728

28-
func buildName(fattrs map[string]string, ls *localstate.State) string {
29+
func BuildName(fattrs map[string]string, ls *localstate.State) string {
2930
if v, ok := fattrs["build-arg:BUILDKIT_BUILD_NAME"]; ok && v != "" {
3031
return v
3132
}
@@ -43,6 +44,8 @@ func buildName(fattrs map[string]string, ls *localstate.State) string {
4344
}
4445
if v, ok := fattrs["vcs:source"]; ok {
4546
vcsSource = v
47+
} else if v, ok := fattrs["input:context"]; ok && build.IsRemoteURL(v) {
48+
vcsSource = v
4649
}
4750
if v, ok := fattrs["filename"]; ok && v != "Dockerfile" {
4851
dockerfilePath = filepath.ToSlash(v)
@@ -99,7 +102,8 @@ func buildName(fattrs map[string]string, ls *localstate.State) string {
99102
}
100103
}
101104
if res == "" && vcsSource != "" {
102-
return vcsSource
105+
u, _ := gitutil.FragmentFormat(vcsSource)
106+
return u
103107
}
104108
return res
105109
}

tests/history.go

Lines changed: 99 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import (
99
"testing"
1010
"time"
1111

12+
"github.com/containerd/continuity/fs/fstest"
13+
"github.com/docker/buildx/util/gitutil"
14+
"github.com/docker/buildx/util/gitutil/gittestutil"
1215
"github.com/moby/buildkit/util/testutil/integration"
1316
"github.com/stretchr/testify/require"
1417
)
@@ -20,7 +23,7 @@ var historyTests = []func(t *testing.T, sb integration.Sandbox){
2023
testHistoryLs,
2124
testHistoryRm,
2225
testHistoryLsStoppedBuilder,
23-
testHistoryBuildNameOverride,
26+
testHistoryBuildName,
2427
}
2528

2629
func testHistoryExport(t *testing.T, sb integration.Sandbox) {
@@ -137,43 +140,106 @@ func testHistoryLsStoppedBuilder(t *testing.T, sb integration.Sandbox) {
137140
require.NoError(t, err, string(bout))
138141
}
139142

140-
func testHistoryBuildNameOverride(t *testing.T, sb integration.Sandbox) {
141-
dir := createTestProject(t)
142-
out, err := buildCmd(sb, withArgs("--build-arg=BUILDKIT_BUILD_NAME=foobar", "--metadata-file", filepath.Join(dir, "md.json"), dir))
143-
require.NoError(t, err, string(out))
143+
func testHistoryBuildName(t *testing.T, sb integration.Sandbox) {
144+
t.Run("override", func(t *testing.T) {
145+
dir := createTestProject(t)
146+
out, err := buildCmd(sb, withArgs("--build-arg=BUILDKIT_BUILD_NAME=foobar", "--metadata-file", filepath.Join(dir, "md.json"), dir))
147+
require.NoError(t, err, string(out))
144148

145-
dt, err := os.ReadFile(filepath.Join(dir, "md.json"))
146-
require.NoError(t, err)
149+
dt, err := os.ReadFile(filepath.Join(dir, "md.json"))
150+
require.NoError(t, err)
147151

148-
type mdT struct {
149-
BuildRef string `json:"buildx.build.ref"`
150-
}
151-
var md mdT
152-
err = json.Unmarshal(dt, &md)
153-
require.NoError(t, err)
152+
type mdT struct {
153+
BuildRef string `json:"buildx.build.ref"`
154+
}
155+
var md mdT
156+
err = json.Unmarshal(dt, &md)
157+
require.NoError(t, err)
158+
159+
refParts := strings.Split(md.BuildRef, "/")
160+
require.Len(t, refParts, 3)
161+
162+
cmd := buildxCmd(sb, withArgs("history", "ls", "--filter=ref="+refParts[2], "--format=json"))
163+
bout, err := cmd.Output()
164+
require.NoError(t, err, string(bout))
165+
166+
type recT struct {
167+
Ref string `json:"ref"`
168+
Name string `json:"name"`
169+
Status string `json:"status"`
170+
CreatedAt *time.Time `json:"created_at"`
171+
CompletedAt *time.Time `json:"completed_at"`
172+
TotalSteps int32 `json:"total_steps"`
173+
CompletedSteps int32 `json:"completed_steps"`
174+
CachedSteps int32 `json:"cached_steps"`
175+
}
176+
var rec recT
177+
err = json.Unmarshal(bout, &rec)
178+
require.NoError(t, err)
179+
require.Equal(t, md.BuildRef, rec.Ref)
180+
require.Equal(t, "foobar", rec.Name)
181+
})
154182

155-
refParts := strings.Split(md.BuildRef, "/")
156-
require.Len(t, refParts, 3)
183+
t.Run("git query", func(t *testing.T) {
184+
dockerfile := []byte(`
185+
FROM busybox:latest
186+
COPY foo /foo
187+
`)
188+
dir := tmpdir(
189+
t,
190+
fstest.CreateFile("Dockerfile", dockerfile, 0600),
191+
fstest.CreateFile("foo", []byte("foo"), 0600),
192+
)
193+
dirDest := t.TempDir()
194+
195+
git, err := gitutil.New(gitutil.WithWorkingDir(dir))
196+
require.NoError(t, err)
197+
198+
gittestutil.GitInit(git, t)
199+
gittestutil.GitAdd(git, t, "Dockerfile", "foo")
200+
gittestutil.GitCommit(git, t, "initial commit")
201+
addr := gittestutil.GitServeHTTP(git, t)
202+
203+
out, err := buildCmd(sb, withDir(dir),
204+
withArgs("--output=type=local,dest="+dirDest, "--metadata-file", filepath.Join(dir, "md.json"), addr+"?ref=main"),
205+
withEnv("BUILDX_SEND_GIT_QUERY_AS_INPUT=true"),
206+
)
207+
require.NoError(t, err, out)
208+
require.FileExists(t, filepath.Join(dirDest, "foo"))
157209

158-
cmd := buildxCmd(sb, withArgs("history", "ls", "--filter=ref="+refParts[2], "--format=json"))
159-
bout, err := cmd.Output()
160-
require.NoError(t, err, string(bout))
210+
dt, err := os.ReadFile(filepath.Join(dir, "md.json"))
211+
require.NoError(t, err)
161212

162-
type recT struct {
163-
Ref string `json:"ref"`
164-
Name string `json:"name"`
165-
Status string `json:"status"`
166-
CreatedAt *time.Time `json:"created_at"`
167-
CompletedAt *time.Time `json:"completed_at"`
168-
TotalSteps int32 `json:"total_steps"`
169-
CompletedSteps int32 `json:"completed_steps"`
170-
CachedSteps int32 `json:"cached_steps"`
171-
}
172-
var rec recT
173-
err = json.Unmarshal(bout, &rec)
174-
require.NoError(t, err)
175-
require.Equal(t, md.BuildRef, rec.Ref)
176-
require.Equal(t, "foobar", rec.Name)
213+
type mdT struct {
214+
BuildRef string `json:"buildx.build.ref"`
215+
}
216+
var md mdT
217+
err = json.Unmarshal(dt, &md)
218+
require.NoError(t, err)
219+
220+
refParts := strings.Split(md.BuildRef, "/")
221+
require.Len(t, refParts, 3)
222+
223+
cmd := buildxCmd(sb, withArgs("history", "ls", "--filter=ref="+refParts[2], "--format=json"))
224+
bout, err := cmd.Output()
225+
require.NoError(t, err, string(bout))
226+
227+
type recT struct {
228+
Ref string `json:"ref"`
229+
Name string `json:"name"`
230+
Status string `json:"status"`
231+
CreatedAt *time.Time `json:"created_at"`
232+
CompletedAt *time.Time `json:"completed_at"`
233+
TotalSteps int32 `json:"total_steps"`
234+
CompletedSteps int32 `json:"completed_steps"`
235+
CachedSteps int32 `json:"cached_steps"`
236+
}
237+
var rec recT
238+
err = json.Unmarshal(bout, &rec)
239+
require.NoError(t, err)
240+
require.Equal(t, md.BuildRef, rec.Ref)
241+
require.Equal(t, addr+"#main", rec.Name)
242+
})
177243
}
178244

179245
type buildRef struct {

0 commit comments

Comments
 (0)