From 2c27f29e642eab2a6303e91eb6f30f0d843d9aca Mon Sep 17 00:00:00 2001 From: Casey Lee Date: Wed, 16 Jan 2019 13:11:32 -0800 Subject: [PATCH] fix #7 - allow passing a custom event.json to be populated in the container filesystem --- actions/runner.go | 25 +++++++++++++------------ actions/types.go | 4 ++-- cmd/root.go | 23 ++++++++++++++++++++--- example/.github/main.workflow | 2 +- example/event.json | 3 +++ 5 files changed, 39 insertions(+), 18 deletions(-) create mode 100644 example/event.json diff --git a/actions/runner.go b/actions/runner.go index 862c2f2f..b0a48af6 100644 --- a/actions/runner.go +++ b/actions/runner.go @@ -46,12 +46,12 @@ func (wFile *workflowsFile) GraphEvent(eventName string) ([][]string, error) { return wFile.newExecutionGraph(workflow.Resolves...), nil } -func (wFile *workflowsFile) RunAction(ctx context.Context, dryrun bool, actionName string) error { +func (wFile *workflowsFile) RunAction(ctx context.Context, dryrun bool, actionName string, eventJSON string) error { log.Debugf("Running action '%s'", actionName) - return wFile.newActionExecutor(ctx, dryrun, "", actionName)() + return wFile.newActionExecutor(ctx, dryrun, "", eventJSON, actionName)() } -func (wFile *workflowsFile) RunEvent(ctx context.Context, dryrun bool, eventName string) error { +func (wFile *workflowsFile) RunEvent(ctx context.Context, dryrun bool, eventName string, eventJSON string) error { log.Debugf("Running event '%s'", eventName) workflow, _, err := wFile.getWorkflow(eventName) if err != nil { @@ -59,7 +59,7 @@ func (wFile *workflowsFile) RunEvent(ctx context.Context, dryrun bool, eventName } log.Debugf("Running actions %s -> %s", eventName, workflow.Resolves) - return wFile.newActionExecutor(ctx, dryrun, eventName, workflow.Resolves...)() + return wFile.newActionExecutor(ctx, dryrun, eventName, eventJSON, workflow.Resolves...)() } func (wFile *workflowsFile) getWorkflow(eventName string) (*workflowDef, string, error) { @@ -138,7 +138,7 @@ func listInLists(srcList []string, searchLists ...[]string) bool { return true } -func (wFile *workflowsFile) newActionExecutor(ctx context.Context, dryrun bool, eventName string, actionNames ...string) common.Executor { +func (wFile *workflowsFile) newActionExecutor(ctx context.Context, dryrun bool, eventName string, eventJSON string, actionNames ...string) common.Executor { graph := wFile.newExecutionGraph(actionNames...) pipeline := make([]common.Executor, 0) @@ -149,7 +149,7 @@ func (wFile *workflowsFile) newActionExecutor(ctx context.Context, dryrun bool, if err != nil { return common.NewErrorExecutor(err) } - actionExecutor := action.asExecutor(ctx, dryrun, wFile.WorkingDir, wFile.TempDir, actionName, wFile.setupEnvironment(eventName, actionName, dryrun)) + actionExecutor := action.asExecutor(ctx, dryrun, wFile.WorkingDir, wFile.TempDir, actionName, wFile.setupEnvironment(eventName, actionName, dryrun), eventJSON) stage = append(stage, actionExecutor) } pipeline = append(pipeline, common.NewParallelExecutor(stage...)) @@ -158,7 +158,7 @@ func (wFile *workflowsFile) newActionExecutor(ctx context.Context, dryrun bool, return common.NewPipelineExecutor(pipeline...) } -func (action *actionDef) asExecutor(ctx context.Context, dryrun bool, workingDir string, tempDir string, actionName string, env []string) common.Executor { +func (action *actionDef) asExecutor(ctx context.Context, dryrun bool, workingDir string, tempDir string, actionName string, env []string, eventJSON string) common.Executor { logger := newActionLogger(actionName, dryrun) log.Debugf("Using '%s' for action '%s'", action.Uses, actionName) @@ -206,7 +206,7 @@ func (action *actionDef) asExecutor(ctx context.Context, dryrun bool, workingDir return common.NewErrorExecutor(fmt.Errorf("unable to determine executor type for image '%s'", action.Uses)) } - ghReader, err := action.createGithubTarball() + ghReader, err := action.createGithubTarball(eventJSON) if err != nil { return common.NewErrorExecutor(err) } @@ -244,7 +244,7 @@ func randString(slen int) string { return string(b) } -func (action *actionDef) createGithubTarball() (io.Reader, error) { +func (action *actionDef) createGithubTarball(eventJSON string) (io.Reader, error) { var buf bytes.Buffer tw := tar.NewWriter(&buf) var files = []struct { @@ -252,18 +252,19 @@ func (action *actionDef) createGithubTarball() (io.Reader, error) { Mode int64 Body string }{ - {"workflow/event.json", 0644, "{}"}, + {"workflow/event.json", 0644, eventJSON}, } for _, file := range files { + log.Debugf("Writing entry to tarball %s len:%d from %v", file.Name, len(eventJSON), eventJSON) hdr := &tar.Header{ Name: file.Name, Mode: file.Mode, - Size: int64(len(file.Body)), + Size: int64(len(eventJSON)), } if err := tw.WriteHeader(hdr); err != nil { return nil, err } - if _, err := tw.Write([]byte(file.Body)); err != nil { + if _, err := tw.Write([]byte(eventJSON)); err != nil { return nil, err } } diff --git a/actions/types.go b/actions/types.go index 16777921..abd17c13 100644 --- a/actions/types.go +++ b/actions/types.go @@ -25,12 +25,12 @@ type EventLister interface { // ActionRunner to run an action type ActionRunner interface { - RunAction(ctx context.Context, dryrun bool, action string) error + RunAction(ctx context.Context, dryrun bool, action string, eventJSON string) error } // EventRunner to run an event type EventRunner interface { - RunEvent(ctx context.Context, dryrun bool, event string) error + RunEvent(ctx context.Context, dryrun bool, event string, eventJSON string) error } type workflowDef struct { diff --git a/cmd/root.go b/cmd/root.go index 15824b3f..5c8afc5e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -3,7 +3,9 @@ package cmd import ( "context" "fmt" + "io/ioutil" "os" + "path/filepath" "github.com/nektos/act/actions" "github.com/nektos/act/common" @@ -17,6 +19,7 @@ var workingDir string var list bool var actionName string var dryrun bool +var eventPath string // Execute is the entry point to running the CLI func Execute(ctx context.Context, version string) { @@ -30,6 +33,7 @@ func Execute(ctx context.Context, version string) { } rootCmd.Flags().BoolVarP(&list, "list", "l", false, "list actions") rootCmd.Flags().StringVarP(&actionName, "action", "a", "", "run action") + rootCmd.Flags().StringVarP(&eventPath, "event", "e", "", "path to event JSON file") rootCmd.PersistentFlags().BoolVarP(&dryrun, "dryrun", "n", false, "dryrun mode") rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output") rootCmd.PersistentFlags().StringVarP(&workflowPath, "file", "f", "./.github/main.workflow", "path to workflow file") @@ -57,14 +61,27 @@ func newRunAction(ctx context.Context) func(*cobra.Command, []string) error { return listEvents(workflows) } + eventJSON := "{}" + if eventPath != "" { + if !filepath.IsAbs(eventPath) { + eventPath = filepath.Join(workingDir, eventPath) + } + log.Debugf("Reading event.json from %s", eventPath) + eventJSONBytes, err := ioutil.ReadFile(eventPath) + if err != nil { + return err + } + eventJSON = string(eventJSONBytes) + } + if actionName != "" { - return workflows.RunAction(ctx, dryrun, actionName) + return workflows.RunAction(ctx, dryrun, actionName, eventJSON) } if len(args) == 0 { - return workflows.RunEvent(ctx, dryrun, "push") + return workflows.RunEvent(ctx, dryrun, "push", eventJSON) } - return workflows.RunEvent(ctx, dryrun, args[0]) + return workflows.RunEvent(ctx, dryrun, args[0], eventJSON) } } diff --git a/example/.github/main.workflow b/example/.github/main.workflow index caad8a58..691c6430 100644 --- a/example/.github/main.workflow +++ b/example/.github/main.workflow @@ -16,6 +16,6 @@ action "test" { action "deploy" { uses = "./action2" - args = "echo 'deploy'" + runs = ["/bin/sh", "-c", "cat $GITHUB_EVENT_PATH"] needs = ["test"] } \ No newline at end of file diff --git a/example/event.json b/example/event.json new file mode 100644 index 00000000..c8c4105e --- /dev/null +++ b/example/event.json @@ -0,0 +1,3 @@ +{ + "foo": "bar" +}