Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix local artifact output locations when BUILDKIT_MULTI_PLATFORM=1 #458

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
#
# Generated on 2024-11-05T11:16:22Z by kres 5fa0b38-dirty.
# Generated on 2024-11-06T12:40:22Z by kres 1becf29d-dirty.

# common variables

Expand Down Expand Up @@ -146,6 +146,15 @@ target-%: ## Builds the specified target defined in the Dockerfile. The build r

local-%: ## Builds the specified target defined in the Dockerfile using the local output type. The build result will be output to the specified local destination.
@$(MAKE) target-$* TARGET_ARGS="--output=type=local,dest=$(DEST) $(TARGET_ARGS)"
@PLATFORM=$(PLATFORM) ARTIFACTS=$(ARTIFACTS) bash -c '\
for platform in $$(tr "," "\n" <<< "$$PLATFORM"); do \
echo $$platform; \
directory="$${platform//\//_}"; \
if [[ -d "$$ARTIFACTS/$$directory" ]]; then \
mv "$$ARTIFACTS/$$directory/"* $$ARTIFACTS; \
rmdir "$$ARTIFACTS/$$directory/"; \
fi; \
done'

generate: ## Generate .proto definitions.
@$(MAKE) local-$@ DEST=./
Expand Down
21 changes: 20 additions & 1 deletion internal/project/common/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,25 @@ import (
"github.com/siderolabs/kres/internal/project/meta"
)

// FixLocalArtifactLocationsScript moves the local build artifacts from the <os>_<arch> subdirectories to the build output root directory.
//
// This is to revert the behavior of buildkit on multi-platform builds.
//
// As we force buildkit to always do multi-platform builds (via `BUILDKIT_MULTI_PLATFORM=1`), we need this fix to restore old output behavior.
//
// This script is appended to the local output build targets.
const FixLocalArtifactLocationsScript = `
@PLATFORM=$(PLATFORM) ARTIFACTS=$(ARTIFACTS) bash -c '\
for platform in $$(tr "," "\n" <<< "$$PLATFORM"); do \
echo $$platform; \
directory="$${platform//\//_}"; \
if [[ -d "$$ARTIFACTS/$$directory" ]]; then \
mv "$$ARTIFACTS/$$directory/"* $$ARTIFACTS; \
rmdir "$$ARTIFACTS/$$directory/"; \
fi; \
done'
`

// Docker provides build infrastructure via docker buildx.
type Docker struct { //nolint:govet
dag.BaseNode
Expand Down Expand Up @@ -147,7 +166,7 @@ func (docker *Docker) CompileMakefile(output *makefile.Output) error {

output.Target("local-%").
Description("Builds the specified target defined in the Dockerfile using the local output type. The build result will be output to the specified local destination.").
Script(`@$(MAKE) target-$* TARGET_ARGS="--output=type=local,dest=$(DEST) $(TARGET_ARGS)"`)
Script(`@$(MAKE) target-$* TARGET_ARGS="--output=type=local,dest=$(DEST) $(TARGET_ARGS)"` + FixLocalArtifactLocationsScript)

return nil
}
3 changes: 2 additions & 1 deletion internal/project/pkgfile/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/siderolabs/kres/internal/output/dockerignore"
"github.com/siderolabs/kres/internal/output/ghworkflow"
"github.com/siderolabs/kres/internal/output/makefile"
"github.com/siderolabs/kres/internal/project/common"
"github.com/siderolabs/kres/internal/project/meta"
)

Expand Down Expand Up @@ -114,7 +115,7 @@ func (pkgfile *Build) CompileMakefile(output *makefile.Output) error {

output.Target("local-%").
Description("Builds the specified target defined in the Pkgfile using the local output type. The build result will be output to the specified local destination.").
Script(`@$(MAKE) target-$* TARGET_ARGS="--output=type=local,dest=$(DEST) $(TARGET_ARGS)"`)
Script(`@$(MAKE) target-$* TARGET_ARGS="--output=type=local,dest=$(DEST) $(TARGET_ARGS)"` + common.FixLocalArtifactLocationsScript)

output.Target("docker-%").
Description("Builds the specified target defined in the Pkgfile using the docker output type. The build result will be loaded into Docker.").
Expand Down