2020-02-05 00:38:41 +00:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
2020-02-11 17:10:35 +00:00
|
|
|
"context"
|
2020-02-05 00:38:41 +00:00
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"github.com/nektos/act/pkg/common"
|
|
|
|
"github.com/nektos/act/pkg/model"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Runner provides capabilities to run GitHub actions
|
|
|
|
type Runner interface {
|
2020-02-07 06:17:58 +00:00
|
|
|
NewPlanExecutor(plan *model.Plan) common.Executor
|
2020-02-14 08:41:20 +00:00
|
|
|
NewRunExecutor(run *model.Run, matrix map[string]interface{}) common.Executor
|
2020-02-05 00:38:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Config contains the config for a new runner
|
|
|
|
type Config struct {
|
2020-02-07 06:17:58 +00:00
|
|
|
Workdir string // path to working directory
|
2020-02-05 00:38:41 +00:00
|
|
|
EventName string // name of event to run
|
|
|
|
EventPath string // path to JSON file to use for event.json in containers
|
|
|
|
ReuseContainers bool // reuse containers to maintain state
|
|
|
|
ForcePull bool // force pulling of the image, if already present
|
2020-02-12 07:55:20 +00:00
|
|
|
LogOutput bool // log the output from docker run
|
2020-02-05 00:38:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type runnerImpl struct {
|
|
|
|
config *Config
|
|
|
|
eventJSON string
|
|
|
|
}
|
|
|
|
|
2020-02-07 06:17:58 +00:00
|
|
|
// New Creates a new Runner
|
|
|
|
func New(runnerConfig *Config) (Runner, error) {
|
2020-02-05 00:38:41 +00:00
|
|
|
runner := &runnerImpl{
|
|
|
|
config: runnerConfig,
|
|
|
|
}
|
|
|
|
|
|
|
|
runner.eventJSON = "{}"
|
2020-02-07 06:17:58 +00:00
|
|
|
if runnerConfig.EventPath != "" {
|
2020-02-05 00:38:41 +00:00
|
|
|
log.Debugf("Reading event.json from %s", runner.config.EventPath)
|
|
|
|
eventJSONBytes, err := ioutil.ReadFile(runner.config.EventPath)
|
|
|
|
if err != nil {
|
2020-02-07 06:17:58 +00:00
|
|
|
return nil, err
|
2020-02-05 00:38:41 +00:00
|
|
|
}
|
|
|
|
runner.eventJSON = string(eventJSONBytes)
|
|
|
|
}
|
2020-02-07 06:17:58 +00:00
|
|
|
return runner, nil
|
2020-02-05 00:38:41 +00:00
|
|
|
}
|
|
|
|
|
2020-02-07 06:17:58 +00:00
|
|
|
func (runner *runnerImpl) NewPlanExecutor(plan *model.Plan) common.Executor {
|
2020-02-05 00:38:41 +00:00
|
|
|
pipeline := make([]common.Executor, 0)
|
|
|
|
for _, stage := range plan.Stages {
|
|
|
|
stageExecutor := make([]common.Executor, 0)
|
|
|
|
for _, run := range stage.Runs {
|
2020-02-14 08:41:20 +00:00
|
|
|
// TODO - don't just grab first index of each dimension
|
|
|
|
matrix := make(map[string]interface{})
|
|
|
|
if run.Job().Strategy != nil {
|
|
|
|
for mkey, mvals := range run.Job().Strategy.Matrix {
|
|
|
|
if mkey == "include" || mkey == "exclude" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
matrix[mkey] = mvals[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stageExecutor = append(stageExecutor, runner.NewRunExecutor(run, matrix))
|
2020-02-05 00:38:41 +00:00
|
|
|
}
|
|
|
|
pipeline = append(pipeline, common.NewParallelExecutor(stageExecutor...))
|
|
|
|
}
|
|
|
|
|
2020-02-07 06:17:58 +00:00
|
|
|
return common.NewPipelineExecutor(pipeline...)
|
2020-02-05 00:38:41 +00:00
|
|
|
}
|
|
|
|
|
2020-02-14 08:41:20 +00:00
|
|
|
func (runner *runnerImpl) NewRunExecutor(run *model.Run, matrix map[string]interface{}) common.Executor {
|
2020-02-07 06:17:58 +00:00
|
|
|
rc := new(RunContext)
|
|
|
|
rc.Config = runner.config
|
|
|
|
rc.Run = run
|
|
|
|
rc.EventJSON = runner.eventJSON
|
2020-02-14 08:41:20 +00:00
|
|
|
rc.StepResults = make(map[string]*stepResult)
|
|
|
|
rc.Matrix = matrix
|
2020-02-17 06:04:13 +00:00
|
|
|
|
|
|
|
ee := rc.NewExpressionEvaluator()
|
|
|
|
rc.PlatformName = ee.Interpolate(run.Job().RunsOn)
|
2020-02-11 17:10:35 +00:00
|
|
|
return func(ctx context.Context) error {
|
|
|
|
ctx = WithJobLogger(ctx, rc.Run.String())
|
|
|
|
return rc.Executor()(ctx)
|
|
|
|
}
|
2020-02-05 00:38:41 +00:00
|
|
|
}
|