1
0
Fork 0
mirror of https://code.forgejo.org/forgejo/runner.git synced 2025-08-06 17:40:58 +00:00

fix #7 - allow passing a custom event.json to be populated in the container filesystem

This commit is contained in:
Casey Lee 2019-01-16 13:11:32 -08:00
parent a8b6f2843d
commit 2c27f29e64
5 changed files with 39 additions and 18 deletions

View file

@ -46,12 +46,12 @@ func (wFile *workflowsFile) GraphEvent(eventName string) ([][]string, error) {
return wFile.newExecutionGraph(workflow.Resolves...), nil 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) 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) log.Debugf("Running event '%s'", eventName)
workflow, _, err := wFile.getWorkflow(eventName) workflow, _, err := wFile.getWorkflow(eventName)
if err != nil { 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) 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) { func (wFile *workflowsFile) getWorkflow(eventName string) (*workflowDef, string, error) {
@ -138,7 +138,7 @@ func listInLists(srcList []string, searchLists ...[]string) bool {
return true 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...) graph := wFile.newExecutionGraph(actionNames...)
pipeline := make([]common.Executor, 0) pipeline := make([]common.Executor, 0)
@ -149,7 +149,7 @@ func (wFile *workflowsFile) newActionExecutor(ctx context.Context, dryrun bool,
if err != nil { if err != nil {
return common.NewErrorExecutor(err) 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) stage = append(stage, actionExecutor)
} }
pipeline = append(pipeline, common.NewParallelExecutor(stage...)) pipeline = append(pipeline, common.NewParallelExecutor(stage...))
@ -158,7 +158,7 @@ func (wFile *workflowsFile) newActionExecutor(ctx context.Context, dryrun bool,
return common.NewPipelineExecutor(pipeline...) 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) logger := newActionLogger(actionName, dryrun)
log.Debugf("Using '%s' for action '%s'", action.Uses, actionName) 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)) 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 { if err != nil {
return common.NewErrorExecutor(err) return common.NewErrorExecutor(err)
} }
@ -244,7 +244,7 @@ func randString(slen int) string {
return string(b) return string(b)
} }
func (action *actionDef) createGithubTarball() (io.Reader, error) { func (action *actionDef) createGithubTarball(eventJSON string) (io.Reader, error) {
var buf bytes.Buffer var buf bytes.Buffer
tw := tar.NewWriter(&buf) tw := tar.NewWriter(&buf)
var files = []struct { var files = []struct {
@ -252,18 +252,19 @@ func (action *actionDef) createGithubTarball() (io.Reader, error) {
Mode int64 Mode int64
Body string Body string
}{ }{
{"workflow/event.json", 0644, "{}"}, {"workflow/event.json", 0644, eventJSON},
} }
for _, file := range files { for _, file := range files {
log.Debugf("Writing entry to tarball %s len:%d from %v", file.Name, len(eventJSON), eventJSON)
hdr := &tar.Header{ hdr := &tar.Header{
Name: file.Name, Name: file.Name,
Mode: file.Mode, Mode: file.Mode,
Size: int64(len(file.Body)), Size: int64(len(eventJSON)),
} }
if err := tw.WriteHeader(hdr); err != nil { if err := tw.WriteHeader(hdr); err != nil {
return nil, err return nil, err
} }
if _, err := tw.Write([]byte(file.Body)); err != nil { if _, err := tw.Write([]byte(eventJSON)); err != nil {
return nil, err return nil, err
} }
} }

View file

@ -25,12 +25,12 @@ type EventLister interface {
// ActionRunner to run an action // ActionRunner to run an action
type ActionRunner interface { 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 // EventRunner to run an event
type EventRunner interface { 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 { type workflowDef struct {

View file

@ -3,7 +3,9 @@ package cmd
import ( import (
"context" "context"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath"
"github.com/nektos/act/actions" "github.com/nektos/act/actions"
"github.com/nektos/act/common" "github.com/nektos/act/common"
@ -17,6 +19,7 @@ var workingDir string
var list bool var list bool
var actionName string var actionName string
var dryrun bool var dryrun bool
var eventPath string
// Execute is the entry point to running the CLI // Execute is the entry point to running the CLI
func Execute(ctx context.Context, version string) { 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().BoolVarP(&list, "list", "l", false, "list actions")
rootCmd.Flags().StringVarP(&actionName, "action", "a", "", "run action") 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(&dryrun, "dryrun", "n", false, "dryrun mode")
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output") rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose output")
rootCmd.PersistentFlags().StringVarP(&workflowPath, "file", "f", "./.github/main.workflow", "path to workflow file") 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) 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 != "" { if actionName != "" {
return workflows.RunAction(ctx, dryrun, actionName) return workflows.RunAction(ctx, dryrun, actionName, eventJSON)
} }
if len(args) == 0 { 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)
} }
} }

View file

@ -16,6 +16,6 @@ action "test" {
action "deploy" { action "deploy" {
uses = "./action2" uses = "./action2"
args = "echo 'deploy'" runs = ["/bin/sh", "-c", "cat $GITHUB_EVENT_PATH"]
needs = ["test"] needs = ["test"]
} }

3
example/event.json Normal file
View file

@ -0,0 +1,3 @@
{
"foo": "bar"
}