Fix cache miss when builtin build args are used.

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2017-04-28 12:49:50 -04:00
parent 8c532a6822
commit a4e352ccb0
2 changed files with 33 additions and 26 deletions

View File

@ -408,9 +408,7 @@ func run(b *Builder, args []string, attributes map[string]bool, original string)
// that starts with "foo=abc" to be considered part of a build-time env var.
saveCmd := config.Cmd
if len(cmdBuildEnv) > 0 {
sort.Strings(cmdBuildEnv)
tmpEnv := append([]string{fmt.Sprintf("|%d", len(cmdBuildEnv))}, cmdBuildEnv...)
saveCmd = strslice.StrSlice(append(tmpEnv, saveCmd...))
saveCmd = prependEnvOnCmd(b.buildArgs, cmdBuildEnv, saveCmd)
}
b.runConfig.Cmd = saveCmd
@ -445,25 +443,24 @@ func run(b *Builder, args []string, attributes map[string]bool, original string)
// properly match it.
b.runConfig.Env = env
// remove builtinAllowedBuildArgs (see: builder.go) from the saveCmd
// these args are transparent so resulting image should be the same regardless of the value
if len(cmdBuildEnv) > 0 {
saveCmd = config.Cmd
var tmpBuildEnv []string
for _, env := range cmdBuildEnv {
key := strings.SplitN(env, "=", 2)[0]
if !b.buildArgs.IsUnreferencedBuiltin(key) {
tmpBuildEnv = append(tmpBuildEnv, env)
}
}
sort.Strings(tmpBuildEnv)
tmpEnv := append([]string{fmt.Sprintf("|%d", len(tmpBuildEnv))}, tmpBuildEnv...)
saveCmd = strslice.StrSlice(append(tmpEnv, saveCmd...))
}
b.runConfig.Cmd = saveCmd
return b.commit(cID, cmd, "run")
}
func prependEnvOnCmd(buildArgs *buildArgs, buildArgVars []string, cmd strslice.StrSlice) strslice.StrSlice {
var tmpBuildEnv []string
for _, env := range buildArgVars {
key := strings.SplitN(env, "=", 2)[0]
if !buildArgs.IsUnreferencedBuiltin(key) {
tmpBuildEnv = append(tmpBuildEnv, env)
}
}
sort.Strings(tmpBuildEnv)
tmpEnv := append([]string{fmt.Sprintf("|%d", len(tmpBuildEnv))}, tmpBuildEnv...)
return strslice.StrSlice(append(tmpEnv, cmd...))
}
// CMD foo
//
// Set the default command to run in the container (which may be empty).

View File

@ -4344,15 +4344,22 @@ func (s *DockerSuite) TestBuildTimeArgHistoryExclusions(c *check.C) {
ARG %s
ARG %s
RUN echo "Testing Build Args!"`, envKey, explicitProxyKey)
buildImage(imgName,
cli.WithFlags("--build-arg", "https_proxy=https://proxy.example.com",
"--build-arg", fmt.Sprintf("%s=%s", envKey, envVal),
"--build-arg", fmt.Sprintf("%s=%s", explicitProxyKey, explicitProxyVal),
"--build-arg", proxy),
build.WithDockerfile(dockerfile),
).Assert(c, icmd.Success)
out, _ := dockerCmd(c, "history", "--no-trunc", imgName)
buildImage := func(imgName string) string {
cli.BuildCmd(c, imgName,
cli.WithFlags("--build-arg", "https_proxy=https://proxy.example.com",
"--build-arg", fmt.Sprintf("%s=%s", envKey, envVal),
"--build-arg", fmt.Sprintf("%s=%s", explicitProxyKey, explicitProxyVal),
"--build-arg", proxy),
build.WithDockerfile(dockerfile),
)
return getIDByName(c, imgName)
}
origID := buildImage(imgName)
result := cli.DockerCmd(c, "history", "--no-trunc", imgName)
out := result.Stdout()
if strings.Contains(out, proxy) {
c.Fatalf("failed to exclude proxy settings from history!")
}
@ -4365,6 +4372,9 @@ func (s *DockerSuite) TestBuildTimeArgHistoryExclusions(c *check.C) {
if !strings.Contains(out, fmt.Sprintf("%s=%s", envKey, envVal)) {
c.Fatalf("missing build arguments from output")
}
cacheID := buildImage(imgName + "-two")
c.Assert(origID, checker.Equals, cacheID)
}
func (s *DockerSuite) TestBuildBuildTimeArgCacheHit(c *check.C) {