Merge pull request #32950 from dnephin/cherry-pick-build-arg-fixes

[17.05.x] Cherry pick build arg fixes
This commit is contained in:
Victor Vieux 2017-05-01 22:45:51 -07:00 committed by GitHub
commit 947cc7b591
2 changed files with 39 additions and 29 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,26 +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
tmpBuildEnv := make([]string, len(cmdBuildEnv))
copy(tmpBuildEnv, cmdBuildEnv)
for i, env := range tmpBuildEnv {
key := strings.SplitN(env, "=", 2)[0]
if b.buildArgs.IsUnreferencedBuiltin(key) {
tmpBuildEnv = append(tmpBuildEnv[:i], tmpBuildEnv[i+1:]...)
}
}
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,23 +4344,37 @@ func (s *DockerSuite) TestBuildTimeArgHistoryExclusions(c *check.C) {
ARG %s
ARG %s
RUN echo "Testing Build Args!"`, envKey, explicitProxyKey)
buildImage(imgName,
cli.WithFlags("--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!")
}
if strings.Contains(out, "https_proxy") {
c.Fatalf("failed to exclude proxy settings from history!")
}
if !strings.Contains(out, fmt.Sprintf("%s=%s", envKey, envVal)) {
c.Fatalf("explicitly defined ARG %s is not in output", explicitProxyKey)
}
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) {
@ -6156,7 +6170,7 @@ func (s *DockerSuite) TestBuildCopyFromPreviousFromWindows(c *check.C) {
func (s *DockerSuite) TestBuildCopyFromForbidWindowsSystemPaths(c *check.C) {
testRequires(c, DaemonIsWindows)
dockerfile := `
FROM ` + testEnv.MinimalBaseImage() + `
FROM ` + testEnv.MinimalBaseImage() + `
FROM ` + testEnv.MinimalBaseImage() + `
COPY --from=0 %s c:\\oscopy
`
@ -6173,7 +6187,7 @@ func (s *DockerSuite) TestBuildCopyFromForbidWindowsSystemPaths(c *check.C) {
func (s *DockerSuite) TestBuildCopyFromForbidWindowsRelativePaths(c *check.C) {
testRequires(c, DaemonIsWindows)
dockerfile := `
FROM ` + testEnv.MinimalBaseImage() + `
FROM ` + testEnv.MinimalBaseImage() + `
FROM ` + testEnv.MinimalBaseImage() + `
COPY --from=0 %s c:\\oscopy
`
@ -6192,7 +6206,7 @@ func (s *DockerSuite) TestBuildCopyFromWindowsIsCaseInsensitive(c *check.C) {
testRequires(c, DaemonIsWindows)
dockerfile := `
FROM ` + testEnv.MinimalBaseImage() + `
COPY foo /
COPY foo /
FROM ` + testEnv.MinimalBaseImage() + `
COPY --from=0 c:\\fOo c:\\copied
RUN type c:\\copied