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

Make sure working directory is respected when configured from matrix #1686

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 12 additions & 8 deletions pkg/runner/step_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ func (sr *stepRun) pre() common.Executor {

func (sr *stepRun) main() common.Executor {
sr.env = map[string]string{}

workingdirectory := sr.getWorkingDirectory(context.Background())
ChristopherHX marked this conversation as resolved.
Show resolved Hide resolved
return runStepExecutor(sr, stepStageMain, common.NewPipelineExecutor(
sr.setupShellCommandExecutor(),
func(ctx context.Context) error {
sr.getRunContext().ApplyExtraPath(ctx, &sr.env)
return sr.getRunContext().JobContainer.Exec(sr.cmd, sr.env, "", sr.Step.WorkingDirectory)(ctx)
return sr.getRunContext().JobContainer.Exec(sr.cmd, sr.env, "", workingdirectory)(ctx)
},
))
}
Expand Down Expand Up @@ -95,7 +95,6 @@ func getScriptName(rc *RunContext, step *model.Step) string {
func (sr *stepRun) setupShellCommand(ctx context.Context) (name, script string, err error) {
logger := common.Logger(ctx)
sr.setupShell(ctx)
sr.setupWorkingDirectory(ctx)

step := sr.Step

Expand Down Expand Up @@ -164,19 +163,24 @@ func (sr *stepRun) setupShell(ctx context.Context) {
}
}

func (sr *stepRun) setupWorkingDirectory(ctx context.Context) {
func (sr *stepRun) getWorkingDirectory(ctx context.Context) string {
rc := sr.RunContext
step := sr.Step
workingdirectory := ""

if step.WorkingDirectory == "" {
step.WorkingDirectory = rc.Run.Job().Defaults.Run.WorkingDirectory
workingdirectory = rc.Run.Job().Defaults.Run.WorkingDirectory
} else {
workingdirectory = step.WorkingDirectory
}

// jobs can receive context values, so we interpolate
step.WorkingDirectory = rc.NewExpressionEvaluator(ctx).Interpolate(ctx, step.WorkingDirectory)
workingdirectory = rc.NewExpressionEvaluator(ctx).Interpolate(ctx, workingdirectory)

// but top level keys in workflow file like `defaults` or `env` can't
if step.WorkingDirectory == "" {
step.WorkingDirectory = rc.Run.Workflow.Defaults.Run.WorkingDirectory
if workingdirectory == "" {
workingdirectory = rc.Run.Workflow.Defaults.Run.WorkingDirectory
}

return workingdirectory
}
10 changes: 10 additions & 0 deletions pkg/runner/testdata/workdir/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- run: '[[ "$(pwd)" == "/tmp" ]]'

workdir-from-matrix:
runs-on: ubuntu-latest
strategy:
max-parallel: 1
matrix:
work_dir: ["/tmp", "/root"]
steps:
- run: '[[ "$(pwd)" == "${{ matrix.work_dir }}" ]]'
working-directory: ${{ matrix.work_dir }}