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"
|
|
|
|
|
|
|
|
"github.com/nektos/act/common"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2019-01-17 02:15:35 -06:00
|
|
|
type runnerImpl struct {
|
|
|
|
config *RunnerConfig
|
|
|
|
workflows *workflowsFile
|
|
|
|
tempDir string
|
|
|
|
eventJSON string
|
|
|
|
}
|
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()
|
|
|
|
|
|
|
|
runner.workflows, err = parseWorkflowsFile(workflowReader)
|
|
|
|
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)
|
|
|
|
for _, w := range runner.workflows.Workflow {
|
|
|
|
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)
|
|
|
|
workflow, _, err := runner.workflows.getWorkflow(eventName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-01-12 22:45:25 -06:00
|
|
|
}
|
2019-01-17 02:15:35 -06:00
|
|
|
return runner.workflows.newExecutionGraph(workflow.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)
|
|
|
|
graph := runner.workflows.newExecutionGraph(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)
|
|
|
|
workflow, _, err := runner.workflows.getWorkflow(runner.config.EventName)
|
2019-01-12 22:45:25 -06:00
|
|
|
if err != nil {
|
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
|
|
|
log.Debugf("Running actions %s -> %s", runner.config.EventName, workflow.Resolves)
|
|
|
|
return runner.RunActions(workflow.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
|
|
|
}
|