Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Parsing fails for named multistage dockerfile using build artifact dependency #5507

Merged
merged 1 commit into from
Mar 11, 2021
Merged
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
27 changes: 27 additions & 0 deletions pkg/skaffold/docker/dependencies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ FROM nginx
ADD *.go *.none /tmp/
`

const simpleArtifactDependency = `
ARG BASE
FROM $BASE
COPY worker.go .
`

const multiStageArtifactDependency = `
ARG BASE
FROM golang:1.9.2
COPY worker.go .

FROM $BASE AS foo
FROM foo as bar
`

const multiStageDockerfile1 = `
FROM golang:1.9.2
WORKDIR /go/src/github.com/r2d4/leeroy/
Expand Down Expand Up @@ -325,6 +340,18 @@ func TestGetDependencies(t *testing.T) {
workspace: "",
expected: []string{"Dockerfile", "server.go", "worker.go"},
},
{
description: "simple dockerfile with artifact dependency",
dockerfile: simpleArtifactDependency,
workspace: "",
expected: []string{"Dockerfile", "worker.go"},
},
{
description: "multistage dockerfile with artifact dependency",
dockerfile: multiStageArtifactDependency,
workspace: "",
expected: []string{"Dockerfile", "worker.go"},
},
{
description: "copy twice",
dockerfile: multiCopy,
Expand Down
19 changes: 9 additions & 10 deletions pkg/skaffold/docker/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,16 @@ func extractCopyCommands(nodes []*parser.Node, onlyLastImage bool, cfg Config) (
switch node.Value {
case command.From:
from := fromInstruction(node)
if from.as != "" {
// Stage names are case insensitive
stages[strings.ToLower(from.as)] = true
}

if from.image == "" {
// some build args like artifact dependencies are not available until the first build sequence has completed.
// skip check if there are unavailable images
continue
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

immediately continuing with the next iteration was wrong here; we need to save the instruction's AS value also to stages map.

}
if from.as != "" {
// Stage names are case insensitive
stages[strings.ToLower(from.as)] = true
}

// If `from` references a previous stage, then the `workdir`
// was already changed.
Expand Down Expand Up @@ -324,15 +325,13 @@ func expandOnbuildInstructions(nodes []*parser.Node, cfg Config) ([]*parser.Node
expandedNodes = append(expandedNodes, nodes[n:m+1]...)
n = m + 1

if from.image == "" {
// some build args like artifact dependencies are not available until the first build sequence has completed.
// skip check if there are unavailable images
continue
}

var onbuildNodes []*parser.Node
if ons, found := onbuildNodesCache[strings.ToLower(from.image)]; found {
onbuildNodes = ons
} else if from.image == "" {
// some build args like artifact dependencies are not available until the first build sequence has completed.
// skip check if there are unavailable images
onbuildNodes = []*parser.Node{}
} else if ons, err := parseOnbuild(from.image, cfg); err == nil {
onbuildNodes = ons
} else if warnMsg, ok := sErrors.IsOldImageManifestProblem(err); ok && warnMsg != "" {
Expand Down