2019-01-12 22:45:25 -06:00
|
|
|
package actions
|
|
|
|
|
|
|
|
import (
|
2019-01-17 02:15:35 -06:00
|
|
|
"io/ioutil"
|
2019-01-12 22:45:25 -06:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
|
2019-01-31 01:14:18 -06:00
|
|
|
"github.com/actions/workflow-parser/model"
|
|
|
|
"github.com/actions/workflow-parser/parser"
|
2019-01-12 22:45:25 -06:00
|
|
|
"github.com/nektos/act/common"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2019-01-17 02:15:35 -06:00
|
|
|
type runnerImpl struct {
|
2019-01-31 01:14:18 -06:00
|
|
|
config *RunnerConfig
|
|
|
|
workflowConfig *model.Configuration
|
|
|
|
tempDir string
|
|
|
|
eventJSON string
|
2019-01-17 02:15:35 -06:00
|
|
|
}
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2019-01-17 02:15:35 -06:00
|
|
|
// NewRunner Creates a new Runner
|
|
|
|
func NewRunner(runnerConfig *RunnerConfig) (Runner, error) {
|
|
|
|
runner := &runnerImpl{
|
|
|
|
config: runnerConfig,
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
|
2019-01-17 02:15:35 -06:00
|
|
|
init := common.NewPipelineExecutor(
|
|
|
|
runner.setupTempDir,
|
|
|
|
runner.setupWorkingDir,
|
|
|
|
runner.setupWorkflows,
|
|
|
|
runner.setupEvent,
|
|
|
|
)
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2019-01-17 02:15:35 -06:00
|
|
|
return runner, init()
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
|
2019-01-17 02:15:35 -06:00
|
|
|
func (runner *runnerImpl) setupTempDir() error {
|
|
|
|
var err error
|
|
|
|
runner.tempDir, err = ioutil.TempDir("", "act-")
|
|
|
|
return err
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
|
2019-01-17 02:15:35 -06:00
|
|
|
func (runner *runnerImpl) setupWorkingDir() error {
|
|
|
|
var err error
|
|
|
|
runner.config.WorkingDir, err = filepath.Abs(runner.config.WorkingDir)
|
|
|
|
log.Debugf("Setting working dir to %s", runner.config.WorkingDir)
|
|
|
|
return err
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
|
2019-01-17 02:15:35 -06:00
|
|
|
func (runner *runnerImpl) setupWorkflows() error {
|
|
|
|
runner.config.WorkflowPath = runner.resolvePath(runner.config.WorkflowPath)
|
|
|
|
log.Debugf("Loading workflow config from %s", runner.config.WorkflowPath)
|
|
|
|
workflowReader, err := os.Open(runner.config.WorkflowPath)
|
2019-01-12 22:45:25 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-17 02:15:35 -06:00
|
|
|
defer workflowReader.Close()
|
|
|
|
|
2019-01-31 01:14:18 -06:00
|
|
|
runner.workflowConfig, err = parser.Parse(workflowReader)
|
2019-01-17 02:15:35 -06:00
|
|
|
return err
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
|
2019-01-17 02:15:35 -06:00
|
|
|
func (runner *runnerImpl) setupEvent() error {
|
|
|
|
runner.eventJSON = "{}"
|
|
|
|
if runner.config.EventPath != "" {
|
|
|
|
runner.config.EventPath = runner.resolvePath(runner.config.EventPath)
|
|
|
|
log.Debugf("Reading event.json from %s", runner.config.EventPath)
|
|
|
|
eventJSONBytes, err := ioutil.ReadFile(runner.config.EventPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
2019-01-17 02:15:35 -06:00
|
|
|
runner.eventJSON = string(eventJSONBytes)
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
2019-01-17 02:15:35 -06:00
|
|
|
return nil
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
|
2019-01-17 02:15:35 -06:00
|
|
|
func (runner *runnerImpl) resolvePath(path string) string {
|
|
|
|
if path == "" {
|
|
|
|
return path
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
2019-01-17 02:15:35 -06:00
|
|
|
if !filepath.IsAbs(path) {
|
|
|
|
path = filepath.Join(runner.config.WorkingDir, path)
|
|
|
|
}
|
|
|
|
return path
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
|
2019-01-17 02:15:35 -06:00
|
|
|
// ListEvents gets all the events in the workflows file
|
|
|
|
func (runner *runnerImpl) ListEvents() []string {
|
|
|
|
log.Debugf("Listing all events")
|
|
|
|
events := make([]string, 0)
|
2019-01-31 01:14:18 -06:00
|
|
|
for _, w := range runner.workflowConfig.Workflows {
|
2019-01-17 02:15:35 -06:00
|
|
|
events = append(events, w.On)
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
|
2019-01-17 02:15:35 -06:00
|
|
|
// sort the list based on depth of dependencies
|
|
|
|
sort.Slice(events, func(i, j int) bool {
|
|
|
|
return events[i] < events[j]
|
|
|
|
})
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2019-01-17 02:15:35 -06:00
|
|
|
return events
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
|
2019-01-17 02:15:35 -06:00
|
|
|
// GraphEvent builds an execution path
|
|
|
|
func (runner *runnerImpl) GraphEvent(eventName string) ([][]string, error) {
|
|
|
|
log.Debugf("Listing actions for event '%s'", eventName)
|
2019-01-31 01:14:18 -06:00
|
|
|
resolves := runner.resolveEvent(runner.config.EventName)
|
|
|
|
return newExecutionGraph(runner.workflowConfig, resolves...), nil
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
|
2019-01-17 02:15:35 -06:00
|
|
|
// RunAction runs a set of actions in parallel, and their dependencies
|
|
|
|
func (runner *runnerImpl) RunActions(actionNames ...string) error {
|
|
|
|
log.Debugf("Running actions %+q", actionNames)
|
2019-01-31 01:14:18 -06:00
|
|
|
graph := newExecutionGraph(runner.workflowConfig, actionNames...)
|
2019-01-12 22:45:25 -06:00
|
|
|
|
|
|
|
pipeline := make([]common.Executor, 0)
|
|
|
|
for _, actions := range graph {
|
|
|
|
stage := make([]common.Executor, 0)
|
|
|
|
for _, actionName := range actions {
|
2019-01-17 02:15:35 -06:00
|
|
|
stage = append(stage, runner.newActionExecutor(actionName))
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
pipeline = append(pipeline, common.NewParallelExecutor(stage...))
|
|
|
|
}
|
|
|
|
|
2019-01-17 02:15:35 -06:00
|
|
|
executor := common.NewPipelineExecutor(pipeline...)
|
|
|
|
return executor()
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
|
2019-01-17 02:15:35 -06:00
|
|
|
// RunEvent runs the actions for a single event
|
|
|
|
func (runner *runnerImpl) RunEvent() error {
|
|
|
|
log.Debugf("Running event '%s'", runner.config.EventName)
|
2019-01-31 01:14:18 -06:00
|
|
|
resolves := runner.resolveEvent(runner.config.EventName)
|
|
|
|
log.Debugf("Running actions %s -> %s", runner.config.EventName, resolves)
|
|
|
|
return runner.RunActions(resolves...)
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
|
|
|
|
2019-01-17 02:15:35 -06:00
|
|
|
func (runner *runnerImpl) Close() error {
|
|
|
|
return os.RemoveAll(runner.tempDir)
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
2019-01-31 01:14:18 -06:00
|
|
|
|
|
|
|
// get list of resolves for an event
|
|
|
|
func (runner *runnerImpl) resolveEvent(eventName string) []string {
|
|
|
|
workflows := runner.workflowConfig.GetWorkflows(runner.config.EventName)
|
|
|
|
resolves := make([]string, 0)
|
|
|
|
for _, workflow := range workflows {
|
|
|
|
for _, resolve := range workflow.Resolves {
|
|
|
|
found := false
|
|
|
|
for _, r := range resolves {
|
|
|
|
if r == resolve {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
resolves = append(resolves, resolve)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return resolves
|
|
|
|
}
|