Skip to content

Commit

Permalink
gh-actions/github/run: Improve err handling
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Northey <ryan@synca.io>
  • Loading branch information
phlax committed Nov 9, 2023
1 parent edad9e7 commit 9104961
Showing 1 changed file with 63 additions and 18 deletions.
81 changes: 63 additions & 18 deletions gh-actions/github/run/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,21 @@ runs:
name: ${{ inputs.steps-pre-name }}
steps: ${{ inputs.steps-pre }}
context: ${{ inputs.context }}

- run: |
if [[ "${#INPUT_ENV}" -ne 0 ]]; then
SOURCETMP="$(mktemp)"
# TODO(phlax): Fix escaping
printf "%s" '${{ inputs.source }}' > "$SOURCETMP"
. "$SOURCETMP"
rm -rf "$SOURCETMP"
fi
echo "file=${SOURCETMP}" >> $GITHUB_OUTPUT
if: ${{ inputs.source }}
id: source
env:
INPUT_ENV: ${{ inputs.source }}
shell: bash
- run: |
COMMAND=()
read -ra ARGS <<< "${{ inputs.args }}"
ACTUAL_COMMAND=(
Expand All @@ -117,16 +124,42 @@ runs:
COMMAND+=(${{ inputs.container-command }})
fi
COMMAND+=("${ACTUAL_COMMAND[@]}")
TMP_OUTPUT=$(mktemp)
if [[ -n "$RUNNER_DEBUG" || -n "$CI_DEBUG" ]]; then
echo "DEBUG: RUN ${COMMAND[*]}" >&2
echo "command=${COMMAND[*]}" >> $GITHUB_OUTPUT
id: command
shell: bash
- run: |
echo "DEBUG: RUN ${COMMAND}" >&2
if [[ -e "$SOURCETMP" ]]; then
cat $SOURCETMP
fi
"${COMMAND[@]}" 2> >(tee "$TMP_OUTPUT") || {
FAILED=true
}
OUTPUT="$(cat "$TMP_OUTPUT")"
rm -rf "$TMP_OUTPUT"
if: ${{ env.CI_DEBUG || env.RUNNER_DEBUG }}
env:
COMMAND: ${{ steps.command.outputs.command }}
SOURCETMP: ${{ steps.source.outputs.file }}
shell: bash
- run: |
if [[ -n "${SOURCETMP}" ]]; then
. "$SOURCETMP"
rm -rf "$SOURCETMP"
fi
TMP_STDERR=$(mktemp)
echo "stderr-path=${TMP_STDERR}" >> $GITHUB_OUTPUT
$COMMAND 2> >(tee "$TMP_STDERR") || {
echo "exit-code=$?" >> $GITHUB_OUTPUT
exit 0
}
echo "exit-code=0" >> $GITHUB_OUTPUT
shell: bash
id: run
env:
COMMAND: ${{ steps.command.outputs.command }}
SOURCETMP: ${{ steps.source.outputs.file }}
- run: |
STDERR="$(cat "$TMP_STDERR")"
rm -rf "$TMP_STDERR"
bubble_messages () {
local message_type="$1" \
matcher="$2" \
Expand All @@ -137,7 +170,7 @@ runs:
done <<< "$matcher"
declare -a issued_warnings
for match in "${matches[@]}"; do
matched="$(echo "$OUTPUT" | grep "$match" || :)"
matched="$(echo "${STDERR}" | grep "$match" || :)"
if [[ -z "$matched" ]]; then
continue
fi
Expand All @@ -160,25 +193,32 @@ runs:
bubble_messages error "${{ inputs.error-match }}"
bubble_messages warning "${{ inputs.warning-match }}"
bubble_messages notice "${{ inputs.notice-match }}"
if [[ -n "$FAILED" && "${{ inputs.catch-errors }}" != "true" ]]; then
exit 1
fi
shell: bash
env:
INPUT_ENV: ${{ inputs.source }}
TMP_STDERR: ${{ steps.run.outputs.stderr-path }}
- uses: envoyproxy/toolshed/gh-actions/jq@actions-v0.1.30
name: Generate run context
id: context
with:
input: ${{ toJSON(steps.run.outputs) }}
filter: >-
{"run": .,
"context": ${{ inputs.context }}}
- uses: envoyproxy/toolshed/gh-actions/using/steps@actions-v0.1.30
name: Run post steps
if: ${{ inputs.steps-post }}
with:
name: ${{ inputs.steps-post-name }}
steps: ${{ inputs.steps-post }}
context: ${{ inputs.context }}
context: ${{ steps.context.outputs.value }}
- uses: envoyproxy/toolshed/gh-actions/using/steps@actions-v0.1.30
name: Post report
if: ${{ inputs.report-post }}
with:
steps: ${{ inputs.report-post }}
context: ${{ inputs.context }}
context: ${{ steps.context.outputs.value }}
- uses: actions/upload-artifact@v3
name: Upload artefacts
if: ${{ inputs.upload-name && inputs.upload-path }}
Expand All @@ -190,4 +230,9 @@ runs:
if: ${{ inputs.summary-post }}
with:
steps: ${{ inputs.summary-post }}
context: ${{ inputs.context }}
context: ${{ steps.context.outputs.value }}
- run: |
exit ${EXIT_CODE}
shell: bash
env:
EXIT_CODE: ${{ !fromJSON(inputs.catch-errors) && steps.run.outputs.exit-code || 0 }}

0 comments on commit 9104961

Please sign in to comment.