Fix issue 416 (#423)

This is a solution to issue #416 where environment variables created or
changed in the previous step are not usable in the next step because
the rc.ExprEval is from the beginning of the previous step.

This change refactors setupEnv so that before interpolating the environment
variables a NewExpressionEvaluator is created.


Fixes: 416
这个提交包含在:
Wink Saville
2021-01-12 16:02:54 -08:00
提交者 GitHub
父节点 2811101dea
当前提交 f2c15074ac
修改 2 个文件,包含 43 行新增27 行删除

查看文件

@@ -202,15 +202,11 @@ func (rc *RunContext) newStepExecutor(step *model.Step) common.Executor {
Outputs: make(map[string]string), Outputs: make(map[string]string),
} }
_ = sc.setupEnv()(ctx) exprEval, err := sc.setupEnv(ctx)
if err != nil {
if sc.Env != nil { return err
err := rc.JobContainer.UpdateFromGithubEnv(&sc.Env)(ctx)
if err != nil {
return err
}
} }
rc.ExprEval = sc.NewExpressionEvaluator() rc.ExprEval = exprEval;
runStep, err := rc.EvalBool(sc.Step.If) runStep, err := rc.EvalBool(sc.Step.If)
if err != nil { if err != nil {

查看文件

@@ -79,33 +79,53 @@ func (sc *StepContext) Executor() common.Executor {
return common.NewErrorExecutor(fmt.Errorf("Unable to determine how to run job:%s step:%+v", rc.Run, step)) return common.NewErrorExecutor(fmt.Errorf("Unable to determine how to run job:%s step:%+v", rc.Run, step))
} }
func (sc *StepContext) setupEnv() common.Executor { func (sc *StepContext) mergeEnv() map[string]string {
rc := sc.RunContext rc := sc.RunContext
job := rc.Run.Job() job := rc.Run.Job()
step := sc.Step step := sc.Step
return func(ctx context.Context) error {
var env map[string]string
c := job.Container()
if c != nil {
env = mergeMaps(rc.GetEnv(), c.Env, step.GetEnv())
} else {
env = mergeMaps(rc.GetEnv(), step.GetEnv())
}
if (rc.ExtraPath != nil) && (len(rc.ExtraPath) > 0) { var env map[string]string
s := append(rc.ExtraPath, `/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin`) c := job.Container()
env["PATH"] = strings.Join(s, `:`) if c != nil {
} env = mergeMaps(rc.GetEnv(), c.Env, step.GetEnv())
} else {
env = mergeMaps(rc.GetEnv(), step.GetEnv())
}
for k, v := range env { if (rc.ExtraPath != nil) && (len(rc.ExtraPath) > 0) {
env[k] = rc.ExprEval.Interpolate(v) s := append(rc.ExtraPath, `/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin`)
} env["PATH"] = strings.Join(s, `:`)
sc.Env = rc.withGithubEnv(env) }
log.Debugf("setupEnv => %v", sc.Env)
return nil sc.Env = rc.withGithubEnv(env)
return env
}
func (sc *StepContext) interpolateEnv(exprEval ExpressionEvaluator) {
for k, v := range sc.Env {
sc.Env[k] = exprEval.Interpolate(v)
} }
} }
func (sc *StepContext) setupEnv(ctx context.Context) (ExpressionEvaluator, error) {
rc := sc.RunContext
sc.Env = sc.mergeEnv()
if sc.Env != nil {
err := rc.JobContainer.UpdateFromGithubEnv(&sc.Env)(ctx)
if err != nil {
return nil, err
}
}
evaluator := sc.NewExpressionEvaluator()
sc.interpolateEnv(evaluator)
log.Debugf("setupEnv: %v", sc.Env)
return evaluator, nil
}
func (sc *StepContext) setupShellCommand() common.Executor { func (sc *StepContext) setupShellCommand() common.Executor {
rc := sc.RunContext rc := sc.RunContext
step := sc.Step step := sc.Step