diff --git a/internal/fod-login/action.yml b/internal/fod-login/action.yml index 96282f3..82a3275 100644 --- a/internal/fod-login/action.yml +++ b/internal/fod-login/action.yml @@ -10,7 +10,7 @@ runs: - uses: fortify/github-action/internal/run-script@feat-1.3.0 if: ${{ !env._FOD_LOGGED_IN }} with: - cwd: ${{ github.action_path }} + dir: ${{ github.action_path }} script: ./fod-login.sh post: ./fod-logout.sh diff --git a/internal/fod-login/fod-login.sh b/internal/fod-login/fod-login.sh index 98ee373..efe2a02 100755 --- a/internal/fod-login/fod-login.sh +++ b/internal/fod-login/fod-login.sh @@ -1,14 +1,5 @@ #!/bin/bash - -### Start common code -if [ -n "$RUNNER_DEBUG" ]; then - set -v -x -fi -if [ -z "$FCLI_CMD" ]; then - echo "ERROR: fortify/github-action/setup must be run to set up fcli before running this action" - exit 1; -fi -### End common code +. $(UTIL_DIR}/common.sh if [ -z "$FOD_URL" ]; then echo "ERROR: FOD_URL environment variable must be set"; exit 1; diff --git a/internal/fod-login/fod-logout.sh b/internal/fod-login/fod-logout.sh index b93d33f..5180f5f 100755 --- a/internal/fod-login/fod-logout.sh +++ b/internal/fod-login/fod-logout.sh @@ -1,14 +1,5 @@ #!/bin/bash - -### Start common code -if [ -n "$RUNNER_DEBUG" ]; then - set -v -x -fi -if [ -z "$FCLI_CMD" ]; then - echo "ERROR: fortify/github-action/setup must be run to set up fcli before running this action" - exit 1; -fi -### End common code +. $(UTIL_DIR}/common.sh if [[ "${_FOD_LOGGED_IN}" == "true" ]]; then echo '_FOD_LOGGED_IN=false' >> $GITHUB_ENV diff --git a/internal/run-script-js/action.yml b/internal/run-script-js/action.yml new file mode 100644 index 0000000..e50f028 --- /dev/null +++ b/internal/run-script-js/action.yml @@ -0,0 +1,26 @@ +name: JavaScript action to run a script with optional post-job cleanup + +description: 'Action to execute a bash script, optionally executing another script on job completion. This action should not be used directly, but through internal/run-script.' + +inputs: + script: + description: 'Script to run' + required: true + post: + description: 'Script to run on job completion' + required: false + dir: + description: 'Directory where scripts are located, should usually be set to ${{ github.action_path }}' + required: true + util: + description: 'Directory where utility scripts are located, set automatically by internal/run-script action' + required: true + key: + description: 'Name of the state variable used to detect the post step.' + required: false + default: POST + +runs: + using: 'node20' + main: 'main.js' + post: 'main.js' diff --git a/internal/run-script/main.js b/internal/run-script-js/main.js similarity index 70% rename from internal/run-script/main.js rename to internal/run-script-js/main.js index f6b3775..e2cb1ea 100644 --- a/internal/run-script/main.js +++ b/internal/run-script-js/main.js @@ -4,8 +4,10 @@ const { EOL } = require("os"); function run(script) { if ( script ) { - const cwd = process.env.INPUT_CWD || process.cwd; - const subprocess = spawn(`bash -c -o pipefail -v ${script}`, { stdio: "inherit", shell: true, cwd: cwd }); + const dir = process.env.INPUT_DIR; + const utilDir = process.env.INPUT_UTIL; + const subprocess = spawn(`bash -c -o pipefail -v UTIL_DIR=${utilDir} ${dir}/${script}`, + { stdio: "inherit", shell: true, cwd: cwd }); subprocess.on("exit", (exitCode) => { process.exitCode = exitCode; }); diff --git a/internal/run-script/package-lock.json b/internal/run-script-js/package-lock.json similarity index 100% rename from internal/run-script/package-lock.json rename to internal/run-script-js/package-lock.json diff --git a/internal/run-script/action.yml b/internal/run-script/action.yml index 144470c..eeb0b31 100644 --- a/internal/run-script/action.yml +++ b/internal/run-script/action.yml @@ -9,15 +9,16 @@ inputs: post: description: 'Script to run on job completion' required: false - cwd: - description: 'Script working directory' - required: false - key: - description: 'Name of the state variable used to detect the post step.' - required: false - default: POST + dir: + description: 'Directory where scripts are located, should usually be set to ${{ github.action_path }}' + required: true runs: - using: 'node20' - main: 'main.js' - post: 'main.js' + using: composite + steps: + - uses: fortify/github-action/internal/run-script-js@feat-1.3.0 + with: + util: ${{ github.action_path }}/util + dir: ${{ inputs.dir }} + script: ${{ inputs.script }} + post: ${{ inputs.post }} diff --git a/internal/run-script/util/common.sh b/internal/run-script/util/common.sh new file mode 100644 index 0000000..bbf6126 --- /dev/null +++ b/internal/run-script/util/common.sh @@ -0,0 +1,8 @@ +#!/bin/bash +if [ -n "$RUNNER_DEBUG" ]; then + set -v -x +fi +if [ -z "$FCLI_CMD" ]; then + echo "ERROR: fortify/github-action/setup must be run to set up fcli before running this action" + exit 1; +fi \ No newline at end of file diff --git a/internal/sc-sast-login/action.yml b/internal/sc-sast-login/action.yml index e012961..1d6010e 100644 --- a/internal/sc-sast-login/action.yml +++ b/internal/sc-sast-login/action.yml @@ -14,7 +14,7 @@ runs: - uses: fortify/github-action/internal/run-script@feat-1.3.0 if: ${{ !env._SC_SAST_LOGGED_IN }} with: - cwd: ${{ github.action_path }} + dir: ${{ github.action_path }} script: ./sc-sast-login.sh post: ./sc-sast-logout.sh diff --git a/internal/sc-sast-login/sc-sast-login.sh b/internal/sc-sast-login/sc-sast-login.sh index 65200df..f92a470 100755 --- a/internal/sc-sast-login/sc-sast-login.sh +++ b/internal/sc-sast-login/sc-sast-login.sh @@ -1,14 +1,5 @@ #!/bin/bash - -### Start common code -if [ -n "$RUNNER_DEBUG" ]; then - set -v -x -fi -if [ -z "$FCLI_CMD" ]; then - echo "ERROR: fortify/github-action/setup must be run to set up fcli before running this action" - exit 1; -fi -### End common code +. $(UTIL_DIR}/common.sh if [ -z "$SSC_URL" ]; then echo "ERROR: SSC_URL environment variable must be set"; exit 1; diff --git a/internal/sc-sast-login/sc-sast-logout.sh b/internal/sc-sast-login/sc-sast-logout.sh index bdf95fa..6c7ecf2 100755 --- a/internal/sc-sast-login/sc-sast-logout.sh +++ b/internal/sc-sast-login/sc-sast-logout.sh @@ -1,14 +1,5 @@ #!/bin/bash - -### Start common code -if [ -n "$RUNNER_DEBUG" ]; then - set -v -x -fi -if [ -z "$FCLI_CMD" ]; then - echo "ERROR: fortify/github-action/setup must be run to set up fcli before running this action" - exit 1; -fi -### End common code +. $(UTIL_DIR}/common.sh if [[ "${_SC_SAST_LOGGED_IN}" == "true" ]]; then echo '_SC_SAST_LOGGED_IN=false' >> $GITHUB_ENV diff --git a/internal/ssc-login/action.yml b/internal/ssc-login/action.yml index 982c58d..7e5412a 100644 --- a/internal/ssc-login/action.yml +++ b/internal/ssc-login/action.yml @@ -10,7 +10,7 @@ runs: - uses: fortify/github-action/internal/run-script@feat-1.3.0 if: ${{ !env._SSC_LOGGED_IN }} with: - cwd: ${{ github.action_path }} + dir: ${{ github.action_path }} script: ./ssc-login.sh post: ./ssc-logout.sh branding: diff --git a/internal/ssc-login/ssc-login.sh b/internal/ssc-login/ssc-login.sh index 8de7917..596d6a2 100755 --- a/internal/ssc-login/ssc-login.sh +++ b/internal/ssc-login/ssc-login.sh @@ -1,14 +1,5 @@ #!/bin/bash - -### Start common code -if [ -n "$RUNNER_DEBUG" ]; then - set -v -x -fi -if [ -z "$FCLI_CMD" ]; then - echo "ERROR: fortify/github-action/setup must be run to set up fcli before running this action" - exit 1; -fi -### End common code +. $(UTIL_DIR}/common.sh if [ -z "$SSC_URL" ]; then echo "ERROR: SSC_URL environment variable must be set"; exit 1; diff --git a/internal/ssc-login/ssc-logout.sh b/internal/ssc-login/ssc-logout.sh index e2254ed..d8d0c51 100755 --- a/internal/ssc-login/ssc-logout.sh +++ b/internal/ssc-login/ssc-logout.sh @@ -1,14 +1,5 @@ #!/bin/bash - -### Start common code -if [ -n "$RUNNER_DEBUG" ]; then - set -v -x -fi -if [ -z "$FCLI_CMD" ]; then - echo "ERROR: fortify/github-action/setup must be run to set up fcli before running this action" - exit 1; -fi -### End common code +. $(UTIL_DIR}/common.sh if [[ "${_SSC_LOGGED_IN}" == "true" ]]; then echo '_SSC_LOGGED_IN=false' >> $GITHUB_ENV