2022-05-24 08:36:06 -05:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-10-06 16:58:16 -05:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2022-05-24 08:36:06 -05:00
|
|
|
|
|
|
|
"github.com/nektos/act/pkg/common"
|
|
|
|
"github.com/nektos/act/pkg/model"
|
|
|
|
)
|
|
|
|
|
2022-10-06 16:58:16 -05:00
|
|
|
func evaluateCompositeInputAndEnv(ctx context.Context, parent *RunContext, step actionStep) map[string]string {
|
|
|
|
env := make(map[string]string)
|
|
|
|
stepEnv := *step.getEnv()
|
|
|
|
for k, v := range stepEnv {
|
|
|
|
// do not set current inputs into composite action
|
|
|
|
// the required inputs are added in the second loop
|
|
|
|
if !strings.HasPrefix(k, "INPUT_") {
|
|
|
|
env[k] = v
|
2022-05-24 08:36:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-07 09:51:27 -05:00
|
|
|
ee := parent.NewStepExpressionEvaluator(ctx, step)
|
|
|
|
|
2022-10-06 16:58:16 -05:00
|
|
|
for inputID, input := range step.getActionModel().Inputs {
|
|
|
|
envKey := regexp.MustCompile("[^A-Z0-9-]").ReplaceAllString(strings.ToUpper(inputID), "_")
|
|
|
|
envKey = fmt.Sprintf("INPUT_%s", strings.ToUpper(envKey))
|
|
|
|
|
|
|
|
// lookup if key is defined in the step but the the already
|
|
|
|
// evaluated value from the environment
|
|
|
|
_, defined := step.getStepModel().With[inputID]
|
|
|
|
if value, ok := stepEnv[envKey]; defined && ok {
|
|
|
|
env[envKey] = value
|
|
|
|
} else {
|
2022-10-07 09:51:27 -05:00
|
|
|
// defaults could contain expressions
|
|
|
|
env[envKey] = ee.Interpolate(ctx, input.Default)
|
2022-10-06 16:58:16 -05:00
|
|
|
}
|
2022-05-24 08:36:06 -05:00
|
|
|
}
|
|
|
|
|
2022-10-06 16:58:16 -05:00
|
|
|
return env
|
2022-05-24 08:36:06 -05:00
|
|
|
}
|
|
|
|
|
2022-06-17 10:55:21 -05:00
|
|
|
func newCompositeRunContext(ctx context.Context, parent *RunContext, step actionStep, actionPath string) *RunContext {
|
2022-10-06 16:58:16 -05:00
|
|
|
env := evaluateCompositeInputAndEnv(ctx, parent, step)
|
2022-05-24 08:36:06 -05:00
|
|
|
|
|
|
|
// run with the global config but without secrets
|
|
|
|
configCopy := *(parent.Config)
|
|
|
|
configCopy.Secrets = nil
|
|
|
|
|
|
|
|
// create a run context for the composite action to run in
|
|
|
|
compositerc := &RunContext{
|
|
|
|
Name: parent.Name,
|
|
|
|
JobName: parent.JobName,
|
|
|
|
Run: &model.Run{
|
|
|
|
JobID: "composite-job",
|
|
|
|
Workflow: &model.Workflow{
|
|
|
|
Name: parent.Run.Workflow.Name,
|
|
|
|
Jobs: map[string]*model.Job{
|
|
|
|
"composite-job": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-10-06 16:58:16 -05:00
|
|
|
Config: &configCopy,
|
|
|
|
StepResults: map[string]*model.StepResult{},
|
|
|
|
JobContainer: parent.JobContainer,
|
|
|
|
ActionPath: actionPath,
|
|
|
|
Env: env,
|
2023-02-04 07:35:13 -06:00
|
|
|
GlobalEnv: parent.GlobalEnv,
|
2022-10-06 16:58:16 -05:00
|
|
|
Masks: parent.Masks,
|
|
|
|
ExtraPath: parent.ExtraPath,
|
|
|
|
Parent: parent,
|
2022-11-16 11:00:49 -06:00
|
|
|
EventJSON: parent.EventJSON,
|
2022-05-24 08:36:06 -05:00
|
|
|
}
|
2022-11-16 15:55:23 -06:00
|
|
|
compositerc.ExprEval = compositerc.NewExpressionEvaluator(ctx)
|
2022-05-24 08:36:06 -05:00
|
|
|
|
|
|
|
return compositerc
|
|
|
|
}
|
|
|
|
|
|
|
|
func execAsComposite(step actionStep) common.Executor {
|
|
|
|
rc := step.getRunContext()
|
|
|
|
action := step.getActionModel()
|
|
|
|
|
|
|
|
return func(ctx context.Context) error {
|
2022-10-06 16:58:16 -05:00
|
|
|
compositeRC := step.getCompositeRunContext(ctx)
|
2022-05-24 08:36:06 -05:00
|
|
|
|
|
|
|
steps := step.getCompositeSteps()
|
|
|
|
|
2023-02-23 16:16:07 -06:00
|
|
|
if steps == nil || steps.main == nil {
|
|
|
|
return fmt.Errorf("missing steps in composite action")
|
|
|
|
}
|
|
|
|
|
2022-10-06 16:58:16 -05:00
|
|
|
ctx = WithCompositeLogger(ctx, &compositeRC.Masks)
|
2022-05-24 08:36:06 -05:00
|
|
|
|
|
|
|
err := steps.main(ctx)
|
|
|
|
|
|
|
|
// Map outputs from composite RunContext to job RunContext
|
2022-10-06 16:58:16 -05:00
|
|
|
eval := compositeRC.NewExpressionEvaluator(ctx)
|
2022-05-24 08:36:06 -05:00
|
|
|
for outputName, output := range action.Outputs {
|
|
|
|
rc.setOutput(ctx, map[string]string{
|
|
|
|
"name": outputName,
|
2022-06-17 10:55:21 -05:00
|
|
|
}, eval.Interpolate(ctx, output.Value))
|
2022-05-24 08:36:06 -05:00
|
|
|
}
|
|
|
|
|
2022-10-06 16:58:16 -05:00
|
|
|
rc.Masks = append(rc.Masks, compositeRC.Masks...)
|
|
|
|
rc.ExtraPath = compositeRC.ExtraPath
|
2023-02-04 07:35:13 -06:00
|
|
|
// compositeRC.Env is dirty, contains INPUT_ and merged step env, only rely on compositeRC.GlobalEnv
|
2023-04-18 13:09:57 -05:00
|
|
|
mergeIntoMap := mergeIntoMapCaseSensitive
|
|
|
|
if rc.JobContainer.IsEnvironmentCaseInsensitive() {
|
|
|
|
mergeIntoMap = mergeIntoMapCaseInsensitive
|
2023-02-04 07:35:13 -06:00
|
|
|
}
|
2023-04-18 13:09:57 -05:00
|
|
|
if rc.GlobalEnv == nil {
|
|
|
|
rc.GlobalEnv = map[string]string{}
|
|
|
|
}
|
|
|
|
mergeIntoMap(rc.GlobalEnv, compositeRC.GlobalEnv)
|
|
|
|
mergeIntoMap(rc.Env, compositeRC.GlobalEnv)
|
2022-05-24 08:36:06 -05:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type compositeSteps struct {
|
|
|
|
pre common.Executor
|
|
|
|
main common.Executor
|
|
|
|
post common.Executor
|
|
|
|
}
|
|
|
|
|
|
|
|
// Executor returns a pipeline executor for all the steps in the job
|
|
|
|
func (rc *RunContext) compositeExecutor(action *model.Action) *compositeSteps {
|
|
|
|
steps := make([]common.Executor, 0)
|
|
|
|
preSteps := make([]common.Executor, 0)
|
|
|
|
var postExecutor common.Executor
|
|
|
|
|
|
|
|
sf := &stepFactoryImpl{}
|
|
|
|
|
|
|
|
for i, step := range action.Runs.Steps {
|
|
|
|
if step.ID == "" {
|
|
|
|
step.ID = fmt.Sprintf("%d", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a copy of the step, since this composite action could
|
|
|
|
// run multiple times and we might modify the instance
|
|
|
|
stepcopy := step
|
|
|
|
|
|
|
|
step, err := sf.newStep(&stepcopy, rc)
|
|
|
|
if err != nil {
|
|
|
|
return &compositeSteps{
|
|
|
|
main: common.NewErrorExecutor(err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-27 14:56:41 -05:00
|
|
|
stepID := step.getStepModel().ID
|
|
|
|
stepPre := rc.newCompositeCommandExecutor(step.pre())
|
|
|
|
preSteps = append(preSteps, newCompositeStepLogExecutor(stepPre, stepID))
|
2022-05-24 08:36:06 -05:00
|
|
|
|
|
|
|
steps = append(steps, func(ctx context.Context) error {
|
2022-07-27 14:56:41 -05:00
|
|
|
ctx = WithCompositeStepLogger(ctx, stepID)
|
2022-06-17 10:55:21 -05:00
|
|
|
logger := common.Logger(ctx)
|
2022-07-27 14:56:41 -05:00
|
|
|
err := rc.newCompositeCommandExecutor(step.main())(ctx)
|
|
|
|
|
2022-05-24 08:36:06 -05:00
|
|
|
if err != nil {
|
2022-06-17 10:55:21 -05:00
|
|
|
logger.Errorf("%v", err)
|
2022-05-24 08:36:06 -05:00
|
|
|
common.SetJobError(ctx, err)
|
|
|
|
} else if ctx.Err() != nil {
|
2022-06-17 10:55:21 -05:00
|
|
|
logger.Errorf("%v", ctx.Err())
|
2022-05-24 08:36:06 -05:00
|
|
|
common.SetJobError(ctx, ctx.Err())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// run the post executor in reverse order
|
|
|
|
if postExecutor != nil {
|
2022-07-27 14:56:41 -05:00
|
|
|
stepPost := rc.newCompositeCommandExecutor(step.post())
|
2022-07-29 14:43:24 -05:00
|
|
|
postExecutor = newCompositeStepLogExecutor(stepPost.Finally(postExecutor), stepID)
|
2022-05-24 08:36:06 -05:00
|
|
|
} else {
|
2022-07-27 14:56:41 -05:00
|
|
|
stepPost := rc.newCompositeCommandExecutor(step.post())
|
|
|
|
postExecutor = newCompositeStepLogExecutor(stepPost, stepID)
|
2022-05-24 08:36:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
steps = append(steps, common.JobError)
|
|
|
|
return &compositeSteps{
|
2022-07-27 14:56:41 -05:00
|
|
|
pre: func(ctx context.Context) error {
|
|
|
|
return common.NewPipelineExecutor(preSteps...)(common.WithJobErrorContainer(ctx))
|
|
|
|
},
|
|
|
|
main: func(ctx context.Context) error {
|
2022-05-24 08:36:06 -05:00
|
|
|
return common.NewPipelineExecutor(steps...)(common.WithJobErrorContainer(ctx))
|
2022-07-27 14:56:41 -05:00
|
|
|
},
|
|
|
|
post: postExecutor,
|
2022-05-24 08:36:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *RunContext) newCompositeCommandExecutor(executor common.Executor) common.Executor {
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
ctx = WithCompositeLogger(ctx, &rc.Masks)
|
|
|
|
|
|
|
|
// We need to inject a composite RunContext related command
|
|
|
|
// handler into the current running job container
|
|
|
|
// We need this, to support scoping commands to the composite action
|
|
|
|
// executing.
|
|
|
|
rawLogger := common.Logger(ctx).WithField("raw_output", true)
|
|
|
|
logWriter := common.NewLineWriter(rc.commandHandler(ctx), func(s string) bool {
|
|
|
|
if rc.Config.LogOutput {
|
|
|
|
rawLogger.Infof("%s", s)
|
|
|
|
} else {
|
|
|
|
rawLogger.Debugf("%s", s)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
oldout, olderr := rc.JobContainer.ReplaceLogWriter(logWriter, logWriter)
|
|
|
|
defer rc.JobContainer.ReplaceLogWriter(oldout, olderr)
|
|
|
|
|
|
|
|
return executor(ctx)
|
|
|
|
}
|
|
|
|
}
|
2022-07-27 14:56:41 -05:00
|
|
|
|
|
|
|
func newCompositeStepLogExecutor(runStep common.Executor, stepID string) common.Executor {
|
|
|
|
return func(ctx context.Context) error {
|
|
|
|
ctx = WithCompositeStepLogger(ctx, stepID)
|
|
|
|
logger := common.Logger(ctx)
|
|
|
|
err := runStep(ctx)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("%v", err)
|
|
|
|
common.SetJobError(ctx, err)
|
|
|
|
} else if ctx.Err() != nil {
|
|
|
|
logger.Errorf("%v", ctx.Err())
|
|
|
|
common.SetJobError(ctx, ctx.Err())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|