diff --git a/pkg/build/image/build_context_archive.go b/pkg/build/image/build_context_archive.go index df553908e2..f207cc1ff5 100644 --- a/pkg/build/image/build_context_archive.go +++ b/pkg/build/image/build_context_archive.go @@ -8,6 +8,8 @@ import ( "path/filepath" "sort" + "github.com/containers/buildah/copier" + "github.com/werf/logboek" "github.com/werf/werf/pkg/container_backend" "github.com/werf/werf/pkg/context_manager" @@ -111,7 +113,40 @@ func (a *BuildContextArchive) CleanupExtractedDir(ctx context.Context) { } } -func (a *BuildContextArchive) CalculatePathsChecksum(ctx context.Context, paths []string) (string, error) { +func (a *BuildContextArchive) CalculateGlobsChecksum(ctx context.Context, globs []string, checkForArchives bool) (string, error) { + contextDir, err := a.ExtractOrGetExtractedDir(ctx) + if err != nil { + return "", fmt.Errorf("unable to get build context dir: %w", err) + } + + globStats, err := copier.Stat(contextDir, contextDir, copier.StatOptions{CheckForArchives: checkForArchives}, globs) + if err != nil { + return "", fmt.Errorf("unable to stat globs: %w", err) + } + if len(globStats) == 0 { + return "", fmt.Errorf("no glob matches for globs: %v", globs) + } + + var matches []string + for _, globStat := range globStats { + if globStat.Error != "" { + return "", fmt.Errorf("unable to stat glob %q: %s", globStat.Glob, globStat.Error) + } + + for _, match := range globStat.Globbed { + matches = append(matches, match) + } + } + + pathsChecksum, err := a.calculatePathsChecksum(ctx, matches) + if err != nil { + return "", fmt.Errorf("unable to calculate build context paths checksum: %w", err) + } + + return pathsChecksum, nil +} + +func (a *BuildContextArchive) calculatePathsChecksum(ctx context.Context, paths []string) (string, error) { sort.Strings(paths) paths = util.UniqStrings(paths) diff --git a/pkg/build/stage/instruction/add.go b/pkg/build/stage/instruction/add.go index 3226bb4d11..48e5f72e0e 100644 --- a/pkg/build/stage/instruction/add.go +++ b/pkg/build/stage/instruction/add.go @@ -5,8 +5,6 @@ import ( "fmt" "strings" - "github.com/containers/buildah/copier" - "github.com/werf/werf/pkg/build/stage" "github.com/werf/werf/pkg/config" "github.com/werf/werf/pkg/container_backend" @@ -45,7 +43,7 @@ func (stg *Add) GetDependencies(ctx context.Context, c stage.Conveyor, cb contai } if len(fileGlobSrc) > 0 { - if srcChecksum, err := calculateBuildContextGlobsChecksum(ctx, fileGlobSrc, true, buildContextArchive); err != nil { + if srcChecksum, err := buildContextArchive.CalculateGlobsChecksum(ctx, fileGlobSrc, true); err != nil { return "", fmt.Errorf("unable to calculate build context globs checksum: %w", err) } else { args = append(args, "SrcChecksum", srcChecksum) @@ -59,36 +57,3 @@ func (stg *Add) GetDependencies(ctx context.Context, c stage.Conveyor, cb contai return util.Sha256Hash(args...), nil } - -func calculateBuildContextGlobsChecksum(ctx context.Context, fileGlobs []string, checkForArchives bool, buildContextArchive container_backend.BuildContextArchiver) (string, error) { - contextDir, err := buildContextArchive.ExtractOrGetExtractedDir(ctx) - if err != nil { - return "", fmt.Errorf("unable to get build context dir: %w", err) - } - - globStats, err := copier.Stat(contextDir, contextDir, copier.StatOptions{CheckForArchives: checkForArchives}, fileGlobs) - if err != nil { - return "", fmt.Errorf("unable to stat globs: %w", err) - } - if len(globStats) == 0 { - return "", fmt.Errorf("no glob matches for globs: %v", fileGlobs) - } - - var matches []string - for _, globStat := range globStats { - if globStat.Error != "" { - return "", fmt.Errorf("unable to stat glob %q: %s", globStat.Glob, globStat.Error) - } - - for _, match := range globStat.Globbed { - matches = append(matches, match) - } - } - - pathsChecksum, err := buildContextArchive.CalculatePathsChecksum(ctx, matches) - if err != nil { - return "", fmt.Errorf("unable to calculate build context paths checksum: %w", err) - } - - return pathsChecksum, nil -} diff --git a/pkg/build/stage/instruction/copy.go b/pkg/build/stage/instruction/copy.go index 8965d6b763..29edd03c36 100644 --- a/pkg/build/stage/instruction/copy.go +++ b/pkg/build/stage/instruction/copy.go @@ -47,7 +47,7 @@ func (stg *Copy) GetDependencies(ctx context.Context, c stage.Conveyor, cb conta args = append(args, "ExpandedFrom", stg.backendInstruction.From) if stg.UsesBuildContext() { - if srcChecksum, err := calculateBuildContextGlobsChecksum(ctx, stg.instruction.Data.Src, false, buildContextArchive); err != nil { + if srcChecksum, err := buildContextArchive.CalculateGlobsChecksum(ctx, stg.instruction.Data.Src, false); err != nil { return "", fmt.Errorf("unable to calculate build context globs checksum: %w", err) } else { args = append(args, "SrcChecksum", srcChecksum) diff --git a/pkg/container_backend/archive_interface.go b/pkg/container_backend/archive_interface.go index 1bbc2af7d9..a357a3a4fc 100644 --- a/pkg/container_backend/archive_interface.go +++ b/pkg/container_backend/archive_interface.go @@ -6,7 +6,7 @@ type BuildContextArchiver interface { Create(ctx context.Context, opts BuildContextArchiveCreateOptions) error Path() string ExtractOrGetExtractedDir(ctx context.Context) (string, error) - CalculatePathsChecksum(ctx context.Context, paths []string) (string, error) + CalculateGlobsChecksum(ctx context.Context, globs []string, checkForArchive bool) (string, error) CleanupExtractedDir(ctx context.Context) }