dockerfile: handle args with unset values

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
docker-19.03
Tonis Tiigi 2019-04-02 13:56:38 -07:00
parent 9b081e607d
commit a6e29dcdd4
3 changed files with 61 additions and 4 deletions

View File

@ -623,9 +623,11 @@ func dispatchRun(d *dispatchState, c *instructions.RunCommand, proxy *llb.ProxyE
env := d.state.Env()
opt := []llb.RunOption{llb.Args(args)}
for _, arg := range d.buildArgs {
if arg.Value != nil {
env = append(env, fmt.Sprintf("%s=%s", arg.Key, arg.ValueString()))
opt = append(opt, llb.AddEnv(arg.Key, arg.ValueString()))
}
}
opt = append(opt, dfCmd(c))
if d.ignoreCache {
opt = append(opt, llb.IgnoreCache)
@ -1066,8 +1068,10 @@ func toEnvMap(args []instructions.KeyValuePairOptional, env []string) map[string
if _, ok := m[arg.Key]; ok {
continue
}
if arg.Value != nil {
m[arg.Key] = arg.ValueString()
}
}
return m
}

View File

@ -116,7 +116,7 @@ func TestToEnvList(t *testing.T) {
args = []instructions.KeyValuePairOptional{{Key: "key2", Value: nil}}
env = []string{"key1=val1"}
resutl = toEnvMap(args, env)
assert.Equal(t, map[string]string{"key1": "val1", "key2": ""}, resutl)
assert.Equal(t, map[string]string{"key1": "val1"}, resutl)
// args has duplicated key with env
v = "val2"

View File

@ -86,6 +86,7 @@ var allTests = []integration.Test{
testCopyWildcardCache,
testDockerignoreOverride,
testTarExporter,
testDefaultEnvWithArgs,
}
var fileOpTests = []integration.Test{
@ -146,6 +147,58 @@ func TestIntegration(t *testing.T) {
}))...)
}
func testDefaultEnvWithArgs(t *testing.T, sb integration.Sandbox) {
f := getFrontend(t, sb)
dockerfile := []byte(`
FROM busybox AS build
ARG my_arg
ENV my_arg "${my_arg:-def_val}"
COPY myscript.sh myscript.sh
RUN ./myscript.sh
FROM scratch
COPY --from=build /out /out
`)
script := []byte(`
#!/usr/bin/env sh
echo -n $my_arg > /out
`)
dir, err := tmpdir(
fstest.CreateFile("Dockerfile", dockerfile, 0600),
fstest.CreateFile("myscript.sh", script, 0700),
)
require.NoError(t, err)
defer os.RemoveAll(dir)
c, err := client.New(context.TODO(), sb.Address())
require.NoError(t, err)
defer c.Close()
destDir, err := ioutil.TempDir("", "buildkit")
require.NoError(t, err)
defer os.RemoveAll(destDir)
_, err = f.Solve(context.TODO(), c, client.SolveOpt{
Exports: []client.ExportEntry{
{
Type: client.ExporterLocal,
OutputDir: destDir,
},
},
LocalDirs: map[string]string{
builder.DefaultLocalNameDockerfile: dir,
builder.DefaultLocalNameContext: dir,
},
}, nil)
require.NoError(t, err)
dt, err := ioutil.ReadFile(filepath.Join(destDir, "out"))
require.NoError(t, err)
require.Equal(t, "def_val", string(dt))
}
func testDockerignoreOverride(t *testing.T, sb integration.Sandbox) {
f := getFrontend(t, sb)
dockerfile := []byte(`