diff --git a/.github/workflows/test-with-post-step.yaml b/.github/workflows/test-with-post-step.yaml new file mode 100644 index 000000000..bc36f66f5 --- /dev/null +++ b/.github/workflows/test-with-post-step.yaml @@ -0,0 +1,51 @@ +name: Test With Post Step Action + +on: + push: + branches: + - main + paths: + - "actions/with-post-step/**" + - ".github/workflows/test-with-post-step.yaml" + + pull_request: + paths: + - "actions/with-post-step/**" + - ".github/workflows/test-with-post-step.yaml" + types: + - edited + - opened + - ready_for_review + - synchronize + + merge_group: + +permissions: + contents: read + +jobs: + test: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 + with: + persist-credentials: false + + - name: Run first command + id: command + run: | + echo "running firts command" + echo "test_output=test_output_content" >> "${GITHUB_OUTPUT}" + + - name: With post step run + uses: ./actions/with-post-step + with: + main: echo "with-post-step run" + post: | + echo ${{ steps.command.outputs.test_output }} + + - name: Run second command + id: second-command + run: | + echo "running second command" diff --git a/actions/with-post-step/README.md b/actions/with-post-step/README.md new file mode 100644 index 000000000..b9132df5a --- /dev/null +++ b/actions/with-post-step/README.md @@ -0,0 +1,39 @@ +# with-post-step + +Action to set a command as a post step + +Source Code reference: https://github.com/pyTooling/Actions/tree/main/with-post-step + + + +```yaml +name: CI +on: + pull_request: + +jobs: + build: + runs-on: ubuntu-latest + permissions: + contents: read + + steps: + - name: Checkout + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 + with: + persist-credentials: false + + - name: Run Create GitHub App Token action + id: command + run: | + echo "running command" + echo "test_output=test_output_content" >> "${GITHUB_OUTPUT}" + + - name: Skip invalid instance + uses: ./actions/with-post-step + with: + main: echo "with-post-step run" +  post: echo ${{ steps.command.outputs.test_output }} +``` + + diff --git a/actions/with-post-step/action.yaml b/actions/with-post-step/action.yaml new file mode 100644 index 000000000..2b8565494 --- /dev/null +++ b/actions/with-post-step/action.yaml @@ -0,0 +1,22 @@ +# Reference https://github.com/pyTooling/Actions/blob/main/with-post-step/action.yml + +name: With post step + +description: "Generic JS Action to execute a main command and set a command as a post step." + +inputs: + main: + description: "Main command/script." + required: true + post: + description: "Post command/script." + required: true + key: + description: "Name of the state variable used to detect the post step." + required: false + default: POST + +runs: + using: "node20" + main: "main.mjs" + post: "main.mjs" diff --git a/actions/with-post-step/main.mjs b/actions/with-post-step/main.mjs new file mode 100644 index 000000000..477ca9350 --- /dev/null +++ b/actions/with-post-step/main.mjs @@ -0,0 +1,22 @@ +// Reference: https://github.com/pyTooling/Actions/blob/main/with-post-step/main.js +import { spawn } from "node:child_process"; +import { appendFileSync } from "fs"; +import EOL from "os"; + +function run(cmd) { + const subprocess = spawn(cmd, { stdio: "inherit", shell: true }); + subprocess.on("exit", (exitCode) => { + process.exitCode = exitCode; + }); +} + +const key = process.env.INPUT_KEY.toUpperCase(); + +if (process.env[`STATE_${key}`] !== undefined) { + // Are we in the 'post' step? + run(process.env.INPUT_POST); +} else { + // Otherwise, this is the main step + appendFileSync(process.env.GITHUB_STATE, `${key}=true${EOL}`); + run(process.env.INPUT_MAIN); +} diff --git a/eslint.config.mjs b/eslint.config.mjs index 2a69f7246..1511e2883 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -3,6 +3,7 @@ import eslintPluginJest from "eslint-plugin-jest"; import eslintPluginPrettierRecommended from "eslint-plugin-prettier/recommended"; import js from "@eslint/js"; import tseslint from "typescript-eslint"; +import globals from "globals"; export default tseslint.config( js.configs.recommended, @@ -31,6 +32,9 @@ export default tseslint.config( parserOptions: { projectService: true, }, + globals: { + ...globals.node, + }, }, }, { diff --git a/release-please-config.json b/release-please-config.json index 147729b6c..6f9ff5957 100644 --- a/release-please-config.json +++ b/release-please-config.json @@ -164,6 +164,10 @@ "package-name": "cleanup-branches", "extra-files": ["README.md"], "initial-version": "0.1.0" + }, + "actions/with-post-step": { + "package-name": "with-post-step", + "extra-files": ["README.md"] } }, "release-type": "simple",