Skip to content

Commit

Permalink
Add shell linter Github action for pull requests (#1007)
Browse files Browse the repository at this point in the history
Add Github CI to enforce shell script standards.
Refs: #397
  • Loading branch information
WalterKolczynski-NOAA committed Aug 31, 2022
1 parent 392ca6f commit 855ee86
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 13 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/linters.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#
name: linters
on:
pull_request:

permissions:
contents: read

defaults:
run:
shell: bash -o pipefail {0}

jobs:
lint:
runs-on: ubuntu-latest

permissions:
security-events: write
pull-requests: write

steps:
- name: Checkout code
uses: actions/checkout@v1
with:
fetch-depth: 0

- name: Lint shell scripts
uses: redhat-plumbers-in-action/differential-shellcheck@latest
with:
token: ${{ secrets.GITHUB_TOKEN }}
shell-scripts: ush/preamble.sh
2 changes: 2 additions & 0 deletions .shellcheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Global settings for Spellcheck (https://github.com/koalaman/shellcheck)
enable=all
31 changes: 18 additions & 13 deletions ush/preamble.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,26 @@ fi
start_time=$(date +%s)

# Get the base name of the calling script
_calling_script=$(basename ${BASH_SOURCE[1]})
_calling_script=$(basename "${BASH_SOURCE[1]}")

# Announce the script has begun
echo "Begin ${_calling_script} at $(date -u)"
start_time_human=$(date -d"@${start_time}" -u)
echo "Begin ${_calling_script} at ${start_time_human}"

# Stage our variables
export STRICT=${STRICT:-"YES"}
export TRACE=${TRACE:-"YES"}
export ERR_EXIT_ON=""
export TRACE_ON=""

if [[ $STRICT == "YES" ]]; then
if [[ ${STRICT} == "YES" ]]; then
# Exit on error and undefined variable
export ERR_EXIT_ON="set -eu"
fi
if [[ $TRACE == "YES" ]]; then
if [[ ${TRACE} == "YES" ]]; then
export TRACE_ON="set -x"
# Print the script name and line number of each command as it is executed
export PS4='+ $(basename $BASH_SOURCE)[$LINENO]'"$id: "
export PS4='+ $(basename ${BASH_SOURCE})[${LINENO}]'"${id}: "
fi

postamble() {
Expand All @@ -64,23 +65,27 @@ postamble() {
#

set +x
script=${1}
start_time=${2}
rc=${3}
script="${1}"
start_time="${2}"
rc="${3}"

# Calculate the elapsed time
end_time=$(date +%s)
end_time_human=$(date -d@"${end_time}" -u +%H:%M:%S)
elapsed_sec=$((end_time - start_time))
elapsed=$(date -d@${elapsed_sec} -u +%H:%M:%S)
elapsed=$(date -d@"${elapsed_sec}" -u +%H:%M:%S)

# Announce the script has ended, then pass the error code up
echo "End ${script} at $(date -u) with error code ${rc:-0} (time elapsed: ${elapsed})"
exit ${rc}
echo "End ${script} at ${end_time_human} with error code ${rc:-0} (time elapsed: ${elapsed})"
exit "${rc}"
}

# Place the postamble in a trap so it is always called no matter how the script exits
# Shellcheck: Turn off warning about substitions at runtime instead of signal time
# shellcheck disable=SC2064
trap "postamble ${_calling_script} ${start_time} \$?" EXIT
# shellcheck disable=

# Turn on our settings
$ERR_EXIT_ON
$TRACE_ON
${ERR_EXIT_ON}
${TRACE_ON}

0 comments on commit 855ee86

Please sign in to comment.