/
copy.go
67 lines (52 loc) · 2.51 KB
/
copy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package instruction
import (
"context"
"fmt"
"github.com/moby/buildkit/frontend/dockerfile/instructions"
"github.com/werf/werf/pkg/build/stage"
"github.com/werf/werf/pkg/config"
"github.com/werf/werf/pkg/container_backend"
backend_instruction "github.com/werf/werf/pkg/container_backend/instruction"
"github.com/werf/werf/pkg/dockerfile"
"github.com/werf/werf/pkg/util"
)
type Copy struct {
*Base[*instructions.CopyCommand, *backend_instruction.Copy]
}
func NewCopy(i *dockerfile.DockerfileStageInstruction[*instructions.CopyCommand], dependencies []*config.Dependency, hasPrevStage bool, opts *stage.BaseStageOptions) *Copy {
return &Copy{Base: NewBase(i, backend_instruction.NewCopy(i.Data), dependencies, hasPrevStage, opts)}
}
func (stg *Copy) ExpandDependencies(ctx context.Context, c stage.Conveyor, baseEnv map[string]string) error {
return stg.doExpandDependencies(ctx, c, baseEnv, stg)
}
func (stg *Copy) ExpandInstruction(c stage.Conveyor, env map[string]string) error {
if err := stg.Base.ExpandInstruction(c, env); err != nil {
return err
}
if stg.instruction.Data.From != "" {
if ds := stg.instruction.GetDependencyByStageRef(stg.instruction.Data.From); ds != nil {
depStageImageName := c.GetImageNameForLastImageStage(ds.WerfImageName())
stg.backendInstruction.From = depStageImageName
}
}
return nil
}
func (stg *Copy) GetDependencies(ctx context.Context, c stage.Conveyor, cb container_backend.ContainerBackend, prevImage, prevBuiltImage *stage.StageImage, buildContextArchive container_backend.BuildContextArchiver) (string, error) {
var args []string
args = append(args, "From", stg.instruction.Data.From)
args = append(args, append([]string{"Sources"}, stg.instruction.Data.Sources()...)...)
args = append(args, "Dest", stg.instruction.Data.Dest())
args = append(args, "Chown", stg.instruction.Data.Chown)
args = append(args, "Chmod", stg.instruction.Data.Chmod)
args = append(args, "ExpandedFrom", stg.backendInstruction.From)
if stg.UsesBuildContext() {
if srcChecksum, err := buildContextArchive.CalculateGlobsChecksum(ctx, stg.instruction.Data.Sources(), false); err != nil {
return "", fmt.Errorf("unable to calculate build context globs checksum: %w", err)
} else {
args = append(args, "SourcesChecksum", srcChecksum)
}
}
// TODO(ilya-lesikov): should checksum of files from other image be calculated if --from specified?
// TODO(staged-dockerfile): support --link option: https://docs.docker.com/engine/reference/builder/#copy---link
return util.Sha256Hash(args...), nil
}