Repository navigation
109 lines (100 loc) · 4.01 KB
/
Copy pathlint-shellcheck.yaml
File metadata and controls
109 lines (100 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
---
# yamllint disable rule:line-length
name: shellcheck
on:
workflow_call:
inputs:
git_ref:
required: true
type: string
files:
required: false
type: string
default: 'ALL'
mise_ignore_cfg:
required: false
type: string
default: ""
mise_log_level:
required: false
type: string
default: "warn"
env:
# renovate: datasource=github-releases depName=koalaman/shellcheck
SHELLCHECK_VERSION: "v0.11.0"
jobs:
shellcheck:
runs-on: ubuntu-26.04
timeout-minutes: 5
steps:
- name: Setup repository and tools
uses: ppat/homelab-ops-actions/actions/setup-repository-tools@b243512e70e84044d607787b1f482881906b0f01 # v3.0.0
with:
current_git_ref: ${{ inputs.git_ref }}
current_repository: ${{ github.repository }}
mise_ignore_cfg: ${{ inputs.mise_ignore_cfg }}
mise_log_level: ${{ inputs.mise_log_level }}
token: ${{ secrets.GITHUB_TOKEN }}
mise_toml: |
[tools]
shellcheck = "${{ env.SHELLCHECK_VERSION }}"
- name: Lint shell scripts
env:
SHELLSCRIPT_FILES: ${{ inputs.files }}
shell: bash
working-directory: ./current
# yamllint disable-line rule:indentation
run: |
shellcheck_files=()
if [[ "${SHELLSCRIPT_FILES}" == "ALL" ]]; then
# The executable bit alone does not mean "shell script": executable python/node
# scripts carry it too, and get rejected outright with SC1071. So keep only the
# files that declare themselves shell -- either a shebang naming one of the
# supported shells (sh/bash/dash/ksh), or a .sh/.bash extension. A file that
# declares neither cannot be linted anyway, since no dialect can be inferred for
# it (SC2148); the check-executables-have-shebangs pre-commit hook is what keeps
# shebang-less executables from being committed in the first place.
while IFS= read -r -d '' file; do
case "${file}" in
*.sh | *.bash)
shellcheck_files+=("${file}")
continue
;;
esac
read -r first_line < "${file}" || true
if [[ "${first_line}" =~ ^#!.*[\ /](sh|bash|dash|ksh)([[:space:]]|$) ]]; then
shellcheck_files+=("${file}")
fi
done < <(find . -type f -not -path './node_modules/*' -not -path './.git/*' -not -path ./bun.lock -executable -print0)
else
# An explicit caller-supplied list: the caller already decided what counts as a
# shell script here, so pass it through unfiltered rather than second-guessing
# it. Consumers legitimately list sourced fragments that have no shebang at all.
# Deliberate word splitting: the caller delimits its list with whitespace.
# shellcheck disable=SC2206
shellcheck_files=(${SHELLSCRIPT_FILES})
fi
if [[ "${#shellcheck_files[@]}" -eq 0 ]]; then
echo "shellcheck: no shell scripts selected -- nothing was checked"
exit 0
fi
# One invocation per file, each with its own PASS/FAIL line, so the log says what
# was checked instead of only what was wrong. A silent run and a run that checked
# nothing are no longer indistinguishable. Findings are printed under the FAIL line
# of the file they belong to, and every file is checked before the job gives up, so
# one bad script does not hide the rest.
failed=0
for file in "${shellcheck_files[@]}"; do
if output=$(shellcheck --rcfile .shellcheckrc "${file}" 2>&1); then
printf 'PASS %s\n' "${file}"
else
printf 'FAIL %s\n' "${file}"
printf '%s\n' "${output}"
failed=$((failed + 1))
fi
done
echo
echo "shellcheck: ${#shellcheck_files[@]} file(s) checked, ${failed} failed"
if [[ "${failed}" -gt 0 ]]; then
exit 1
fi