Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type Input struct {
useNewActionCache bool
localRepository []string
listOptions bool
applyEventFilters bool
}

func (i *Input) resolve(path string) string {
Expand Down
79 changes: 28 additions & 51 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func createRootCommand(ctx context.Context, input *Input, version string) *cobra
rootCmd.PersistentFlags().BoolVarP(&input.useNewActionCache, "use-new-action-cache", "", false, "Enable using the new Action Cache for storing Actions locally")
rootCmd.PersistentFlags().StringArrayVarP(&input.localRepository, "local-repository", "", []string{}, "Replaces the specified repository and ref with a local folder (e.g. https://github.com/test/test@v0=/home/act/test or test/test@v0=/home/act/test, the latter matches any hosts or protocols)")
rootCmd.PersistentFlags().BoolVar(&input.listOptions, "list-options", false, "Print a json structure of compatible options")
rootCmd.PersistentFlags().BoolVar(&input.applyEventFilters, "apply-event-filters", false, "Only run workflows for an event that matches the event filters defined for the workflow.")
rootCmd.SetArgs(args())
return rootCmd
}
Expand Down Expand Up @@ -444,7 +445,7 @@ func newRunCommand(ctx context.Context, input *Input) func(*cobra.Command, []str
matrixes := parseMatrix(input.matrix)
log.Debugf("Evaluated matrix inclusions: %v", matrixes)

planner, err := model.NewWorkflowPlanner(input.WorkflowsPath(), input.noWorkflowRecurse)
planner, err := model.NewWorkflowPlanner(input.WorkflowsPath(), input.noWorkflowRecurse, input.applyEventFilters, input.eventPath)
if err != nil {
return err
}
Expand All @@ -469,53 +470,6 @@ func newRunCommand(ctx context.Context, input *Input) func(*cobra.Command, []str
// collect all events from loaded workflows
events := planner.GetEvents()

// plan with filtered jobs - to be used for filtering only
var filterPlan *model.Plan

// Determine the event name to be filtered
var filterEventName string

if len(args) > 0 {
log.Debugf("Using first passed in arguments event for filtering: %s", args[0])
filterEventName = args[0]
} else if input.autodetectEvent && len(events) > 0 && len(events[0]) > 0 {
// set default event type to first event from many available
// this way user dont have to specify the event.
log.Debugf("Using first detected workflow event for filtering: %s", events[0])
filterEventName = events[0]
}

var plannerErr error
if jobID != "" {
log.Debugf("Preparing plan with a job: %s", jobID)
filterPlan, plannerErr = planner.PlanJob(jobID)
} else if filterEventName != "" {
log.Debugf("Preparing plan for a event: %s", filterEventName)
filterPlan, plannerErr = planner.PlanEvent(filterEventName)
} else {
log.Debugf("Preparing plan with all jobs")
filterPlan, plannerErr = planner.PlanAll()
}
if filterPlan == nil && plannerErr != nil {
return plannerErr
}

if list {
err = printList(filterPlan)
if err != nil {
return err
}
return plannerErr
}

if graph {
err = drawGraph(filterPlan)
if err != nil {
return err
}
return plannerErr
}

// plan with triggered jobs
var plan *model.Plan

Expand All @@ -539,22 +493,44 @@ func newRunCommand(ctx context.Context, input *Input) func(*cobra.Command, []str
}

// build the plan for this run
var plannerErr error
if jobID != "" {
log.Debugf("Planning job: %s", jobID)
plan, plannerErr = planner.PlanJob(jobID)
plan, plannerErr = planner.PlanJob(jobID, eventName)
} else {
log.Debugf("Planning jobs for event: %s", eventName)
plan, plannerErr = planner.PlanEvent(eventName)
}
if plan != nil {
if len(plan.Stages) == 0 {
plannerErr = fmt.Errorf("Could not find any stages to run. View the valid jobs with `act --list`. Use `act --help` to find how to filter by Job ID/Workflow/Event Name")
if len(plan.Stages) == 0 && !input.applyEventFilters {
plannerErr = fmt.Errorf("could not find any stages to run. View the valid jobs with `act --list`. Use `act --help` to find how to filter by Job ID/Workflow/Event Name")
}
}
if plan == nil && plannerErr != nil {
return plannerErr
}

if list {
err = printList(plan)
if err != nil {
return err
}
return plannerErr
}

if graph {
err = drawGraph(plan)
if err != nil {
return err
}
return plannerErr
}

if len(plan.Stages) == 0 && input.applyEventFilters {
log.Info("The configured event filters caused all jobs to be skipped - if you are expecting a job to execute then check your supplied event structure against the workflow 'on' filters, or run Act without applying event filters.")
return plannerErr
}

// check to see if the main branch was defined
defaultbranch, err := cmd.Flags().GetString("defaultbranch")
if err != nil {
Expand Down Expand Up @@ -635,6 +611,7 @@ func newRunCommand(ctx context.Context, input *Input) func(*cobra.Command, []str
ReplaceGheActionTokenWithGithubCom: input.replaceGheActionTokenWithGithubCom,
Matrix: matrixes,
ContainerNetworkMode: docker_container.NetworkMode(input.networkName),
ApplyEventFilters: input.applyEventFilters,
}
if input.useNewActionCache || len(input.localRepository) > 0 {
if input.actionOfflineMode {
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ require (
dario.cat/mergo v1.0.1
github.com/distribution/reference v0.6.0
github.com/golang-jwt/jwt/v5 v5.2.2
github.com/google/go-github/v71 v71.0.0
google.golang.org/protobuf v1.36.6
)

Expand All @@ -63,6 +64,7 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
github.com/google/go-cmp v0.7.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,13 @@ github.com/golang-jwt/jwt/v5 v5.2.2 h1:Rl4B7itRWVtYIHFrSNd7vhTiz9UpLdi6gZhZ3wEeD
github.com/golang-jwt/jwt/v5 v5.2.2/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 h1:f+oWsMOmNPc8JmEHVZIycC7hBoQxHH9pNKQORJNozsQ=
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8/go.mod h1:wcDNUvekVysuuOpQKo3191zZyTpiI6se1N1ULghS0sw=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/google/go-github/v71 v71.0.0 h1:Zi16OymGKZZMm8ZliffVVJ/Q9YZreDKONCr+WUd0Z30=
github.com/google/go-github/v71 v71.0.0/go.mod h1:URZXObp2BLlMjwu0O8g4y6VBneUj2bCHgnI8FfgZ51M=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
Expand Down
2 changes: 1 addition & 1 deletion pkg/artifacts/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func runTestJobFile(ctx context.Context, t *testing.T, tjfi TestJobFileInfo) {
runner, err := runner.New(runnerConfig)
assert.Nil(t, err, tjfi.workflowPath)

planner, err := model.NewWorkflowPlanner(fullWorkflowPath, true)
planner, err := model.NewWorkflowPlanner(fullWorkflowPath, true, false, "")
assert.Nil(t, err, fullWorkflowPath)

plan, err := planner.PlanEvent(tjfi.eventName)
Expand Down
77 changes: 57 additions & 20 deletions pkg/model/planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import (
// WorkflowPlanner contains methods for creating plans
type WorkflowPlanner interface {
PlanEvent(eventName string) (*Plan, error)
PlanJob(jobName string) (*Plan, error)
PlanJob(jobName string, eventName string) (*Plan, error)
PlanAll() (*Plan, error)
GetEvents() []string
ShouldApplyEventFilters() bool
EventPath() string
}

// Plan contains a list of stages to run in series
Expand Down Expand Up @@ -56,7 +58,7 @@ type WorkflowFiles struct {
}

// NewWorkflowPlanner will load a specific workflow, all workflows from a directory or all workflows from a directory and its subdirectories
func NewWorkflowPlanner(path string, noWorkflowRecurse bool) (WorkflowPlanner, error) {
func NewWorkflowPlanner(path string, noWorkflowRecurse bool, applyEventFilters bool, eventPath string) (WorkflowPlanner, error) {
path, err := filepath.Abs(path)
if err != nil {
return nil, err
Expand Down Expand Up @@ -153,11 +155,12 @@ func NewWorkflowPlanner(path string, noWorkflowRecurse bool) (WorkflowPlanner, e
_ = f.Close()
}
}

wp.shouldApplyEventFilters = applyEventFilters
wp.eventPath = eventPath
return wp, nil
}

func NewSingleWorkflowPlanner(name string, f io.Reader) (WorkflowPlanner, error) {
func NewSingleWorkflowPlanner(name string, f io.Reader, applyEventFilters bool, eventPath string) (WorkflowPlanner, error) {
wp := new(workflowPlanner)

log.Debugf("Reading workflow %s", name)
Expand All @@ -179,7 +182,8 @@ func NewSingleWorkflowPlanner(name string, f io.Reader) (WorkflowPlanner, error)
}

wp.workflows = append(wp.workflows, workflow)

wp.shouldApplyEventFilters = applyEventFilters
wp.eventPath = eventPath
return wp, nil
}

Expand All @@ -194,7 +198,9 @@ func validateJobName(workflow *Workflow) error {
}

type workflowPlanner struct {
workflows []*Workflow
workflows []*Workflow
shouldApplyEventFilters bool
eventPath string
}

// PlanEvent builds a new list of runs to execute in parallel for an event name
Expand All @@ -207,20 +213,39 @@ func (wp *workflowPlanner) PlanEvent(eventName string) (*Plan, error) {
var lastErr error

for _, w := range wp.workflows {
events := w.On()
if len(events) == 0 {
log.Debugf("no events found for workflow: %s", w.File)
continue
}
// the user might want the legacy behaviour so we do this in two sections
if wp.ShouldApplyEventFilters() {
// user has explicitly opted in to maybe not having a workflow run because of the event filters
if w.ShouldFilterForEvent(eventName, wp.EventPath()) {
log.Debugf("Skipping workflow %s because workflow does not have an event filter that matches this event", w.Name)
continue
}

for _, e := range events {
if e == eventName {
stages, err := createStages(w, w.GetJobIDs()...)
if err != nil {
log.Warn(err)
lastErr = err
} else {
plan.mergeStages(stages)
stages, err := createStages(w, w.GetJobIDs()...)
if err != nil {
log.Warn(err)
lastErr = err
} else {
plan.mergeStages(stages)
}
} else {
// the user wants the legacy behaviour which is that if there is a matching on event then we execute it
// even if the filters for branch/tag/path don't match
events := w.On()
if len(events) == 0 {
log.Debugf("no events found for workflow: %s", w.File)
continue
}

for _, e := range events {
if e == eventName {
stages, err := createStages(w, w.GetJobIDs()...)
if err != nil {
log.Warn(err)
lastErr = err
} else {
plan.mergeStages(stages)
}
}
}
}
Expand All @@ -229,14 +254,18 @@ func (wp *workflowPlanner) PlanEvent(eventName string) (*Plan, error) {
}

// PlanJob builds a new run to execute in parallel for a job name
func (wp *workflowPlanner) PlanJob(jobName string) (*Plan, error) {
func (wp *workflowPlanner) PlanJob(jobName string, eventName string) (*Plan, error) {
plan := new(Plan)
if len(wp.workflows) == 0 {
log.Debugf("no jobs found for workflow: %s", jobName)
}
var lastErr error

for _, w := range wp.workflows {
if wp.ShouldApplyEventFilters() && w.ShouldFilterForEvent(eventName, wp.EventPath()) {
log.Debugf("Skipping workflow %s because workflow does not have an event filter that matches this event", w.Name)
continue
}
stages, err := createStages(w, jobName)
if err != nil {
log.Warn(err)
Expand Down Expand Up @@ -300,6 +329,14 @@ func (wp *workflowPlanner) GetEvents() []string {
return events
}

func (wp *workflowPlanner) ShouldApplyEventFilters() bool {
return wp.shouldApplyEventFilters
}

func (wp *workflowPlanner) EventPath() string {
return wp.eventPath
}

// MaxRunNameLen determines the max name length of all jobs
func (p *Plan) MaxRunNameLen() int {
maxRunNameLen := 0
Expand Down
Loading