mirror of
https://github.com/GoogleContainerTools/kaniko
synced 2026-10-18 18:58:32 +02:00
Issue #439 double quotes in ARG value
* Strip out double quotes enclosing ARG value after parsing dockerfile
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
ARG FILE_NAME="myFile"
|
||||
|
||||
FROM busybox:latest AS builder
|
||||
ARG FILE_NAME
|
||||
|
||||
RUN echo $FILE_NAME && touch /$FILE_NAME.txt && stat /$FILE_NAME.txt;
|
||||
|
||||
FROM busybox:latest
|
||||
ARG FILE_NAME
|
||||
|
||||
RUN echo $FILE_NAME && touch /$FILE_NAME.txt && stat /$FILE_NAME.txt;
|
||||
COPY --from=builder /$FILE_NAME.txt /
|
||||
@@ -111,9 +111,33 @@ func Parse(b []byte) ([]instructions.Stage, []instructions.ArgCommand, error) {
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
metaArgs = stripEnclosingDoubleQuotes(metaArgs)
|
||||
|
||||
return stages, metaArgs, nil
|
||||
}
|
||||
|
||||
// stripEnclosingDoubleQuotes removes double quotes enclosing the value of each instructions.ArgCommand in a slice
|
||||
func stripEnclosingDoubleQuotes(metaArgs []instructions.ArgCommand) []instructions.ArgCommand {
|
||||
for i := range metaArgs {
|
||||
arg := metaArgs[i]
|
||||
v := arg.Value
|
||||
if v != nil {
|
||||
val := *v
|
||||
if val[0] == '"' {
|
||||
val = val[1:]
|
||||
}
|
||||
|
||||
if val[len(val)-1] == '"' {
|
||||
val = val[:len(val)-1]
|
||||
}
|
||||
arg.Value = &val
|
||||
metaArgs[i] = arg
|
||||
}
|
||||
}
|
||||
return metaArgs
|
||||
}
|
||||
|
||||
// targetStage returns the index of the target stage kaniko is trying to build
|
||||
func targetStage(stages []instructions.Stage, target string) (int, error) {
|
||||
if target == "" {
|
||||
|
||||
@@ -17,13 +17,64 @@ limitations under the License.
|
||||
package dockerfile
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/GoogleContainerTools/kaniko/pkg/config"
|
||||
"github.com/GoogleContainerTools/kaniko/testutil"
|
||||
"github.com/moby/buildkit/frontend/dockerfile/instructions"
|
||||
)
|
||||
|
||||
func TestStagesArgValueWithQuotes(t *testing.T) {
|
||||
dockerfile := `
|
||||
ARG IMAGE="ubuntu:16.04"
|
||||
FROM ${IMAGE}
|
||||
RUN echo hi > /hi
|
||||
|
||||
FROM scratch AS second
|
||||
COPY --from=0 /hi /hi2
|
||||
|
||||
FROM scratch
|
||||
COPY --from=second /hi2 /hi3
|
||||
`
|
||||
tmpfile, err := ioutil.TempFile("", "Dockerfile.test")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
defer os.Remove(tmpfile.Name())
|
||||
|
||||
if _, err := tmpfile.Write([]byte(dockerfile)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := tmpfile.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
stages, err := Stages(&config.KanikoOptions{DockerfilePath: tmpfile.Name()})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(stages) == 0 {
|
||||
t.Fatal("length of stages expected to be greater than zero, but was zero")
|
||||
|
||||
}
|
||||
|
||||
if len(stages[0].MetaArgs) == 0 {
|
||||
t.Fatal("length of stage[0] meta args expected to be greater than zero, but was zero")
|
||||
}
|
||||
|
||||
expectedVal := "ubuntu:16.04"
|
||||
|
||||
arg := stages[0].MetaArgs[0]
|
||||
if arg.ValueString() != expectedVal {
|
||||
t.Fatalf("expected stages[0].MetaArgs[0] val to be %s but was %s", expectedVal, arg.ValueString())
|
||||
}
|
||||
}
|
||||
|
||||
func Test_resolveStages(t *testing.T) {
|
||||
dockerfile := `
|
||||
FROM scratch
|
||||
|
||||
Reference in New Issue
Block a user