Skip to content

Commit f9fc48e

Browse files
Merge pull request #293 from nalind/process-targets
NewStages(): resolve stage names using ARGs
2 parents 635c868 + cfce9e9 commit f9fc48e

3 files changed

Lines changed: 64 additions & 5 deletions

File tree

builder.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,25 @@ func NewStages(node *parser.Node, b *Builder) (Stages, error) {
300300
if len(name) == 0 {
301301
name = strconv.Itoa(i)
302302
}
303+
filteredUserArgs := make(map[string]string)
304+
for k, v := range b.UserArgs {
305+
for _, a := range b.GlobalAllowedArgs {
306+
if a == k {
307+
filteredUserArgs[k] = v
308+
}
309+
}
310+
}
311+
userArgs := envMapAsSlice(filteredUserArgs)
312+
userArgs = mergeEnv(envMapAsSlice(b.BuiltinArgDefaults), userArgs)
313+
userArgs = mergeEnv(envMapAsSlice(builtinArgDefaults), userArgs)
314+
userArgs = mergeEnv(envMapAsSlice(b.HeadingArgs), userArgs)
315+
processedName, err := ProcessWord(name, userArgs)
316+
if err != nil {
317+
return nil, err
318+
}
303319
stages = append(stages, Stage{
304320
Position: i,
305-
Name: name,
321+
Name: processedName,
306322
Builder: b.builderForStage(headingArgs),
307323
Node: root,
308324
})

builder_test.go

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ func TestByTarget(t *testing.T) {
8181
if err != nil {
8282
t.Fatal(err)
8383
}
84-
if len(stages) != 3 {
85-
t.Fatalf("expected 3 stages, got %d", len(stages))
84+
if len(stages) != 4 {
85+
t.Fatalf("expected 4 stages, got %d", len(stages))
8686
}
8787
t.Logf("stages: %#v", stages)
8888

@@ -123,6 +123,25 @@ func TestByTarget(t *testing.T) {
123123
}
124124
t.Logf("stages4: %#v", stages4)
125125
assert.Equal(t, stages4, stages2)
126+
127+
stages5, found := stages.ByTarget("mytarget3")
128+
if !found {
129+
t.Fatal("Fifth target not found")
130+
}
131+
if len(stages5) != 1 {
132+
t.Fatalf("expected 1 stages, got %d", len(stages5))
133+
}
134+
t.Logf("stages5: %#v", stages5)
135+
136+
stages6, found := stages.ByTarget("3")
137+
if !found {
138+
t.Fatal("Sixth target not found")
139+
}
140+
if len(stages6) != 1 {
141+
t.Fatalf("expected 1 stages, got %d", len(stages4))
142+
}
143+
t.Logf("stages6: %#v", stages6)
144+
assert.Equal(t, stages6, stages5)
126145
}
127146

128147
func TestThroughTarget(t *testing.T) {
@@ -134,8 +153,8 @@ func TestThroughTarget(t *testing.T) {
134153
if err != nil {
135154
t.Fatal(err)
136155
}
137-
if len(stages) != 3 {
138-
t.Fatalf("expected 3 stages, got %d", len(stages))
156+
if len(stages) != 4 {
157+
t.Fatalf("expected 4 stages, got %d", len(stages))
139158
}
140159
t.Logf("stages: %#v", stages)
141160

@@ -176,6 +195,25 @@ func TestThroughTarget(t *testing.T) {
176195
}
177196
t.Logf("stages4: %#v", stages4)
178197
assert.Equal(t, stages4, stages2)
198+
199+
stages5, found := stages.ThroughTarget("mytarget3")
200+
if !found {
201+
t.Fatal("Fifth target not found")
202+
}
203+
if len(stages5) != 4 {
204+
t.Fatalf("expected 4 stages, got %d", len(stages5))
205+
}
206+
t.Logf("stages5: %#v", stages5)
207+
208+
stages6, found := stages.ThroughTarget("3")
209+
if !found {
210+
t.Fatal("Sixth target not found")
211+
}
212+
if len(stages6) != 4 {
213+
t.Fatalf("expected 4 stages, got %d", len(stages4))
214+
}
215+
t.Logf("stages6: %#v", stages6)
216+
assert.Equal(t, stages6, stages5)
179217
}
180218

181219
func TestMultiStageParse(t *testing.T) {

dockerclient/testdata/Dockerfile.target

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
ARG TARGET3=mytarget3
2+
13
FROM ubuntu:latest
24
RUN touch /1
35

@@ -6,3 +8,6 @@ RUN touch /2
68

79
FROM busybox:latest AS mytarget2
810
RUN touch /3
11+
12+
FROM busybox:latest AS ${TARGET3}
13+
RUN touch /4

0 commit comments

Comments
 (0)