From 66d857cdd7618cf532f992dc05a3172b1ebc10c8 Mon Sep 17 00:00:00 2001 From: Chris Evich Date: Fri, 4 Nov 2022 12:36:15 -0400 Subject: [PATCH] GHA: Update scripts to pass shellcheck Signed-off-by: Chris Evich --- .../check_cirrus_cron/cron_failures.sh | 5 ++++- .github/actions/check_cirrus_cron/lib.sh | 21 ++++++++++++++++++- .../check_cirrus_cron/make_email_body.sh | 8 +++++-- .../check_cirrus_cron/rerun_failed_tasks.sh | 8 +++++-- 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/.github/actions/check_cirrus_cron/cron_failures.sh b/.github/actions/check_cirrus_cron/cron_failures.sh index 1ed216f5fd..f3a7203dac 100755 --- a/.github/actions/check_cirrus_cron/cron_failures.sh +++ b/.github/actions/check_cirrus_cron/cron_failures.sh @@ -14,7 +14,9 @@ elif [[ -z "$NAME_ID_FILEPATH" ]]; then # output filepath err $(printf "$_errfmt" "\$NAME_ID_FILEPATH") fi -mkdir -p artifacts +confirm_gha_environment + +mkdir -p ./artifacts cat > ./artifacts/query_raw.json << "EOF" query { ownerRepository(platform: "LINUX", owner: "@@OWNER@@", name: "@@REPO@@") { @@ -84,5 +86,6 @@ records=$(wc --words "$NAME_ID_FILEPATH" | cut -d ' ' -f 1) failures=$((records/2)) # Set the output of this step. # Ref: https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-output-parameter +# shellcheck disable=SC2154 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 index 70f08f8099..1b8a4e8a69 100644 --- a/.github/actions/check_cirrus_cron/lib.sh +++ b/.github/actions/check_cirrus_cron/lib.sh @@ -8,10 +8,27 @@ msg() { # 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 - msg "::error file=${BASH_SOURCE[1]},line=${BASH_LINENO[0]}::$@" + msg "::error file=${BASH_SOURCE[1]},line=${BASH_LINENO[0]}::$*" exit 1 } +confirm_gha_environment() { + _errfmt="I don't seem to be running from a github-actions workflow" + # These are all defined by github-actions + # shellcheck disable=SC2154 + if [[ -z "$GITHUB_OUTPUT" ]]; then + err "$_errfmt, \$GITHUB_OUTPUT is empty" + elif [[ -z "$GITHUB_WORKFLOW" ]]; then + err "$_errfmt, \$GITHUB_WORKFLOW is empty" + elif [[ ! -d "$GITHUB_WORKSPACE" ]]; then + # Defined by github-actions + # shellcheck disable=SC2154 + err "$_errfmt, \$GITHUB_WORKSPACE='$GITHUB_WORKSPACE' isn't a directory" + fi + + cd "$GITHUB_WORKSPACE" || false +} + # Using python3 here is a compromise for readability and # properly handling quote, control and unicode character encoding. escape_query() { @@ -45,6 +62,8 @@ gql() { msg "::error file=${BASH_SOURCE[1]},line=${BASH_LINENO[0]}::Invalid query JSON: $query" return 1 fi + # SECRET_CIRRUS_API_KEY is defined github secret + # shellcheck disable=SC2154 if output=$(curl \ --request POST \ --silent \ diff --git a/.github/actions/check_cirrus_cron/make_email_body.sh b/.github/actions/check_cirrus_cron/make_email_body.sh index ab5a717eb0..af38566286 100755 --- a/.github/actions/check_cirrus_cron/make_email_body.sh +++ b/.github/actions/check_cirrus_cron/make_email_body.sh @@ -9,14 +9,16 @@ set -eo pipefail source $(dirname "${BASH_SOURCE[0]}")/lib.sh _errfmt="Expecting %s value to not be empty" +# NAME_ID_FILEPATH is defined by workflow YAML +# shellcheck disable=SC2154 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 err "Expecting \$NAME_ID_FILEPATH value ($NAME_ID_FILEPATH) to be a readable file" fi +confirm_gha_environment + mkdir -p artifacts ( echo "Detected one or more Cirrus-CI cron-triggered jobs have failed recently:" @@ -27,6 +29,8 @@ mkdir -p artifacts done < "$NAME_ID_FILEPATH" echo "" + # Defined by github-actions + # shellcheck disable=SC2154 echo "# Source: ${GITHUB_WORKFLOW} workflow on ${GITHUB_REPOSITORY}." # Separate content from sendgrid.com automatic footer. echo "" diff --git a/.github/actions/check_cirrus_cron/rerun_failed_tasks.sh b/.github/actions/check_cirrus_cron/rerun_failed_tasks.sh index 8432815d03..33baa9da95 100755 --- a/.github/actions/check_cirrus_cron/rerun_failed_tasks.sh +++ b/.github/actions/check_cirrus_cron/rerun_failed_tasks.sh @@ -25,12 +25,16 @@ set -eo pipefail source $(dirname "${BASH_SOURCE[0]}")/lib.sh _errfmt="Expecting %s value to not be empty" +# NAME_ID_FILEPATH is defined by workflow YAML +# shellcheck disable=SC2154 if [[ -z "$SECRET_CIRRUS_API_KEY" ]]; then err $(printf "$_errfmt" "\$SECRET_CIRRUS_API_KEY") elif [[ ! -r "$NAME_ID_FILEPATH" ]]; then # output from cron_failures.sh err $(printf "Expecting %s value to be a readable file" "\$NAME_ID_FILEPATH") fi +confirm_gha_environment + mkdir -p artifacts # If there are no tasks, don't fail reading the file truncate -s 0 ./artifacts/rerun_tids.txt @@ -92,7 +96,7 @@ cat "$NAME_ID_FILEPATH" | \ # Check-value returned if the gql call was successful canary=$(uuidgen) # Ensure the trailing ',' is stripped from the end (would be invalid JSON) - task_ids=$(printf '[%s]' $(printf '"%s",' ${rerun_tasks[@]} | head -c -1)) + task_ids=$(printf '[%s]' $(printf '"%s",' ${rerun_tasks[*]} | head -c -1)) rerun_m=" mutation { batchReRun(input: { @@ -107,6 +111,6 @@ cat "$NAME_ID_FILEPATH" | \ filter='.data.batchReRun.clientMutationId' result=$(gql "$rerun_m" "$filter") if [[ $(jq -r -e "$filter"<<<"$result") != "$canary" ]]; then - err "Attempt to re-run tasks for build $BID failed: ${rerun_tasks[@]}" + err "Attempt to re-run tasks for build $BID failed: ${rerun_tasks[*]}" fi done