Skip to content

Commit 87c9ef0

Browse files
committed
fix: Parsing fails for named multistage dockerfile using build artifact dependency
1 parent 6d4fb48 commit 87c9ef0

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

pkg/skaffold/docker/dependencies_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,21 @@ FROM nginx
6363
ADD *.go *.none /tmp/
6464
`
6565

66+
const simpleArtifactDependency = `
67+
ARG BASE
68+
FROM $BASE
69+
COPY worker.go .
70+
`
71+
72+
const multiStageArtifactDependency = `
73+
ARG BASE
74+
FROM golang:1.9.2
75+
COPY worker.go .
76+
77+
FROM $BASE AS foo
78+
FROM foo as bar
79+
`
80+
6681
const multiStageDockerfile1 = `
6782
FROM golang:1.9.2
6883
WORKDIR /go/src/github.com/r2d4/leeroy/
@@ -325,6 +340,18 @@ func TestGetDependencies(t *testing.T) {
325340
workspace: "",
326341
expected: []string{"Dockerfile", "server.go", "worker.go"},
327342
},
343+
{
344+
description: "simple dockerfile with artifact dependency",
345+
dockerfile: simpleArtifactDependency,
346+
workspace: "",
347+
expected: []string{"Dockerfile", "worker.go"},
348+
},
349+
{
350+
description: "multistage dockerfile with artifact dependency",
351+
dockerfile: multiStageArtifactDependency,
352+
workspace: "",
353+
expected: []string{"Dockerfile", "worker.go"},
354+
},
328355
{
329356
description: "copy twice",
330357
dockerfile: multiCopy,

pkg/skaffold/docker/parse.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,16 @@ func extractCopyCommands(nodes []*parser.Node, onlyLastImage bool, cfg Config) (
214214
switch node.Value {
215215
case command.From:
216216
from := fromInstruction(node)
217+
if from.as != "" {
218+
// Stage names are case insensitive
219+
stages[strings.ToLower(from.as)] = true
220+
}
221+
217222
if from.image == "" {
218223
// some build args like artifact dependencies are not available until the first build sequence has completed.
219224
// skip check if there are unavailable images
220225
continue
221226
}
222-
if from.as != "" {
223-
// Stage names are case insensitive
224-
stages[strings.ToLower(from.as)] = true
225-
}
226227

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

327-
if from.image == "" {
328-
// some build args like artifact dependencies are not available until the first build sequence has completed.
329-
// skip check if there are unavailable images
330-
continue
331-
}
332-
333328
var onbuildNodes []*parser.Node
334329
if ons, found := onbuildNodesCache[strings.ToLower(from.image)]; found {
335330
onbuildNodes = ons
331+
} else if from.image == "" {
332+
// some build args like artifact dependencies are not available until the first build sequence has completed.
333+
// skip check if there are unavailable images
334+
onbuildNodes = []*parser.Node{}
336335
} else if ons, err := parseOnbuild(from.image, cfg); err == nil {
337336
onbuildNodes = ons
338337
} else if warnMsg, ok := sErrors.IsOldImageManifestProblem(err); ok && warnMsg != "" {

0 commit comments

Comments
 (0)