Add proper support for working-directory & fix command builder (#772)

* fix: align other Docker executors to print action

* fix: formatting

* fix: add proper workdir support

* fix: replace script filepath after slice creation

* fix: match substring so it works for pwsh

+ rename containerPath to scriptPath to reflect what value it contains
This commit is contained in:
Ryan
2021-08-10 19:40:20 +00:00
committed by GitHub
parent 77b3968913
commit bea32d5651
6 changed files with 76 additions and 36 deletions

View File

@@ -151,7 +151,7 @@ func (rc *RunContext) startJobContainer() common.Executor {
rc.JobContainer.Create(rc.Config.ContainerCapAdd, rc.Config.ContainerCapDrop),
rc.JobContainer.Start(false),
rc.JobContainer.UpdateFromEnv("/etc/environment", &rc.Env),
rc.JobContainer.Exec([]string{"mkdir", "-m", "0777", "-p", ActPath}, rc.Env, "root"),
rc.JobContainer.Exec([]string{"mkdir", "-m", "0777", "-p", ActPath}, rc.Env, "root", ""),
rc.JobContainer.CopyDir(copyToPath, rc.Config.Workdir+string(filepath.Separator)+".", rc.Config.UseGitIgnore).IfBool(copyWorkspace),
rc.JobContainer.Copy(ActPath+"/", &container.FileEntry{
Name: "workflow/event.json",
@@ -169,9 +169,10 @@ func (rc *RunContext) startJobContainer() common.Executor {
)(ctx)
}
}
func (rc *RunContext) execJobContainer(cmd []string, env map[string]string) common.Executor {
func (rc *RunContext) execJobContainer(cmd []string, env map[string]string, user, workdir string) common.Executor {
return func(ctx context.Context) error {
return rc.JobContainer.Exec(cmd, env, "")(ctx)
return rc.JobContainer.Exec(cmd, env, user, workdir)(ctx)
}
}

View File

@@ -37,7 +37,7 @@ type StepContext struct {
func (sc *StepContext) execJobContainer() common.Executor {
return func(ctx context.Context) error {
return sc.RunContext.execJobContainer(sc.Cmd, sc.Env)(ctx)
return sc.RunContext.execJobContainer(sc.Cmd, sc.Env, "", sc.Step.WorkingDirectory)(ctx)
}
}
@@ -195,12 +195,6 @@ func (sc *StepContext) setupShellCommand() common.Executor {
step.WorkingDirectory = rc.Run.Workflow.Defaults.Run.WorkingDirectory
}
step.WorkingDirectory = rc.ExprEval.Interpolate(step.WorkingDirectory)
if step.WorkingDirectory != "" {
_, err = script.WriteString(fmt.Sprintf("cd %s\n", step.WorkingDirectory))
if err != nil {
return err
}
}
run := rc.ExprEval.Interpolate(step.Run)
step.Shell = rc.ExprEval.Interpolate(step.Shell)
@@ -233,7 +227,7 @@ func (sc *StepContext) setupShellCommand() common.Executor {
run = runPrepend + "\n" + run + "\n" + runAppend
log.Debugf("Wrote command '%s' to '%s'", run, scriptName)
containerPath := fmt.Sprintf("%s/%s", rc.Config.ContainerWorkdir(), scriptName)
scriptPath := fmt.Sprintf("%s/%s", rc.Config.ContainerWorkdir(), scriptName)
if step.Shell == "" {
step.Shell = rc.Run.Job().Defaults.Run.Shell
@@ -242,13 +236,22 @@ func (sc *StepContext) setupShellCommand() common.Executor {
step.Shell = rc.Run.Workflow.Defaults.Run.Shell
}
scCmd := step.ShellCommand()
scResolvedCmd := strings.Replace(scCmd, "{0}", containerPath, 1)
var finalCMD []string
if step.Shell == "pwsh" || step.Shell == "powershell" {
sc.Cmd = strings.SplitN(scResolvedCmd, " ", 3)
finalCMD = strings.SplitN(scCmd, " ", 3)
} else {
sc.Cmd = strings.Fields(scResolvedCmd)
finalCMD = strings.Fields(scCmd)
}
for k, v := range finalCMD {
if strings.Contains(v, `{0}`) {
finalCMD[k] = strings.Replace(v, `{0}`, scriptPath, 1)
}
}
sc.Cmd = finalCMD
return rc.JobContainer.Copy(rc.Config.ContainerWorkdir(), &container.FileEntry{
Name: scriptName,
Mode: 0755,
@@ -307,6 +310,7 @@ func (sc *StepContext) newStepContainer(ctx context.Context, image string, cmd [
})
return stepContainer
}
func (sc *StepContext) runUsesContainer() common.Executor {
rc := sc.RunContext
step := sc.Step
@@ -485,7 +489,7 @@ func (sc *StepContext) runAction(actionDir string, actionPath string, localActio
}
containerArgs := []string{"node", path.Join(containerActionDir, action.Runs.Main)}
log.Debugf("executing remote job container: %s", containerArgs)
return rc.execJobContainer(containerArgs, sc.Env)(ctx)
return rc.execJobContainer(containerArgs, sc.Env, "", "")(ctx)
case model.ActionRunsUsingDocker:
return sc.execAsDocker(ctx, action, actionName, containerActionDir, actionLocation, rc, step, localAction)
case model.ActionRunsUsingComposite:

View File

@@ -8,6 +8,7 @@ jobs:
check:
runs-on: ubuntu-latest
steps:
- run: '[[ "$(pwd)" == "${GITHUB_WORKSPACE}" ]]'
- run: echo ${{ env.TEST }} | grep value
- run: env
- uses: docker://node:12-buster-slim

View File

@@ -0,0 +1,7 @@
---
jobs:
dir-with-spaces:
runs-on: ubuntu-latest
steps:
- run: echo "$PWD"
'on': push

View File

@@ -1,15 +1,24 @@
name: workdir
on: push
defaults:
run:
working-directory: /tmp
jobs:
workdir:
runs-on: ubuntu-latest
defaults:
run:
working-directory: /root
steps:
- run: mkdir -p "${GITHUB_WORKSPACE}/workdir"
- run: '[[ "$(pwd)" == "${GITHUB_WORKSPACE}/workdir" ]]'
working-directory: workdir
- run: '[[ "$(pwd)" == "/root" ]]'
noworkdir:
- run: mkdir -p "${GITHUB_WORKSPACE}/workdir"
- run: '[[ "$(pwd)" == "${GITHUB_WORKSPACE}/workdir" ]]'
working-directory: workdir
top-level-workdir:
runs-on: ubuntu-latest
steps:
- run: '[[ "$(pwd)" == "${GITHUB_WORKSPACE}" ]]'
- run: '[[ "$(pwd)" == "/tmp" ]]'