-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: allow to spawn and run a local reusable workflow #1423
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
46c6e59
feat: allow to spawn and run a local reusable workflow
KnisterPeter 2427e3c
feat: add workflow_call inputs
KnisterPeter be411f3
test: improve inputs test
KnisterPeter 8457be5
feat: add input defaults
KnisterPeter 3c1e340
feat: allow expressions in inputs
KnisterPeter 0ebe02b
feat: use context specific expression evaluator
KnisterPeter 5589cf1
refactor: prepare for better re-usability
KnisterPeter 92955f5
feat: add secrets for reusable workflows
KnisterPeter b14105e
test: use secrets during test run
KnisterPeter 74da5b0
feat: handle reusable workflow outputs
KnisterPeter f911901
Merge branch 'master' into local-reusable-workflows
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,7 +55,7 @@ func (rc *RunContext) NewExpressionEvaluatorWithEnv(ctx context.Context, env map | |
// todo: should be unavailable | ||
// but required to interpolate/evaluate the step outputs on the job | ||
Steps: rc.getStepsContext(), | ||
Secrets: rc.Config.Secrets, | ||
Secrets: getWorkflowSecrets(ctx, rc), | ||
Strategy: strategy, | ||
Matrix: rc.Matrix, | ||
Needs: using, | ||
|
@@ -101,7 +101,7 @@ func (rc *RunContext) NewStepExpressionEvaluator(ctx context.Context, step step) | |
Env: *step.getEnv(), | ||
Job: rc.getJobContext(), | ||
Steps: rc.getStepsContext(), | ||
Secrets: rc.Config.Secrets, | ||
Secrets: getWorkflowSecrets(ctx, rc), | ||
Strategy: strategy, | ||
Matrix: rc.Matrix, | ||
Needs: using, | ||
|
@@ -315,6 +315,8 @@ func rewriteSubExpression(ctx context.Context, in string, forceFormat bool) (str | |
func getEvaluatorInputs(ctx context.Context, rc *RunContext, step step, ghc *model.GithubContext) map[string]interface{} { | ||
inputs := map[string]interface{}{} | ||
|
||
setupWorkflowInputs(ctx, &inputs, rc) | ||
|
||
var env map[string]string | ||
if step != nil { | ||
env = *step.getEnv() | ||
|
@@ -347,3 +349,54 @@ func getEvaluatorInputs(ctx context.Context, rc *RunContext, step step, ghc *mod | |
|
||
return inputs | ||
} | ||
|
||
func setupWorkflowInputs(ctx context.Context, inputs *map[string]interface{}, rc *RunContext) { | ||
if rc.caller != nil { | ||
config := rc.Run.Workflow.WorkflowCallConfig() | ||
|
||
for name, input := range config.Inputs { | ||
value := rc.caller.runContext.Run.Job().With[name] | ||
if value != nil { | ||
if str, ok := value.(string); ok { | ||
// evaluate using the calling RunContext (outside) | ||
value = rc.caller.runContext.ExprEval.Interpolate(ctx, str) | ||
} | ||
} | ||
|
||
if value == nil && config != nil && config.Inputs != nil { | ||
value = input.Default | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on my research, the inputs ctx is available here as the workflow_dispatch inputs of the caller workflow. |
||
if rc.ExprEval != nil { | ||
if str, ok := value.(string); ok { | ||
// evaluate using the called RunContext (inside) | ||
value = rc.ExprEval.Interpolate(ctx, str) | ||
} | ||
} | ||
} | ||
|
||
(*inputs)[name] = value | ||
} | ||
} | ||
} | ||
|
||
func getWorkflowSecrets(ctx context.Context, rc *RunContext) map[string]string { | ||
if rc.caller != nil { | ||
job := rc.caller.runContext.Run.Job() | ||
secrets := job.Secrets() | ||
|
||
if secrets == nil && job.InheritSecrets() { | ||
secrets = rc.caller.runContext.Config.Secrets | ||
} | ||
|
||
if secrets == nil { | ||
secrets = map[string]string{} | ||
} | ||
|
||
for k, v := range secrets { | ||
secrets[k] = rc.caller.runContext.ExprEval.Interpolate(ctx, v) | ||
} | ||
|
||
return secrets | ||
} | ||
|
||
return rc.Config.Secrets | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package runner | ||
|
||
import ( | ||
"fmt" | ||
"path" | ||
|
||
"github.com/nektos/act/pkg/common" | ||
"github.com/nektos/act/pkg/model" | ||
) | ||
|
||
func newLocalReusableWorkflowExecutor(rc *RunContext) common.Executor { | ||
return newReusableWorkflowExecutor(rc, rc.Config.Workdir) | ||
} | ||
|
||
func newRemoteReusableWorkflowExecutor(rc *RunContext) common.Executor { | ||
return common.NewErrorExecutor(fmt.Errorf("remote reusable workflows are currently not supported (see https://github.com/nektos/act/issues/826 for updates)")) | ||
} | ||
|
||
func newReusableWorkflowExecutor(rc *RunContext, directory string) common.Executor { | ||
planner, err := model.NewWorkflowPlanner(path.Join(directory, rc.Run.Job().Uses), true) | ||
if err != nil { | ||
return common.NewErrorExecutor(err) | ||
} | ||
|
||
plan := planner.PlanEvent("workflow_call") | ||
|
||
runner, err := NewReusableWorkflowRunner(rc) | ||
if err != nil { | ||
return common.NewErrorExecutor(err) | ||
} | ||
|
||
return runner.NewPlanExecutor(plan) | ||
} | ||
|
||
func NewReusableWorkflowRunner(rc *RunContext) (Runner, error) { | ||
runner := &runnerImpl{ | ||
config: rc.Config, | ||
eventJSON: rc.EventJSON, | ||
caller: &caller{ | ||
runContext: rc, | ||
}, | ||
} | ||
|
||
return runner.configure() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this behind workflow_dispatch (line 349) ?, because workflow_call has precedence and unset inputs are filled up by workflow_dispatch inputs of the parent workflow.
The workflow_dispatch on field is probably wrong in this case..., the value is correct.Is
github.event_name
/ ghc.EventName correctly set the parent / grandparent event_name? It is never workflow_call.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The inputs in the expression evaluator are a bit messy currently.
We should consider refactoring the setup of the evaluator to be more flexible and factor in the context availability.
Currently we only have to kind of fixed setups for the expression evaluator which is too tightly coupled.