2020-02-04 18:38:41 -06:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
2020-02-11 11:10:35 -06:00
|
|
|
"context"
|
2020-02-17 12:25:28 -06:00
|
|
|
"fmt"
|
2020-02-04 18:38:41 -06: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 00:17:58 -06:00
|
|
|
NewPlanExecutor(plan *model.Plan) common.Executor
|
2020-02-04 18:38:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Config contains the config for a new runner
|
|
|
|
type Config struct {
|
2020-05-12 02:14:56 -05:00
|
|
|
Actor string // the user that triggered the event
|
2020-02-17 23:51:49 -06:00
|
|
|
Workdir string // path to working directory
|
2020-02-24 19:48:21 -06:00
|
|
|
BindWorkdir bool // bind the workdir to the job container
|
2020-02-17 23:51:49 -06:00
|
|
|
EventName string // name of event to run
|
|
|
|
EventPath string // path to JSON file to use for event.json in containers
|
2020-09-02 09:56:44 -05:00
|
|
|
DefaultBranch string // name of the main branch for this repository
|
2020-02-17 23:51:49 -06:00
|
|
|
ReuseContainers bool // reuse containers to maintain state
|
|
|
|
ForcePull bool // force pulling of the image, if already present
|
|
|
|
LogOutput bool // log the output from docker run
|
2020-04-17 12:04:40 -05:00
|
|
|
Env map[string]string // env for containers
|
2020-02-17 23:51:49 -06:00
|
|
|
Secrets map[string]string // list of secrets
|
2021-01-12 00:28:45 -06:00
|
|
|
InsecureSecrets bool // switch hiding output when printing to terminal
|
2020-02-19 21:16:40 -06:00
|
|
|
Platforms map[string]string // list of platforms
|
2020-08-01 15:21:49 -05:00
|
|
|
Privileged bool // use privileged mode
|
2021-02-27 10:31:25 -06:00
|
|
|
UsernsMode string // user namespace to use
|
2020-02-04 18:38:41 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type runnerImpl struct {
|
|
|
|
config *Config
|
|
|
|
eventJSON string
|
|
|
|
}
|
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
// New Creates a new Runner
|
|
|
|
func New(runnerConfig *Config) (Runner, error) {
|
2020-02-04 18:38:41 -06:00
|
|
|
runner := &runnerImpl{
|
|
|
|
config: runnerConfig,
|
|
|
|
}
|
|
|
|
|
|
|
|
runner.eventJSON = "{}"
|
2020-02-07 00:17:58 -06:00
|
|
|
if runnerConfig.EventPath != "" {
|
2020-02-04 18:38:41 -06:00
|
|
|
log.Debugf("Reading event.json from %s", runner.config.EventPath)
|
|
|
|
eventJSONBytes, err := ioutil.ReadFile(runner.config.EventPath)
|
|
|
|
if err != nil {
|
2020-02-07 00:17:58 -06:00
|
|
|
return nil, err
|
2020-02-04 18:38:41 -06:00
|
|
|
}
|
|
|
|
runner.eventJSON = string(eventJSONBytes)
|
|
|
|
}
|
2020-02-07 00:17:58 -06:00
|
|
|
return runner, nil
|
2020-02-04 18:38:41 -06:00
|
|
|
}
|
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
func (runner *runnerImpl) NewPlanExecutor(plan *model.Plan) common.Executor {
|
2020-02-27 01:29:43 -06:00
|
|
|
maxJobNameLen := 0
|
2020-02-17 12:25:28 -06:00
|
|
|
|
2020-02-04 18:38:41 -06:00
|
|
|
pipeline := make([]common.Executor, 0)
|
|
|
|
for _, stage := range plan.Stages {
|
|
|
|
stageExecutor := make([]common.Executor, 0)
|
|
|
|
for _, run := range stage.Runs {
|
2020-02-17 12:11:16 -06:00
|
|
|
job := run.Job()
|
2020-02-23 17:01:25 -06:00
|
|
|
matrixes := job.GetMatrixes()
|
2020-02-14 02:41:20 -06:00
|
|
|
|
2020-02-27 01:29:43 -06:00
|
|
|
for i, matrix := range matrixes {
|
|
|
|
rc := runner.newRunContext(run, matrix)
|
2020-03-04 18:24:14 -06:00
|
|
|
if len(matrixes) > 1 {
|
2020-02-27 01:29:43 -06:00
|
|
|
rc.Name = fmt.Sprintf("%s-%d", rc.Name, i+1)
|
|
|
|
}
|
|
|
|
if len(rc.String()) > maxJobNameLen {
|
|
|
|
maxJobNameLen = len(rc.String())
|
|
|
|
}
|
2020-02-17 12:25:28 -06:00
|
|
|
stageExecutor = append(stageExecutor, func(ctx context.Context) error {
|
2020-02-27 01:29:43 -06:00
|
|
|
jobName := fmt.Sprintf("%-*s", maxJobNameLen, rc.String())
|
2021-01-12 00:28:45 -06:00
|
|
|
return rc.Executor()(WithJobLogger(ctx, jobName, rc.Config.Secrets, rc.Config.InsecureSecrets))
|
2020-02-17 12:25:28 -06:00
|
|
|
})
|
2020-02-17 12:11:16 -06:00
|
|
|
}
|
2020-02-04 18:38:41 -06:00
|
|
|
}
|
|
|
|
pipeline = append(pipeline, common.NewParallelExecutor(stageExecutor...))
|
|
|
|
}
|
|
|
|
|
2020-02-07 00:17:58 -06:00
|
|
|
return common.NewPipelineExecutor(pipeline...)
|
2020-02-04 18:38:41 -06:00
|
|
|
}
|
|
|
|
|
2020-02-27 01:29:43 -06:00
|
|
|
func (runner *runnerImpl) newRunContext(run *model.Run, matrix map[string]interface{}) *RunContext {
|
2020-02-23 17:01:25 -06:00
|
|
|
rc := &RunContext{
|
|
|
|
Config: runner.config,
|
|
|
|
Run: run,
|
|
|
|
EventJSON: runner.eventJSON,
|
|
|
|
StepResults: make(map[string]*stepResult),
|
|
|
|
Matrix: matrix,
|
2020-02-17 12:11:16 -06:00
|
|
|
}
|
|
|
|
rc.ExprEval = rc.NewExpressionEvaluator()
|
2020-02-27 01:29:43 -06:00
|
|
|
rc.Name = rc.ExprEval.Interpolate(run.String())
|
|
|
|
return rc
|
2020-02-04 18:38:41 -06:00
|
|
|
}
|