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

@@ -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: