Skip to content

Commit

Permalink
Merge pull request #552 from joshrwolf/default-remove-builder
Browse files Browse the repository at this point in the history
Default remove builder
  • Loading branch information
joshrwolf authored Jul 17, 2023
2 parents f0aae91 + e16fb77 commit 2dde56c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/md/melange_build.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ melange build [flags]
--continue-label string continue build execution at the specified label
--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
--debug-runner when enabled, the builder pod will persist after the build suceeds or fails
--dependency-log string log dependencies to a specified file
--empty-workspace whether the build workspace should be empty
--env-file string file to use for preloaded environment variables
Expand Down
21 changes: 16 additions & 5 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ type Build struct {
imgRef string
containerConfig *container.Config
Debug bool
DebugRunner bool
LogPolicy []string

EnabledBuildOptions []string
Expand Down Expand Up @@ -821,6 +822,14 @@ func WithDebug(debug bool) Option {
}
}

// WithDebugRunner indicates whether the runner should leave the build environment up on failures
func WithDebugRunner(debug bool) Option {
return func(b *Build) error {
b.DebugRunner = debug
return nil
}
}

// WithLogPolicy sets the logging policy to use during builds.
func WithLogPolicy(policy []string) Option {
return func(b *Build) error {
Expand Down Expand Up @@ -1645,6 +1654,13 @@ func (b *Build) BuildPackage(ctx context.Context) error {
if err := b.Runner.StartPod(ctx, cfg); err != nil {
return fmt.Errorf("unable to start pod: %w", err)
}
if !b.DebugRunner {
defer func() {
if err := b.Runner.TerminatePod(ctx, cfg); err != nil {
b.Logger.Warnf("unable to terminate pod: %s", err)
}
}()
}

// run the main pipeline
b.Logger.Printf("running the main pipeline")
Expand Down Expand Up @@ -1757,11 +1773,6 @@ func (b *Build) BuildPackage(ctx context.Context) error {
}

if !b.IsBuildLess() {
// terminate pod
if err := b.Runner.TerminatePod(ctx, cfg); err != nil {
b.Logger.Printf("WARNING: unable to terminate pod: %s", err)
}

// clean build guest container
if err := os.RemoveAll(b.GuestDir); err != nil {
b.Logger.Printf("WARNING: unable to clean guest container: %s", err)
Expand Down
3 changes: 3 additions & 0 deletions pkg/cli/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func Build() *cobra.Command {
var logPolicy []string
var createBuildLog bool
var debug bool
var debugRunner bool
var runner string

cmd := &cobra.Command{
Expand Down Expand Up @@ -94,6 +95,7 @@ func Build() *cobra.Command {
build.WithEnabledBuildOptions(buildOption),
build.WithCreateBuildLog(createBuildLog),
build.WithDebug(debug),
build.WithDebugRunner(debugRunner),
build.WithLogPolicy(logPolicy),
build.WithRunner(runner),
}
Expand Down Expand Up @@ -142,6 +144,7 @@ func Build() *cobra.Command {
cmd.Flags().StringSliceVarP(&extraRepos, "repository-append", "r", []string{}, "path to extra repositories to include in the build environment")
cmd.Flags().BoolVar(&createBuildLog, "create-build-log", false, "creates a package.log file containing a list of packages that were built by the command")
cmd.Flags().BoolVar(&debug, "debug", false, "enables debug logging of build pipelines")
cmd.Flags().BoolVar(&debugRunner, "debug-runner", false, "when enabled, the builder pod will persist after the build suceeds or fails")

return cmd
}
Expand Down

0 comments on commit 2dde56c

Please sign in to comment.