Skip to content

Commit

Permalink
fix: map job output for reusable workflows
Browse files Browse the repository at this point in the history
This fixes the job outputs for reusable workflows. There is
a required indirection. Before this we took the outputs from
all jobs which is not what users express with the workflow
outputs.
  • Loading branch information
KnisterPeter authored and github-actions committed Jan 13, 2023
1 parent 3aeec77 commit fcbecf1
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
6 changes: 6 additions & 0 deletions pkg/exprparser/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type EvaluationEnvironment struct {
Github *model.GithubContext
Env map[string]string
Job *model.JobContext
Jobs *map[string]*model.WorkflowCallResult
Steps map[string]*model.StepResult
Runner map[string]interface{}
Secrets map[string]string
Expand Down Expand Up @@ -155,6 +156,11 @@ func (impl *interperterImpl) evaluateVariable(variableNode *actionlint.VariableN
return impl.env.Env, nil
case "job":
return impl.env.Job, nil
case "jobs":
if impl.env.Jobs == nil {
return nil, fmt.Errorf("Unavailable context: jobs")
}
return impl.env.Jobs, nil
case "steps":
return impl.env.Steps, nil
case "runner":
Expand Down
4 changes: 4 additions & 0 deletions pkg/model/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ type WorkflowCall struct {
Outputs map[string]WorkflowCallOutput `yaml:"outputs"`
}

type WorkflowCallResult struct {
Outputs map[string]string
}

func (w *Workflow) WorkflowCallConfig() *WorkflowCall {
if w.RawOn.Kind != yaml.MappingNode {
return nil
Expand Down
20 changes: 20 additions & 0 deletions pkg/runner/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func (rc *RunContext) NewExpressionEvaluator(ctx context.Context) ExpressionEval
}

func (rc *RunContext) NewExpressionEvaluatorWithEnv(ctx context.Context, env map[string]string) ExpressionEvaluator {
var workflowCallResult map[string]*model.WorkflowCallResult

// todo: cleanup EvaluationEnvironment creation
using := make(map[string]exprparser.Needs)
strategy := make(map[string]interface{})
Expand All @@ -44,6 +46,23 @@ func (rc *RunContext) NewExpressionEvaluatorWithEnv(ctx context.Context, env map
Result: jobs[needs].Result,
}
}

// only setup jobs context in case of workflow_call
// and existing expression evaluator (this means, jobs are at
// least ready to run)
if rc.caller != nil && rc.ExprEval != nil {
workflowCallResult = map[string]*model.WorkflowCallResult{}

for jobName, job := range jobs {
result := model.WorkflowCallResult{
Outputs: map[string]string{},
}
for k, v := range job.Outputs {
result.Outputs[k] = rc.ExprEval.Interpolate(ctx, v)
}
workflowCallResult[jobName] = &result
}
}
}

ghc := rc.getGithubContext(ctx)
Expand All @@ -53,6 +72,7 @@ func (rc *RunContext) NewExpressionEvaluatorWithEnv(ctx context.Context, env map
Github: ghc,
Env: env,
Job: rc.getJobContext(),
Jobs: &workflowCallResult,
// todo: should be unavailable
// but required to interpolate/evaluate the step outputs on the job
Steps: rc.getStepsContext(),
Expand Down
5 changes: 3 additions & 2 deletions pkg/runner/job_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,9 @@ func setJobOutputs(ctx context.Context, rc *RunContext) {
callerOutputs := make(map[string]string)

ee := rc.NewExpressionEvaluator(ctx)
for k, v := range rc.Run.Job().Outputs {
callerOutputs[k] = ee.Interpolate(ctx, v)

for k, v := range rc.Run.Workflow.WorkflowCallConfig().Outputs {
callerOutputs[k] = ee.Interpolate(ctx, ee.Interpolate(ctx, v.Value))
}

rc.caller.runContext.Run.Job().Outputs = callerOutputs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ on:
outputs:
output:
description: "A workflow output"
value: ${{ jobs.reusable_workflow_job.outputs.output }}
value: ${{ jobs.reusable_workflow_job.outputs.job-output }}

jobs:
reusable_workflow_job:
Expand Down Expand Up @@ -79,4 +79,4 @@ jobs:
echo "value=${{ inputs.string_required }}" >> $GITHUB_OUTPUT
outputs:
output: ${{ steps.output_test.outputs.value }}
job-output: ${{ steps.output_test.outputs.value }}

0 comments on commit fcbecf1

Please sign in to comment.