Skip to content

Commit

Permalink
add script and using it in pull requests workflow that is used to eva…
Browse files Browse the repository at this point in the history
…luate the result of the jobs that a job depends on (`needs`)
  • Loading branch information
kolorfilm committed Sep 11, 2023
1 parent d43b735 commit 1ced27f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .github/scripts/exit-1-if-result-failure-was-passed.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash

# This script is used to evaluate the result of the jobs that a job depends on (`needs`).
#
# The script expects a string containing the JSON of the `needs` as a parameter. Example invocation:
# `./exit-1-if-result-failure-was-passed.sh "{ e2e_tests: { result: failure, outputs: {} } }"`
# If one of the `needs` had the result "failure", the script exits with 1. Otherwise, it exits with 0.

# Assign the parameters to constants to improve readability.
NEEDS_JSON="$1"

# Echo the passed JSON for easier debugging
echo "Evaluating the following JSON for failures:"
echo "$NEEDS_JSON"

if echo "$NEEDS_JSON" | grep -q "result: failure"; then
echo "One or more jobs seem to have failed."
exit 1
elif echo "$NEEDS_JSON" | grep -q "result: skipped"; then
echo "One or more jobs seem to have been skipped."
exit 1
else
echo "Looks like all jobs succeeded."
exit 0
fi
11 changes: 11 additions & 0 deletions .github/workflows/lint-and-test-and-build-pull-requests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,14 @@ jobs:
lint-and-test-and-build-job:
name: Lint, test and build
uses: ./.github/workflows/reusable-lint-and-test-and-build-job.yml

wait-for-required-jobs:
name: Wait for jobs required for pull requests to be merge-able
runs-on: ubuntu-latest
if: always()
needs: [lint-and-test-and-build-job]
steps:
- name: Checkout the repository so that the Bash script is accessible
uses: actions/checkout@v4
- name: Exit 1 if one of the `needs` was a failure
run: ./.github/scripts/exit-1-if-result-failure-was-passed.sh "${{ toJSON(needs) }}"

0 comments on commit 1ced27f

Please sign in to comment.