Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(dockerfile): support RUN with --mount from another stage
When the following instruction used:

    RUN --mount=type=bind,from=build,source=/usr/local/test_project/dist,target=/usr/test_project/dist \
    cp -v /usr/test_project/dist/prog.py /usr/local/bin/prog

— change image digest when `from=build` stage digest has changed.

Signed-off-by: Timofey Kirillov <timofey.kirillov@flant.com>
  • Loading branch information
distorhead committed Jun 1, 2022
1 parent a655a35 commit ebd544a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
22 changes: 17 additions & 5 deletions pkg/build/dockerfile_helpers/dockerfile_helpers.go
Expand Up @@ -18,11 +18,23 @@ func ResolveDockerStagesFromValue(stages []instructions.Stage) {
}

for _, cmd := range s.Commands {
copyCmd, ok := cmd.(*instructions.CopyCommand)
if ok && copyCmd.From != "" {
from := strings.ToLower(copyCmd.From)
if val, ok := nameToIndex[from]; ok {
copyCmd.From = val
switch typedCmd := cmd.(type) {
case *instructions.CopyCommand:
if typedCmd.From != "" {
from := strings.ToLower(typedCmd.From)
if val, ok := nameToIndex[from]; ok {
typedCmd.From = val
}
}

case *instructions.RunCommand:
for _, mount := range instructions.GetMounts(typedCmd) {
if mount.From != "" {
from := strings.ToLower(mount.From)
if val, ok := nameToIndex[from]; ok {
mount.From = val
}
}
}
}
}
Expand Down
14 changes: 11 additions & 3 deletions pkg/build/stage/dockerfile.go
Expand Up @@ -474,12 +474,20 @@ func (s *DockerfileStage) GetDependencies(ctx context.Context, c Conveyor, _ con
}

for _, cmd := range stage.Commands {
copyCmd, ok := cmd.(*instructions.CopyCommand)
if ok && copyCmd.From != "" {
relatedStageIndex, err := strconv.Atoi(copyCmd.From)
switch typedCmd := cmd.(type) {
case *instructions.CopyCommand:
relatedStageIndex, err := strconv.Atoi(typedCmd.From)
if err == nil && relatedStageIndex < len(stagesDependencies) {
stagesDependencies[ind] = append(stagesDependencies[ind], stagesDependencies[relatedStageIndex]...)
}

case *instructions.RunCommand:
for _, mount := range instructions.GetMounts(typedCmd) {
relatedStageIndex, err := strconv.Atoi(mount.From)
if err == nil && relatedStageIndex < len(stagesDependencies) {
stagesDependencies[ind] = append(stagesDependencies[ind], stagesDependencies[relatedStageIndex]...)
}
}
}
}
}
Expand Down

0 comments on commit ebd544a

Please sign in to comment.