GHA: Update scripts to pass shellcheck

Signed-off-by: Chris Evich <cevich@redhat.com>
This commit is contained in:
Chris Evich
2022-11-04 12:36:15 -04:00
parent d17b7d852a
commit 66d857cdd7
4 changed files with 36 additions and 6 deletions

View File

@ -14,7 +14,9 @@ elif [[ -z "$NAME_ID_FILEPATH" ]]; then # output filepath
err $(printf "$_errfmt" "\$NAME_ID_FILEPATH") err $(printf "$_errfmt" "\$NAME_ID_FILEPATH")
fi fi
mkdir -p artifacts confirm_gha_environment
mkdir -p ./artifacts
cat > ./artifacts/query_raw.json << "EOF" cat > ./artifacts/query_raw.json << "EOF"
query { query {
ownerRepository(platform: "LINUX", owner: "@@OWNER@@", name: "@@REPO@@") { ownerRepository(platform: "LINUX", owner: "@@OWNER@@", name: "@@REPO@@") {
@ -84,5 +86,6 @@ records=$(wc --words "$NAME_ID_FILEPATH" | cut -d ' ' -f 1)
failures=$((records/2)) failures=$((records/2))
# Set the output of this step. # Set the output of this step.
# Ref: https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-output-parameter # 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 "failures::$failures" >> $GITHUB_OUTPUT
echo "Total failed Cirrus-CI cron builds: $failures" echo "Total failed Cirrus-CI cron builds: $failures"

View File

@ -8,10 +8,27 @@ msg() {
# Must be called from top-level of script, not another function. # Must be called from top-level of script, not another function.
err() { err() {
# Ref: https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions # 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 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 # Using python3 here is a compromise for readability and
# properly handling quote, control and unicode character encoding. # properly handling quote, control and unicode character encoding.
escape_query() { escape_query() {
@ -45,6 +62,8 @@ gql() {
msg "::error file=${BASH_SOURCE[1]},line=${BASH_LINENO[0]}::Invalid query JSON: $query" msg "::error file=${BASH_SOURCE[1]},line=${BASH_LINENO[0]}::Invalid query JSON: $query"
return 1 return 1
fi fi
# SECRET_CIRRUS_API_KEY is defined github secret
# shellcheck disable=SC2154
if output=$(curl \ if output=$(curl \
--request POST \ --request POST \
--silent \ --silent \

View File

@ -9,14 +9,16 @@ set -eo pipefail
source $(dirname "${BASH_SOURCE[0]}")/lib.sh source $(dirname "${BASH_SOURCE[0]}")/lib.sh
_errfmt="Expecting %s value to not be empty" _errfmt="Expecting %s value to not be empty"
# NAME_ID_FILEPATH is defined by workflow YAML
# shellcheck disable=SC2154
if [[ -z "$GITHUB_REPOSITORY" ]]; then if [[ -z "$GITHUB_REPOSITORY" ]]; then
err $(printf "$_errfmt" "\$GITHUB_REPOSITORY") err $(printf "$_errfmt" "\$GITHUB_REPOSITORY")
elif [[ -z "$GITHUB_WORKFLOW" ]]; then
err $(printf "$_errfmt" "\$GITHUB_WORKFLOW")
elif [[ ! -r "$NAME_ID_FILEPATH" ]]; then elif [[ ! -r "$NAME_ID_FILEPATH" ]]; then
err "Expecting \$NAME_ID_FILEPATH value ($NAME_ID_FILEPATH) to be a readable file" err "Expecting \$NAME_ID_FILEPATH value ($NAME_ID_FILEPATH) to be a readable file"
fi fi
confirm_gha_environment
mkdir -p artifacts mkdir -p artifacts
( (
echo "Detected one or more Cirrus-CI cron-triggered jobs have failed recently:" echo "Detected one or more Cirrus-CI cron-triggered jobs have failed recently:"
@ -27,6 +29,8 @@ mkdir -p artifacts
done < "$NAME_ID_FILEPATH" done < "$NAME_ID_FILEPATH"
echo "" echo ""
# Defined by github-actions
# shellcheck disable=SC2154
echo "# Source: ${GITHUB_WORKFLOW} workflow on ${GITHUB_REPOSITORY}." echo "# Source: ${GITHUB_WORKFLOW} workflow on ${GITHUB_REPOSITORY}."
# Separate content from sendgrid.com automatic footer. # Separate content from sendgrid.com automatic footer.
echo "" echo ""

View File

@ -25,12 +25,16 @@ set -eo pipefail
source $(dirname "${BASH_SOURCE[0]}")/lib.sh source $(dirname "${BASH_SOURCE[0]}")/lib.sh
_errfmt="Expecting %s value to not be empty" _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 if [[ -z "$SECRET_CIRRUS_API_KEY" ]]; then
err $(printf "$_errfmt" "\$SECRET_CIRRUS_API_KEY") err $(printf "$_errfmt" "\$SECRET_CIRRUS_API_KEY")
elif [[ ! -r "$NAME_ID_FILEPATH" ]]; then # output from cron_failures.sh elif [[ ! -r "$NAME_ID_FILEPATH" ]]; then # output from cron_failures.sh
err $(printf "Expecting %s value to be a readable file" "\$NAME_ID_FILEPATH") err $(printf "Expecting %s value to be a readable file" "\$NAME_ID_FILEPATH")
fi fi
confirm_gha_environment
mkdir -p artifacts mkdir -p artifacts
# If there are no tasks, don't fail reading the file # If there are no tasks, don't fail reading the file
truncate -s 0 ./artifacts/rerun_tids.txt truncate -s 0 ./artifacts/rerun_tids.txt
@ -92,7 +96,7 @@ cat "$NAME_ID_FILEPATH" | \
# Check-value returned if the gql call was successful # Check-value returned if the gql call was successful
canary=$(uuidgen) canary=$(uuidgen)
# Ensure the trailing ',' is stripped from the end (would be invalid JSON) # 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=" rerun_m="
mutation { mutation {
batchReRun(input: { batchReRun(input: {
@ -107,6 +111,6 @@ cat "$NAME_ID_FILEPATH" | \
filter='.data.batchReRun.clientMutationId' filter='.data.batchReRun.clientMutationId'
result=$(gql "$rerun_m" "$filter") result=$(gql "$rerun_m" "$filter")
if [[ $(jq -r -e "$filter"<<<"$result") != "$canary" ]]; then 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 fi
done done