dockerfile: apply dockerignore on loading local contexts

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
master
Tonis Tiigi 2022-01-07 18:16:28 -08:00
parent ffe2301031
commit 507d18188c
2 changed files with 45 additions and 2 deletions

View File

@ -863,7 +863,44 @@ func contextByName(ctx context.Context, c client.Client, name string, platform *
}
return st, nil, nil
case "local":
st := llb.Local(vv[1], llb.WithCustomName("[context "+name+"] load from client"), llb.SessionID(c.BuildOpts().SessionID), llb.SharedKeyHint("context:"+name))
st := llb.Local(vv[1],
llb.SessionID(c.BuildOpts().SessionID),
llb.FollowPaths([]string{dockerignoreFilename}),
llb.SharedKeyHint("context:"+name+"-"+dockerignoreFilename),
llb.WithCustomName("[context "+name+"] load "+dockerignoreFilename),
llb.Differ(llb.DiffNone, false),
)
def, err := st.Marshal(ctx)
if err != nil {
return nil, nil, err
}
res, err := c.Solve(ctx, client.SolveRequest{
Evaluate: true,
Definition: def.ToPB(),
})
if err != nil {
return nil, nil, err
}
ref, err := res.SingleRef()
if err != nil {
return nil, nil, err
}
dt, _ := ref.ReadFile(ctx, client.ReadRequest{
Filename: dockerignoreFilename,
}) // error ignored
var excludes []string
if len(dt) != 0 {
excludes, err = dockerignore.ReadAll(bytes.NewBuffer(dt))
if err != nil {
return nil, nil, err
}
}
st = llb.Local(vv[1],
llb.WithCustomName("[context "+name+"] load from client"),
llb.SessionID(c.BuildOpts().SessionID),
llb.SharedKeyHint("context:"+name),
llb.ExcludePatterns(excludes),
)
return &st, nil, nil
case "input":
inputs, err := c.Inputs(ctx)

View File

@ -5471,7 +5471,7 @@ func testNamedLocalContext(t *testing.T, sb integration.Sandbox) {
FROM busybox AS base
RUN cat /etc/alpine-release > /out
FROM scratch
COPY --from=base /out /
COPY --from=base /o* /
`)
dir, err := tmpdir(
@ -5484,6 +5484,8 @@ COPY --from=base /out /
dir2, err := tmpdir(
fstest.CreateFile("out", outf, 0600),
fstest.CreateFile("out2", outf, 0600),
fstest.CreateFile(".dockerignore", []byte("out2\n"), 0600),
)
require.NoError(t, err)
defer os.RemoveAll(dir2)
@ -5515,6 +5517,10 @@ COPY --from=base /out /
dt, err := ioutil.ReadFile(filepath.Join(destDir, "out"))
require.NoError(t, err)
require.True(t, len(dt) > 0)
_, err = ioutil.ReadFile(filepath.Join(destDir, "out2"))
require.Error(t, err)
require.True(t, errors.Is(err, os.ErrNotExist))
}
func testNamedInputContext(t *testing.T, sb integration.Sandbox) {