From 3a85d537b6073322b1a78be46c2bcc59d2c199c1 Mon Sep 17 00:00:00 2001 From: Chris Evich Date: Thu, 20 Oct 2022 10:37:21 -0400 Subject: [PATCH] GHA: Migrate inline script to file Inline scripts make github-action workflow YAML harder to read/maintain. Relocate the e-mail formation script to a dedicated file. This also permits better input-validation and re-use of a common `err()` function. Signed-off-by: Chris Evich --- .../check_cirrus_cron/cron_failures.sh | 8 ++--- .github/actions/check_cirrus_cron/lib.sh | 7 ++++ .../check_cirrus_cron/make_email_body.sh | 35 +++++++++++++++++++ .github/workflows/check_cirrus_cron.yml | 19 +--------- 4 files changed, 45 insertions(+), 24 deletions(-) create mode 100644 .github/actions/check_cirrus_cron/lib.sh create mode 100755 .github/actions/check_cirrus_cron/make_email_body.sh diff --git a/.github/actions/check_cirrus_cron/cron_failures.sh b/.github/actions/check_cirrus_cron/cron_failures.sh index f4dddff8bc..1efe57c145 100755 --- a/.github/actions/check_cirrus_cron/cron_failures.sh +++ b/.github/actions/check_cirrus_cron/cron_failures.sh @@ -5,11 +5,7 @@ set -eo pipefail # Intended to be executed from a github action workflow step. # Outputs the Cirrus cron names and IDs of any failed builds -err() { - # Ref: https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions - echo "::error file=${BASH_SOURCE[0]},line=${BASH_LINENO[0]}::${1:-No error message given}" - exit 1 -} +source $(dirname "${BASH_SOURCE[0]}")/lib.sh _errfmt="Expecting %s value to not be empty" if [[ -z "$GITHUB_REPOSITORY" ]]; then @@ -118,5 +114,5 @@ cat "$NAME_ID_FILEPATH" records=$(wc --words "$NAME_ID_FILEPATH" | cut -d ' ' -f 1) # Always two words per record failures=$((records/2)) -echo "::set-output name=failures::$failures" +echo "failures::$failures" >> $GITHUB_OUTPUT echo "Total failed Cirrus-CI cron builds: $failures" diff --git a/.github/actions/check_cirrus_cron/lib.sh b/.github/actions/check_cirrus_cron/lib.sh new file mode 100644 index 0000000000..1838798dd1 --- /dev/null +++ b/.github/actions/check_cirrus_cron/lib.sh @@ -0,0 +1,7 @@ + +# Must be called from top-level of script, not another function. +err() { + # Ref: https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions + echo "::error file=${BASH_SOURCE[1]},line=${BASH_LINENO[1]}::${1:-No error message given}" + exit 1 +} diff --git a/.github/actions/check_cirrus_cron/make_email_body.sh b/.github/actions/check_cirrus_cron/make_email_body.sh new file mode 100755 index 0000000000..f88803da9f --- /dev/null +++ b/.github/actions/check_cirrus_cron/make_email_body.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +set -eo pipefail + +# Intended to be executed from a github action workflow step. +# Input: File listing space separated failed cron build names and IDs +# Output: $GITHUB_WORKSPACE/artifacts/email_body.txt file + +source $(dirname "${BASH_SOURCE[0]}")/lib.sh + +_errfmt="Expecting %s value to not be empty" +if [[ -z "$GITHUB_REPOSITORY" ]]; then + err $(printf "$_errfmt" "\$GITHUB_REPOSITORY") +elif [[ -z "$GITHUB_WORKFLOW" ]]; then + err $(printf "$_errfmt" "\$GITHUB_WORKFLOW") +elif [[ ! -r "$NAME_ID_FILEPATH" ]]; then + _errfmt="Expecting %s value to be a readable file" + err $(printf "$_errfmt" "\$NAME_ID_FILEPATH") +fi + +mkdir -p artifacts +( + echo "Detected one or more Cirrus-CI cron-triggered jobs have failed recently:" + echo "" + + while read -r NAME BID; do + echo "Cron build '$NAME' Failed: https://cirrus-ci.com/build/$BID" + done < "$NAME_ID_FILEPATH" + + echo "" + echo "# Source: ${GITHUB_WORKFLOW} workflow on ${GITHUB_REPOSITORY}." + # Separate content from sendgrid.com automatic footer. + echo "" + echo "" +) > ./artifacts/email_body.txt diff --git a/.github/workflows/check_cirrus_cron.yml b/.github/workflows/check_cirrus_cron.yml index 51e5c40aab..1162ba2e76 100644 --- a/.github/workflows/check_cirrus_cron.yml +++ b/.github/workflows/check_cirrus_cron.yml @@ -42,24 +42,7 @@ jobs: - if: steps.cron.outputs.failures > 0 shell: bash - # Must be inline, since context expressions are used. - # Ref: https://docs.github.com/en/free-pro-team@latest/actions/reference/context-and-expression-syntax-for-github-actions - run: | - set -eo pipefail - ( - echo "Detected one or more Cirrus-CI cron-triggered jobs have failed recently:" - echo "" - - while read -r NAME BID; do - echo "Cron build '$NAME' Failed: https://cirrus-ci.com/build/$BID" - done < "$NAME_ID_FILEPATH" - - echo "" - echo "# Source: ${{ github.workflow }} workflow on ${{ github.repository }}." - # Separate content from sendgrid.com automatic footer. - echo "" - echo "" - ) > ./artifacts/email_body.txt + run: './.github/actions/check_cirrus_cron/make_email_body' - if: steps.cron.outputs.failures > 0 name: Send failure notification e-mail