Refactor local, composite actions and run steps (#712)

Skips docker cp for local actions and use their correct path
Defines GITHUB_ACTION_PATH also for nodejs actions
Evaluate Env of composite action
Evaluate Run of composite action correctly
Evaluate Shell of run step
Evaluate WorkingDirectory of run step
Changed tests for behavior change

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
ChristopherHX 2021-06-10 17:28:23 +02:00 committed by GitHub
parent 003c995b36
commit e5d4886787
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 24 deletions

View file

@ -173,6 +173,7 @@ func (sc *StepContext) setupShellCommand() common.Executor {
if step.WorkingDirectory == "" { if step.WorkingDirectory == "" {
step.WorkingDirectory = rc.Run.Workflow.Defaults.Run.WorkingDirectory step.WorkingDirectory = rc.Run.Workflow.Defaults.Run.WorkingDirectory
} }
step.WorkingDirectory = rc.ExprEval.Interpolate(step.WorkingDirectory)
if step.WorkingDirectory != "" { if step.WorkingDirectory != "" {
_, err = script.WriteString(fmt.Sprintf("cd %s\n", step.WorkingDirectory)) _, err = script.WriteString(fmt.Sprintf("cd %s\n", step.WorkingDirectory))
if err != nil { if err != nil {
@ -181,6 +182,7 @@ func (sc *StepContext) setupShellCommand() common.Executor {
} }
run := rc.ExprEval.Interpolate(step.Run) run := rc.ExprEval.Interpolate(step.Run)
step.Shell = rc.ExprEval.Interpolate(step.Shell)
if _, err = script.WriteString(run); err != nil { if _, err = script.WriteString(run); err != nil {
return err return err
@ -381,15 +383,13 @@ func getOsSafeRelativePath(s, prefix string) string {
func (sc *StepContext) getContainerActionPaths(step *model.Step, actionDir string, rc *RunContext) (string, string) { func (sc *StepContext) getContainerActionPaths(step *model.Step, actionDir string, rc *RunContext) (string, string) {
actionName := "" actionName := ""
containerActionDir := "." containerActionDir := "."
if !rc.Config.BindWorkdir && step.Type() != model.StepTypeUsesActionRemote { if step.Type() != model.StepTypeUsesActionRemote {
actionName = getOsSafeRelativePath(actionDir, rc.Config.Workdir) actionName = getOsSafeRelativePath(actionDir, rc.Config.Workdir)
containerActionDir = ActPath + "/actions/" + actionName containerActionDir = rc.Config.ContainerWorkdir() + "/" + actionName
actionName = "./" + actionName
} else if step.Type() == model.StepTypeUsesActionRemote { } else if step.Type() == model.StepTypeUsesActionRemote {
actionName = getOsSafeRelativePath(actionDir, rc.ActionCacheDir()) actionName = getOsSafeRelativePath(actionDir, rc.ActionCacheDir())
containerActionDir = ActPath + "/actions/" + actionName containerActionDir = ActPath + "/actions/" + actionName
} else if step.Type() == model.StepTypeUsesActionLocal {
actionName = getOsSafeRelativePath(actionDir, rc.Config.Workdir)
containerActionDir = ActPath + "/actions/" + actionName
} }
if actionName == "" { if actionName == "" {
@ -422,12 +422,10 @@ func (sc *StepContext) runAction(actionDir string, actionPath string) common.Exe
log.Debugf("type=%v actionDir=%s actionPath=%s Workdir=%s ActionCacheDir=%s actionName=%s containerActionDir=%s", step.Type(), actionDir, actionPath, rc.Config.Workdir, rc.ActionCacheDir(), actionName, containerActionDir) log.Debugf("type=%v actionDir=%s actionPath=%s Workdir=%s ActionCacheDir=%s actionName=%s containerActionDir=%s", step.Type(), actionDir, actionPath, rc.Config.Workdir, rc.ActionCacheDir(), actionName, containerActionDir)
maybeCopyToActionDir := func() error { maybeCopyToActionDir := func() error {
sc.Env["GITHUB_ACTION_PATH"] = containerActionDir
if step.Type() != model.StepTypeUsesActionRemote { if step.Type() != model.StepTypeUsesActionRemote {
// If the workdir is bound to our repository then we don't need to copy the file
if rc.Config.BindWorkdir {
return nil return nil
} }
}
err := removeGitIgnore(actionDir) err := removeGitIgnore(actionDir)
if err != nil { if err != nil {
return err return err
@ -570,28 +568,29 @@ func (sc *StepContext) execAsComposite(ctx context.Context, step *model.Step, _
} }
} }
if stepClone.Env == nil {
stepClone.Env = make(map[string]string)
}
actionPath := filepath.Join(containerActionDir, actionName)
env := stepClone.Environment() env := stepClone.Environment()
env["GITHUB_ACTION_PATH"] = actionPath
stepClone.Run = strings.ReplaceAll(stepClone.Run, "${{ github.action_path }}", actionPath)
stepContext := StepContext{ stepContext := StepContext{
RunContext: rcClone, RunContext: rcClone,
Step: &stepClone, Step: step,
Env: mergeMaps(sc.Env, env), Env: mergeMaps(sc.Env, env),
} }
// Interpolate the outer inputs into the composite step with items // Required to set github.action_path
exprEval := sc.NewExpressionEvaluator() if rcClone.Config.Env == nil {
for k, v := range stepContext.Step.With { // Workaround to get test working
if strings.Contains(v, "inputs") { rcClone.Config.Env = make(map[string]string)
stepContext.Step.With[k] = exprEval.Interpolate(v)
}
} }
rcClone.Config.Env["GITHUB_ACTION_PATH"] = sc.Env["GITHUB_ACTION_PATH"]
ev := stepContext.NewExpressionEvaluator()
// Required to interpolate inputs and github.action_path into the env map
stepContext.interpolateEnv(ev)
// Required to interpolate inputs, env and github.action_path into run steps
ev = stepContext.NewExpressionEvaluator()
stepClone.Run = ev.Interpolate(stepClone.Run)
stepClone.Shell = ev.Interpolate(stepClone.Shell)
stepClone.WorkingDirectory = ev.Interpolate(stepClone.WorkingDirectory)
stepContext.Step = &stepClone
executors = append(executors, stepContext.Executor()) executors = append(executors, stepContext.Executor())
} }

View file

@ -5,4 +5,5 @@ jobs:
test: test:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v2
- uses: ./actions/docker-url - uses: ./actions/docker-url

View file

@ -5,6 +5,7 @@ jobs:
test: test:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v2
- uses: ./actions/docker-local - uses: ./actions/docker-local
with: with:
who-to-greet: 'Mona the Octocat' who-to-greet: 'Mona the Octocat'

View file

@ -5,6 +5,7 @@ jobs:
test: test:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v2
- uses: ./actions/node12 - uses: ./actions/node12
with: with:
who-to-greet: 'Mona the Octocat' who-to-greet: 'Mona the Octocat'