Skip to content

Commit

Permalink
feat: run all composite post actions in a step
Browse files Browse the repository at this point in the history
Since composite actions could have multiple pre/post steps inside,
we need to run all of them in a single top-level pre/post step.

This PR includes a test case for this and the correct order of steps
to be executed.
  • Loading branch information
KnisterPeter authored and github-actions committed May 19, 2022
1 parent 7d6fb10 commit e4b258b
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 3 deletions.
12 changes: 9 additions & 3 deletions pkg/runner/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,13 +426,16 @@ func getOsSafeRelativePath(s, prefix string) string {

func shouldRunPostStep(step actionStep) common.Conditional {
return func(ctx context.Context) bool {
log := common.Logger(ctx)
stepResults := step.getRunContext().getStepsContext()

if stepResults[step.getStepModel().ID].Conclusion == model.StepStatusSkipped {
log.Debugf("skip post step for '%s'; main step was skipped", step.getStepModel())
return false
}

if step.getActionModel() == nil {
log.Debugf("skip post step for '%s': no action model available", step.getStepModel())
return false
}

Expand All @@ -443,14 +446,17 @@ func shouldRunPostStep(step actionStep) common.Conditional {
func hasPostStep(step actionStep) common.Conditional {
return func(ctx context.Context) bool {
action := step.getActionModel()
return (action.Runs.Using == model.ActionRunsUsingNode12 ||
action.Runs.Using == model.ActionRunsUsingNode16) &&
action.Runs.Post != ""
return action.Runs.Using == model.ActionRunsUsingComposite ||
((action.Runs.Using == model.ActionRunsUsingNode12 ||
action.Runs.Using == model.ActionRunsUsingNode16) &&
action.Runs.Post != "")
}
}

func runPostStep(step actionStep) common.Executor {
return func(ctx context.Context) error {
common.Logger(ctx).Debugf("run post step for '%s'", step.getStepModel())

rc := step.getRunContext()
stepModel := step.getStepModel()
action := step.getActionModel()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: "Action with pre and post"
description: "Action with pre and post"

inputs:
step:
description: "step"
required: true

runs:
using: "node16"
pre: pre.js
main: main.js
post: post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const { appendFileSync } = require('fs');
const env = process.env['STEP_OUTPUT_TEST'];
const step = process.env['INPUT_STEP'];
appendFileSync(process.env['GITHUB_ENV'], `;${step}`, { encoding:'utf-8' })
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const { appendFileSync } = require('fs');
const env = process.env['STEP_OUTPUT_TEST'];
const step = process.env['INPUT_STEP'];
appendFileSync(process.env['GITHUB_ENV'], `;${step}-post`, { encoding:'utf-8' })
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('pre');
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: "Test Composite Action"
description: "Test action uses composite"

runs:
using: "composite"
steps:
- uses: ./uses-composite-with-pre-and-post-steps/action-with-pre-and-post
with:
step: step1
- uses: ./uses-composite-with-pre-and-post-steps/action-with-pre-and-post
with:
step: step2
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: "last action check"
description: "last action check"

runs:
using: "node16"
main: main.js
post: post.js
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const output = process.env['STEP_OUTPUT_TEST'];
const expected = 'empty;step1;step2;step2-post;step1-post';

console.log(output);
if (output !== expected) {
throw new Error(`Expected '${expected}' but got '${output}'`);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: uses-composite-with-pre-and-post-steps
on: push

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: ./uses-composite-with-pre-and-post-steps/last-action
- uses: actions/checkout@v2
- run: echo -n "STEP_OUTPUT_TEST=empty" >> $GITHUB_ENV
- uses: ./uses-composite-with-pre-and-post-steps/composite_action

0 comments on commit e4b258b

Please sign in to comment.