diff --git a/docs/md/melange_build.md b/docs/md/melange_build.md index 42f58bcdc..3967433b7 100644 --- a/docs/md/melange_build.md +++ b/docs/md/melange_build.md @@ -34,6 +34,7 @@ melange build [flags] --build-option strings build options to enable --cache-dir string directory used for cached inputs (default "./melange-cache/") --cache-source string directory or bucket used for preloading the cache + --cleanup when enabled, the temp dir used for the guest will be cleaned up after completion (default true) --cpu string default CPU resources to use for builds --create-build-log creates a package.log file containing a list of packages that were built by the command --debug enables debug logging of build pipelines diff --git a/docs/md/melange_test.md b/docs/md/melange_test.md index 2fec45b89..a450ea4c3 100644 --- a/docs/md/melange_test.md +++ b/docs/md/melange_test.md @@ -32,6 +32,7 @@ melange test [flags] --arch strings architectures to build for (e.g., x86_64,ppc64le,arm64) -- default is all, unless specified in config --cache-dir string directory used for cached inputs --cache-source string directory or bucket used for preloading the cache + --cleanup when enabled, the temp dir used for the guest will be cleaned up after completion (default true) --debug enables debug logging of test pipelines (sets -x for steps) --debug-runner when enabled, the builder pod will persist after the build succeeds or fails --env-file string file to use for preloaded environment variables diff --git a/pkg/build/build.go b/pkg/build/build.go index e6a38cbaa..131e24702 100644 --- a/pkg/build/build.go +++ b/pkg/build/build.go @@ -67,6 +67,7 @@ type Build struct { PipelineDirs []string SourceDir string GuestDir string + Cleanup bool SigningKey string SigningPassphrase string Namespace string @@ -703,6 +704,10 @@ func (b *Build) BuildPackage(ctx context.Context) error { return fmt.Errorf("unable to make guest directory: %w", err) } b.GuestDir = guestDir + + if b.Cleanup { + defer os.RemoveAll(guestDir) + } } log.Infof("evaluating pipelines for package requirements") diff --git a/pkg/build/options.go b/pkg/build/options.go index df1f929ed..e416b9f30 100644 --- a/pkg/build/options.go +++ b/pkg/build/options.go @@ -384,3 +384,11 @@ func WithIgnoreSignatures(ignore bool) Option { return nil } } + +// WithCleanup indicates whether to clean up the build environment after the build is complete. +func WithCleanup(c bool) Option { + return func(b *Build) error { + b.Cleanup = c + return nil + } +} diff --git a/pkg/build/test.go b/pkg/build/test.go index f3ad301b9..795d50e3a 100644 --- a/pkg/build/test.go +++ b/pkg/build/test.go @@ -51,6 +51,7 @@ type Test struct { PipelineDirs []string SourceDir string GuestDir string + Cleanup bool Arch apko_types.Architecture ExtraKeys []string ExtraRepos []string @@ -376,6 +377,10 @@ func (t *Test) TestPackage(ctx context.Context) error { return fmt.Errorf("unable to make guest directory: %w", err) } t.GuestDir = guestDir + + if t.Cleanup { + defer os.RemoveAll(guestDir) + } } imgRef := "" diff --git a/pkg/build/test_options.go b/pkg/build/test_options.go index 746c74dcb..5b743fb22 100644 --- a/pkg/build/test_options.go +++ b/pkg/build/test_options.go @@ -196,3 +196,11 @@ func WithTestAuth(domain, user, pass string) TestOption { return nil } } + +// If true, the test will clean up the test environment after the test is complete. +func WithTestCleanup(c bool) TestOption { + return func(t *Test) error { + t.Cleanup = c + return nil + } +} diff --git a/pkg/cli/build.go b/pkg/cli/build.go index 886cf84e9..fe0a39a8b 100644 --- a/pkg/cli/build.go +++ b/pkg/cli/build.go @@ -76,6 +76,7 @@ func buildCmd() *cobra.Command { var libc string var lintRequire, lintWarn []string var ignoreSignatures bool + var cleanup bool var traceFile string @@ -157,6 +158,7 @@ func buildCmd() *cobra.Command { build.WithTimeout(timeout), build.WithLibcFlavorOverride(libc), build.WithIgnoreSignatures(ignoreSignatures), + build.WithCleanup(cleanup), } if len(args) > 0 { @@ -224,6 +226,7 @@ func buildCmd() *cobra.Command { cmd.Flags().StringSliceVar(&lintRequire, "lint-require", linter.DefaultRequiredLinters(), "linters that must pass") cmd.Flags().StringSliceVar(&lintWarn, "lint-warn", linter.DefaultWarnLinters(), "linters that will generate warnings") cmd.Flags().BoolVar(&ignoreSignatures, "ignore-signatures", false, "ignore repository signature verification") + cmd.Flags().BoolVar(&cleanup, "cleanup", true, "when enabled, the temp dir used for the guest will be cleaned up after completion") _ = cmd.Flags().Bool("fail-on-lint-warning", false, "DEPRECATED: DO NOT USE") _ = cmd.Flags().MarkDeprecated("fail-on-lint-warning", "use --lint-require and --lint-warn instead") diff --git a/pkg/cli/test.go b/pkg/cli/test.go index 87e4b5a7f..8679bd25b 100644 --- a/pkg/cli/test.go +++ b/pkg/cli/test.go @@ -48,6 +48,7 @@ func test() *cobra.Command { var interactive bool var runner string var extraTestPackages []string + var cleanup bool cmd := &cobra.Command{ Use: "test", @@ -78,6 +79,7 @@ func test() *cobra.Command { build.WithTestDebug(debug), build.WithTestDebugRunner(debugRunner), build.WithTestInteractive(interactive), + build.WithTestCleanup(cleanup), } if len(args) > 0 { @@ -129,6 +131,7 @@ func test() *cobra.Command { cmd.Flags().BoolVarP(&interactive, "interactive", "i", false, "when enabled, attaches stdin with a tty to the pod on failure") cmd.Flags().StringSliceVarP(&extraRepos, "repository-append", "r", []string{}, "path to extra repositories to include in the build environment") cmd.Flags().StringSliceVar(&extraTestPackages, "test-package-append", []string{}, "extra packages to install for each of the test environments") + cmd.Flags().BoolVar(&cleanup, "cleanup", true, "when enabled, the temp dir used for the guest will be cleaned up after completion") return cmd }