diff --git a/integration_tests/dockerfiles/config_test_scratch.json b/integration_tests/dockerfiles/config_test_scratch.json new file mode 100644 index 000000000..b9b8930fe --- /dev/null +++ b/integration_tests/dockerfiles/config_test_scratch.json @@ -0,0 +1,12 @@ +[ + { + "Image1": "gcr.io/kaniko-test/docker-test-scratch:latest", + "Image2": "gcr.io/kaniko-test/kaniko-test-scratch:latest", + "DiffType": "File", + "Diff": { + "Adds": null, + "Dels": null, + "Mods": null + } + } +] \ No newline at end of file diff --git a/pkg/commands/env_test.go b/pkg/commands/env_test.go index d4eec7502..16d7b33f7 100644 --- a/pkg/commands/env_test.go +++ b/pkg/commands/env_test.go @@ -23,37 +23,6 @@ import ( "testing" ) -func TestUpdateEnvConfig(t *testing.T) { - cfg := &v1.Config{ - Env: []string{ - "PATH=/path/to/dir", - "hey=hey", - }, - } - - newEnvs := []instructions.KeyValuePair{ - { - Key: "foo", - Value: "foo2", - }, - { - Key: "PATH", - Value: "/new/path/", - }, - { - Key: "foo", - Value: "newfoo", - }, - } - - expectedEnvArray := []string{ - "PATH=/new/path/", - "hey=hey", - "foo=newfoo", - } - updateConfigEnv(newEnvs, cfg) - testutil.CheckErrorAndDeepEqual(t, false, nil, expectedEnvArray, cfg.Env) -} func Test_EnvExecute(t *testing.T) { cfg := &v1.Config{ Env: []string{ diff --git a/pkg/dockerfile/dockerfile.go b/pkg/dockerfile/dockerfile.go index 4e295deba..973120d13 100644 --- a/pkg/dockerfile/dockerfile.go +++ b/pkg/dockerfile/dockerfile.go @@ -123,12 +123,15 @@ func Dependencies(index int, stages []instructions.Stage, buildArgs *BuildArgs) if err := util.UpdateConfigEnv(c.Env, &imageConfig.Config, replacementEnvs); err != nil { return nil, err } + case *instructions.ArgCommand: + buildArgs.AddArg(c.Key, c.Value) case *instructions.CopyCommand: if c.From != strconv.Itoa(index) { continue } // First, resolve any environment replacement - resolvedEnvs, err := util.ResolveEnvironmentReplacementList(c.SourcesAndDest, imageConfig.Config.Env, true) + replacementEnvs := buildArgs.ReplacementEnvs(imageConfig.Config.Env) + resolvedEnvs, err := util.ResolveEnvironmentReplacementList(c.SourcesAndDest, replacementEnvs, true) if err != nil { return nil, err } diff --git a/pkg/dockerfile/dockerfile_test.go b/pkg/dockerfile/dockerfile_test.go index 397ff3801..526bd3efd 100644 --- a/pkg/dockerfile/dockerfile_test.go +++ b/pkg/dockerfile/dockerfile_test.go @@ -89,7 +89,48 @@ func Test_Dependencies(t *testing.T) { } for index := range stages { - actualDeps, err := Dependencies(index, stages) + buildArgs := NewBuildArgs([]string{}) + actualDeps, err := Dependencies(index, stages, buildArgs) + testutil.CheckErrorAndDeepEqual(t, false, err, expectedDependencies[index], actualDeps) + } +} + +func Test_DependenciesWithArg(t *testing.T) { + testDir, err := ioutil.TempDir("", "") + if err != nil { + t.Fatal(err) + } + helloPath := filepath.Join(testDir, "hello") + if err := os.Mkdir(helloPath, 0755); err != nil { + t.Fatal(err) + } + + dockerfile := fmt.Sprintf(` + FROM scratch + COPY %s %s + + FROM scratch AS second + ARG hienv + COPY a b + COPY --from=0 /$hienv %s /hi2/ + `, helloPath, helloPath, testDir) + + stages, err := Parse([]byte(dockerfile)) + if err != nil { + t.Fatal(err) + } + + expectedDependencies := [][]string{ + { + helloPath, + testDir, + }, + nil, + } + buildArgs := NewBuildArgs([]string{fmt.Sprintf("hienv=%s", helloPath)}) + + for index := range stages { + actualDeps, err := Dependencies(index, stages, buildArgs) testutil.CheckErrorAndDeepEqual(t, false, err, expectedDependencies[index], actualDeps) } } diff --git a/pkg/executor/executor.go b/pkg/executor/executor.go index cd3ce879b..3b5425340 100644 --- a/pkg/executor/executor.go +++ b/pkg/executor/executor.go @@ -1,9 +1,12 @@ /* Copyright 2018 Google LLC + Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.