Skip to content

Commit

Permalink
chore: Refactor internal/run-script action
Browse files Browse the repository at this point in the history
  • Loading branch information
rsenden committed Jun 3, 2024
1 parent dd2ca06 commit 8b21836
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 75 deletions.
2 changes: 1 addition & 1 deletion internal/fod-login/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ runs:
- uses: fortify/github-action/internal/[email protected]
if: ${{ !env._FOD_LOGGED_IN }}
with:
cwd: ${{ github.action_path }}
dir: ${{ github.action_path }}
script: ./fod-login.sh
post: ./fod-logout.sh

Expand Down
11 changes: 1 addition & 10 deletions internal/fod-login/fod-login.sh
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
11 changes: 1 addition & 10 deletions internal/fod-login/fod-logout.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down
26 changes: 26 additions & 0 deletions internal/run-script-js/action.yml
Original file line number Diff line number Diff line change
@@ -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'
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down
File renamed without changes.
21 changes: 11 additions & 10 deletions internal/run-script/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]
with:
util: ${{ github.action_path }}/util
dir: ${{ inputs.dir }}
script: ${{ inputs.script }}
post: ${{ inputs.post }}
8 changes: 8 additions & 0 deletions internal/run-script/util/common.sh
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion internal/sc-sast-login/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ runs:
- uses: fortify/github-action/internal/[email protected]
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

Expand Down
11 changes: 1 addition & 10 deletions internal/sc-sast-login/sc-sast-login.sh
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
11 changes: 1 addition & 10 deletions internal/sc-sast-login/sc-sast-logout.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/ssc-login/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ runs:
- uses: fortify/github-action/internal/[email protected]
if: ${{ !env._SSC_LOGGED_IN }}
with:
cwd: ${{ github.action_path }}
dir: ${{ github.action_path }}
script: ./ssc-login.sh
post: ./ssc-logout.sh
branding:
Expand Down
11 changes: 1 addition & 10 deletions internal/ssc-login/ssc-login.sh
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
11 changes: 1 addition & 10 deletions internal/ssc-login/ssc-logout.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 8b21836

Please sign in to comment.