Skip to content

Commit

Permalink
Merge pull request #839 from antechrestos/fix/upcase_for_from_stage_name
Browse files Browse the repository at this point in the history
Fix failure when using capital letters in image alias in 'FROM ... AS…' instruction
  • Loading branch information
tejal29 authored Dec 9, 2019
2 parents 6326515 + d22a760 commit acb5b9f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
3 changes: 2 additions & 1 deletion pkg/dockerfile/dockerfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ func targetStage(stages []instructions.Stage, target string) (int, error) {

// resolveStages resolves any calls to previous stages with names to indices
// Ex. --from=second_stage should be --from=1 for easier processing later on
// As third party library lowers stage name in FROM instruction, this function resolves stage case insensitively.
func resolveStages(stages []instructions.Stage) {
nameToIndex := make(map[string]string)
for i, stage := range stages {
Expand All @@ -214,7 +215,7 @@ func resolveStages(stages []instructions.Stage) {
switch c := cmd.(type) {
case *instructions.CopyCommand:
if c.From != "" {
if val, ok := nameToIndex[c.From]; ok {
if val, ok := nameToIndex[strings.ToLower(c.From)]; ok {
c.From = val
}

Expand Down
17 changes: 13 additions & 4 deletions pkg/dockerfile/dockerfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,14 @@ func Test_resolveStages(t *testing.T) {
FROM scratch AS second
COPY --from=0 /hi /hi2
FROM scratch
FROM scratch AS tHiRd
COPY --from=second /hi2 /hi3
COPY --from=1 /hi2 /hi3
FROM scratch
COPY --from=thIrD /hi3 /hi4
COPY --from=third /hi3 /hi4
COPY --from=2 /hi3 /hi4
`
stages, _, err := Parse([]byte(dockerfile))
if err != nil {
Expand All @@ -209,11 +215,14 @@ func Test_resolveStages(t *testing.T) {
if index == 0 {
continue
}
copyCmd := stage.Commands[0].(*instructions.CopyCommand)
expectedStage := strconv.Itoa(index - 1)
if copyCmd.From != expectedStage {
t.Fatalf("unexpected copy command: %s resolved to stage %s, expected %s", copyCmd.String(), copyCmd.From, expectedStage)
for _, command := range stage.Commands {
copyCmd := command.(*instructions.CopyCommand)
if copyCmd.From != expectedStage {
t.Fatalf("unexpected copy command: %s resolved to stage %s, expected %s", copyCmd.String(), copyCmd.From, expectedStage)
}
}

}
}

Expand Down

0 comments on commit acb5b9f

Please sign in to comment.