Skip to content

Commit

Permalink
Merge pull request #1085 from joshrwolf/debug-workdir
Browse files Browse the repository at this point in the history
open debug session in the specific workdir
  • Loading branch information
joshrwolf authored Mar 12, 2024
2 parents 3238a18 + d107a5d commit f9e6368
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions pkg/build/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,12 +344,9 @@ func (pctx *PipelineContext) evalRun(ctx context.Context, pb *PipelineBuild) err

sysPath := "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

workdir := "/home/build"
if pctx.Pipeline.WorkDir != "" {
workdir, err = util.MutateStringFromMap(pctx.Pipeline.With, pctx.Pipeline.WorkDir)
if err != nil {
return err
}
workdir, err := pctx.pipelineStepWorkDir()
if err != nil {
return err
}

fragment, err := util.MutateStringFromMap(pctx.Pipeline.With, pctx.Pipeline.Runs)
Expand Down Expand Up @@ -386,8 +383,13 @@ func (pctx *PipelineContext) maybeDebug(ctx context.Context, pb *PipelineBuild,
return runErr
}

workdir, err := pctx.pipelineStepWorkDir()
if err != nil {
return err
}

log.Errorf("Step failed: %v\n%s", runErr, strings.Join(cmd, " "))
log.Infof("Execing into pod %q to debug interactively.", pctx.WorkspaceConfig.PodID)
log.Info(fmt.Sprintf("Execing into pod %q to debug interactively.", pctx.WorkspaceConfig.PodID), "workdir", workdir)
log.Infof("Type 'exit 0' to continue the next pipeline step or 'exit 1' to abort.")

// If the context has already been cancelled, return before we mess with it.
Expand All @@ -398,7 +400,7 @@ func (pctx *PipelineContext) maybeDebug(ctx context.Context, pb *PipelineBuild,
// Don't cancel the context if we hit ctrl+C while debugging.
signal.Ignore(os.Interrupt)

if dbgErr := dbg.Debug(ctx, pctx.WorkspaceConfig, "/bin/sh"); dbgErr != nil {
if dbgErr := dbg.Debug(ctx, pctx.WorkspaceConfig, []string{"/bin/sh", "-c", fmt.Sprintf("cd %s && exec /bin/sh", workdir)}...); dbgErr != nil {
return fmt.Errorf("failed to debug: %w; original error: %w", dbgErr, runErr)
}

Expand Down Expand Up @@ -478,7 +480,6 @@ func (pctx *PipelineContext) Run(ctx context.Context, pb *PipelineBuild) (bool,
}

ran, err := spctx.Run(ctx, pb)

if err != nil {
return false, err
}
Expand Down Expand Up @@ -530,5 +531,14 @@ func (pctx *PipelineContext) ApplyNeeds(ctx context.Context, pb *PipelineBuild)
return nil
}

// pipelineStepWorkDir returns the workdir for the current pipeline step.
func (pctx *PipelineContext) pipelineStepWorkDir() (string, error) {
if pctx.Pipeline.WorkDir == "" {
return "/home/build", nil
}

return util.MutateStringFromMap(pctx.Pipeline.With, pctx.Pipeline.WorkDir)
}

//go:embed pipelines/*
var f embed.FS

0 comments on commit f9e6368

Please sign in to comment.