Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,11 @@ x-vars:
build-src: &build-src
build:
dockerfile_path: /foo/bar/${SOME_ARG}
args:
VERSION: ${SOME_ARG}
MESSAGE: 'say "hi"'
QUOTED: 'say "${SOME_ARG}"'
ESCAPED: '\${SOME_ARG}'
source: *http-src

sources:
Expand Down Expand Up @@ -1151,6 +1156,12 @@ targets:
assert.Check(t, cmp.Equal(spec.Sources["context"].Context.Name, "test"))
assert.Check(t, cmp.Equal(spec.Sources["build"].Build.DockerfilePath, "/foo/bar/test"))
assert.Check(t, cmp.Equal(spec.Sources["build"].Build.Source.HTTP.URL, "https://test"))
assert.DeepEqual(t, spec.Sources["build"].Build.Args, map[string]string{
"VERSION": "test",
"MESSAGE": `say "hi"`,
"QUOTED": `say "test"`,
"ESCAPED": `\${SOME_ARG}`,
})

assert.Check(t, cmp.Equal(spec.Build.Env["TEST_TOP"], "test"))
assert.Check(t, cmp.Equal(spec.Build.Steps[0].Env["TEST"], "test"))
Expand Down
14 changes: 14 additions & 0 deletions source_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,20 @@ func (src *SourceBuild) processBuildArgs(lex *shell.Lex, args map[string]string,
}
src.Target = updated

// Build argument values are passed directly to the Dockerfile frontend.
// Expand references without removing literal quotes or backslashes.
rawLex := *lex
rawLex.SkipProcessQuotes = true
rawLex.RawEscapes = true
for name, value := range SortedMapIter(src.Args) {
updated, err := expandArgs(&rawLex, value, args, allowArg)
if err != nil {
errs = append(errs, fmt.Errorf("arg %q: %w", name, err))
continue
}
src.Args[name] = updated
}

if len(errs) > 0 {
return fmt.Errorf("failed to expand args on build source: %w", goerrors.Join(errs...))
}
Expand Down
20 changes: 20 additions & 0 deletions test/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,26 @@ EOF
doBuildTest(t, "dir", newBuildSpec("foo", dirSrc("foo")))
})
})

t.Run("substitute build args", func(t *testing.T) {
t.Parallel()
spec := newBuildSpec("", func() dalec.Source {
return dalec.Source{Inline: &dalec.SourceInline{File: &dalec.SourceInlineFile{Contents: `
FROM scratch
ARG MESSAGE
COPY <<EOF /hello
${MESSAGE}
EOF
`}}}
})
spec.Args = map[string]string{"VERSION": "1.2.3"}
spec.Sources["test"].Build.Args = map[string]string{"MESSAGE": `version "${VERSION}"`}

testEnv.RunTest(baseCtx, t, func(ctx context.Context, gwc gwclient.Client) {
res := solveT(ctx, t, gwc, newSolveRequest(withBuildTarget("debug/sources"), withSpec(ctx, t, spec)))
checkFile(ctx, t, "test/hello", res, []byte("version \"1.2.3\"\n"))
})
})
}

func TestSourceHTTP(t *testing.T) {
Expand Down