Skip to content

Commit

Permalink
fix(build, buildah, dockerfile): fix multi-stage does not work properly
Browse files Browse the repository at this point in the history
Tagging did not account for the base Dockerfile stage, resulting in rebuilds not being triggered.

For werf.StagedDockerfileV2, the new logic should be the default. However, to avoid breaking tag reproducibility, this logic is currently enabled only for the multi-stage case. Eventually, this behavior should be the default for all versions without the extra if condition.

Signed-off-by: Alexey Igrychev <alexey.igrychev@flant.com>
  • Loading branch information
alexey-igrychev committed Jun 7, 2024
1 parent fbca962 commit 02da623
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
12 changes: 7 additions & 5 deletions pkg/build/build_phase.go
Original file line number Diff line number Diff line change
Expand Up @@ -916,9 +916,12 @@ func (phase *BuildPhase) calculateStage(ctx context.Context, img *image.Image, s
opts.CacheVersionParts = nil
opts.TargetPlatform = img.TargetPlatform

if werf.GetStagedDockerfileVersion() == werf.StagedDockerfileV1 {
if img.IsDockerfileImage && img.DockerfileImageConfig.Staged {
if !stg.HasPrevStage() {
if img.IsDockerfileImage && img.DockerfileImageConfig.Staged {
if !stg.HasPrevStage() {
// FIXME: For werf.StagedDockerfileV2, this logic should also be the default.
// Currently, to avoid breaking tag reproducibility, this logic is only enabled for multi-stage cases.
// Eventually, this behavior should be default for all versions without the extra if condition.
if img.IsBasedOnStage() || werf.GetStagedDockerfileVersion() == werf.StagedDockerfileV1 {
opts.BaseImage = img.GetBaseImageReference()
}
}
Expand Down Expand Up @@ -1231,7 +1234,7 @@ func calculateDigest(ctx context.Context, stageName, stageDependencies string, p
var checksumArgs []string
var checksumArgsNames []string

// linux/amd64 not affects digest for compatibility with currently built stages
// TODO: linux/amd64 not affects digest for compatibility with currently built stages.
if opts.TargetPlatform != "" && opts.TargetPlatform != "linux/amd64" {
checksumArgs = append(checksumArgs, opts.TargetPlatform)
checksumArgsNames = append(checksumArgsNames, "TargetPlatform")
Expand Down Expand Up @@ -1265,7 +1268,6 @@ func calculateDigest(ctx context.Context, stageName, stageDependencies string, p
}
}

// TODO(staged-dockerfile): this is legacy digest part used for StagedDockerfileV1
if opts.BaseImage != "" {
checksumArgs = append(checksumArgs, opts.BaseImage)
checksumArgsNames = append(checksumArgsNames, "BaseImage")
Expand Down
4 changes: 4 additions & 0 deletions pkg/build/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ func ImageLogTagStyle(isArtifact bool) color.Style {
return logging.ImageDefaultStyle(isArtifact)
}

func (i *Image) IsBasedOnStage() bool {
return i.baseImageType == StageAsBaseImage
}

func (i *Image) IsFinal() bool {
if i.IsArtifact {
return false
Expand Down

0 comments on commit 02da623

Please sign in to comment.