From fd78345a9e4f12ea31cb616199bf02c7366e1596 Mon Sep 17 00:00:00 2001 From: rabi-siddique Date: Tue, 30 Jul 2024 15:41:33 +0500 Subject: [PATCH] ci: check dirty git tree for jobs --- .github/actions/restore-node/action.yml | 41 ++++++++++++++--------- .github/actions/with-post-step/action.yml | 21 ++++++++++++ .github/actions/with-post-step/main.cjs | 22 ++++++++++++ 3 files changed, 69 insertions(+), 15 deletions(-) create mode 100644 .github/actions/with-post-step/action.yml create mode 100644 .github/actions/with-post-step/main.cjs diff --git a/.github/actions/restore-node/action.yml b/.github/actions/restore-node/action.yml index c2143659c95..6d7740325b3 100644 --- a/.github/actions/restore-node/action.yml +++ b/.github/actions/restore-node/action.yml @@ -181,18 +181,29 @@ runs: mkdir -p node_modules/.cache/agoric date > node_modules/.cache/agoric/yarn-built - - name: git dirty check - working-directory: ${{ inputs.path }} - shell: bash - run: |- - set -x - # Fail if `git status` reports anything other than the following: - # * an untracked endo-sha.txt from above - # * work tree files that have been staged without further changes - # (e.g., package.json or yarn.lock) as indicated by the Y position - # in "XY PATH" being a space - if [ -n "$(git status --porcelain | grep -vE '^[?][?] endo-sha.txt$|^. '; true)" ]; then - git status - echo "Unexpected dirty git status" 1>&2 - exit 1 - fi + # Refs: https://github.com/orgs/community/discussions/45342 + - name: Validate Git Tree in Root Directory + if: inputs.path == '.' + uses: ./.github/actions/with-post-step + with: + main: echo "Checking Git tree for changes in the root directory..." + post: | + set -x + if [ -n "$(git status --porcelain | grep -vE 'junit.xml$' | grep -vE '^[?][?] endo-sha.txt$|^.'; true)" ]; then + git status + echo "Unexpected dirty git status in default path" 1>&2 + exit 1 + fi + + - name: Validate Git Tree in Agoric SDK Directory + if: inputs.path == './agoric-sdk' + uses: ./agoric-sdk/.github/actions/with-post-step + with: + main: echo "Checking Git tree for changes in the root directory..." + post: | + set -x + if [ -n "$(git status --porcelain | grep -vE 'junit.xml$' | grep -vE '^[?][?] endo-sha.txt$|^.'; true)" ]; then + git status + echo "Unexpected dirty git status in Agoric SDK path" 1>&2 + exit 1 + fi diff --git a/.github/actions/with-post-step/action.yml b/.github/actions/with-post-step/action.yml new file mode 100644 index 00000000000..9e1295380c8 --- /dev/null +++ b/.github/actions/with-post-step/action.yml @@ -0,0 +1,21 @@ +name: With Post Step + +description: > + JavaScript Action to run a main command and a subsequent post command. + +inputs: + main: + description: 'Primary command or script to execute.' + required: true + post: + description: 'Command or script to run after the main command.' + required: true + key: + description: 'State variable name to identify the post step.' + required: false + default: POST + +runs: + using: 'node20' + main: 'main.cjs' + post: 'main.cjs' diff --git a/.github/actions/with-post-step/main.cjs b/.github/actions/with-post-step/main.cjs new file mode 100644 index 00000000000..15390bc4bb4 --- /dev/null +++ b/.github/actions/with-post-step/main.cjs @@ -0,0 +1,22 @@ +// Ref: https://github.com/pyTooling/Actions/blob/main/with-post-step/main.js +const { spawn } = require('child_process'); +const { appendFileSync } = require('fs'); +const { EOL } = require('os'); + +const 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); +}