From e4b258bcd5d7a7b011a532e5c9e975766aca5821 Mon Sep 17 00:00:00 2001 From: Markus Wolf Date: Mon, 4 Apr 2022 10:53:56 +0200 Subject: [PATCH] feat: run all composite post actions in a step 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. --- pkg/runner/action.go | 12 +++++++++--- .../action-with-pre-and-post/action.yml | 13 +++++++++++++ .../action-with-pre-and-post/main.js | 4 ++++ .../action-with-pre-and-post/post.js | 4 ++++ .../action-with-pre-and-post/pre.js | 1 + .../composite_action/action.yml | 12 ++++++++++++ .../last-action/action.yml | 7 +++++++ .../last-action/main.js | 0 .../last-action/post.js | 7 +++++++ .../uses-composite-with-pre-and-post-steps/push.yml | 11 +++++++++++ 10 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 pkg/runner/testdata/uses-composite-with-pre-and-post-steps/action-with-pre-and-post/action.yml create mode 100644 pkg/runner/testdata/uses-composite-with-pre-and-post-steps/action-with-pre-and-post/main.js create mode 100644 pkg/runner/testdata/uses-composite-with-pre-and-post-steps/action-with-pre-and-post/post.js create mode 100644 pkg/runner/testdata/uses-composite-with-pre-and-post-steps/action-with-pre-and-post/pre.js create mode 100644 pkg/runner/testdata/uses-composite-with-pre-and-post-steps/composite_action/action.yml create mode 100644 pkg/runner/testdata/uses-composite-with-pre-and-post-steps/last-action/action.yml create mode 100644 pkg/runner/testdata/uses-composite-with-pre-and-post-steps/last-action/main.js create mode 100644 pkg/runner/testdata/uses-composite-with-pre-and-post-steps/last-action/post.js create mode 100644 pkg/runner/testdata/uses-composite-with-pre-and-post-steps/push.yml diff --git a/pkg/runner/action.go b/pkg/runner/action.go index 3d4b6c466ce..626619ed07c 100644 --- a/pkg/runner/action.go +++ b/pkg/runner/action.go @@ -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 } @@ -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() diff --git a/pkg/runner/testdata/uses-composite-with-pre-and-post-steps/action-with-pre-and-post/action.yml b/pkg/runner/testdata/uses-composite-with-pre-and-post-steps/action-with-pre-and-post/action.yml new file mode 100644 index 00000000000..63be7989bee --- /dev/null +++ b/pkg/runner/testdata/uses-composite-with-pre-and-post-steps/action-with-pre-and-post/action.yml @@ -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 diff --git a/pkg/runner/testdata/uses-composite-with-pre-and-post-steps/action-with-pre-and-post/main.js b/pkg/runner/testdata/uses-composite-with-pre-and-post-steps/action-with-pre-and-post/main.js new file mode 100644 index 00000000000..ebd555521b4 --- /dev/null +++ b/pkg/runner/testdata/uses-composite-with-pre-and-post-steps/action-with-pre-and-post/main.js @@ -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' }) diff --git a/pkg/runner/testdata/uses-composite-with-pre-and-post-steps/action-with-pre-and-post/post.js b/pkg/runner/testdata/uses-composite-with-pre-and-post-steps/action-with-pre-and-post/post.js new file mode 100644 index 00000000000..502dfeb8fa2 --- /dev/null +++ b/pkg/runner/testdata/uses-composite-with-pre-and-post-steps/action-with-pre-and-post/post.js @@ -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' }) diff --git a/pkg/runner/testdata/uses-composite-with-pre-and-post-steps/action-with-pre-and-post/pre.js b/pkg/runner/testdata/uses-composite-with-pre-and-post-steps/action-with-pre-and-post/pre.js new file mode 100644 index 00000000000..b17cb69448c --- /dev/null +++ b/pkg/runner/testdata/uses-composite-with-pre-and-post-steps/action-with-pre-and-post/pre.js @@ -0,0 +1 @@ +console.log('pre'); diff --git a/pkg/runner/testdata/uses-composite-with-pre-and-post-steps/composite_action/action.yml b/pkg/runner/testdata/uses-composite-with-pre-and-post-steps/composite_action/action.yml new file mode 100644 index 00000000000..2d12207af66 --- /dev/null +++ b/pkg/runner/testdata/uses-composite-with-pre-and-post-steps/composite_action/action.yml @@ -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 diff --git a/pkg/runner/testdata/uses-composite-with-pre-and-post-steps/last-action/action.yml b/pkg/runner/testdata/uses-composite-with-pre-and-post-steps/last-action/action.yml new file mode 100644 index 00000000000..1ba0fc61eb7 --- /dev/null +++ b/pkg/runner/testdata/uses-composite-with-pre-and-post-steps/last-action/action.yml @@ -0,0 +1,7 @@ +name: "last action check" +description: "last action check" + +runs: + using: "node16" + main: main.js + post: post.js diff --git a/pkg/runner/testdata/uses-composite-with-pre-and-post-steps/last-action/main.js b/pkg/runner/testdata/uses-composite-with-pre-and-post-steps/last-action/main.js new file mode 100644 index 00000000000..e69de29bb2d diff --git a/pkg/runner/testdata/uses-composite-with-pre-and-post-steps/last-action/post.js b/pkg/runner/testdata/uses-composite-with-pre-and-post-steps/last-action/post.js new file mode 100644 index 00000000000..bc6d7edc8c2 --- /dev/null +++ b/pkg/runner/testdata/uses-composite-with-pre-and-post-steps/last-action/post.js @@ -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}'`); +} diff --git a/pkg/runner/testdata/uses-composite-with-pre-and-post-steps/push.yml b/pkg/runner/testdata/uses-composite-with-pre-and-post-steps/push.yml new file mode 100644 index 00000000000..922d38a4b02 --- /dev/null +++ b/pkg/runner/testdata/uses-composite-with-pre-and-post-steps/push.yml @@ -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