-
Notifications
You must be signed in to change notification settings - Fork 0
167 lines (142 loc) · 4.99 KB
/
bash-lint.yml
File metadata and controls
167 lines (142 loc) · 4.99 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
name: Bash Linting
on:
push:
branches:
- main
- master
- develop
paths:
- '**.sh'
- '.github/workflows/bash-lint.yml'
pull_request:
branches:
- main
- master
- develop
paths:
- '**.sh'
workflow_dispatch:
permissions:
contents: read
env:
FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true
jobs:
shellcheck:
name: ShellCheck Linting
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Install ShellCheck
run: |
sudo apt-get update
sudo apt-get install -y shellcheck
- name: Verify ShellCheck installation
run: shellcheck --version
- name: Find all shell scripts
id: find_scripts
run: |
SCRIPTS=$(find . -type f -name "*.sh" ! -path "./.git/*" ! -path "./node_modules/*" ! -path "./vendor/*")
echo "scripts<<EOF" >> "$GITHUB_OUTPUT"
echo "$SCRIPTS" >> "$GITHUB_OUTPUT"
echo "EOF" >> "$GITHUB_OUTPUT"
echo "Script count: $(echo "$SCRIPTS" | wc -l)"
- name: Run ShellCheck on all shell scripts
id: shellcheck
continue-on-error: true
run: |
FAILED=0
while IFS= read -r script; do
if [[ -n "$script" ]]; then
echo "🔍 Linting: $script"
if ! shellcheck -x "$script"; then
FAILED=$((FAILED + 1))
fi
fi
done <<< "${{ steps.find_scripts.outputs.scripts }}"
if [[ $FAILED -gt 0 ]]; then
echo "❌ $FAILED script(s) failed linting"
exit 1
else
echo "✅ All scripts passed linting"
exit 0
fi
- name: Summary report
if: always()
run: |
echo "## Bash Linting Report" >> "$GITHUB_STEP_SUMMARY"
echo "" >> "$GITHUB_STEP_SUMMARY"
echo "**Tool**: ShellCheck" >> "$GITHUB_STEP_SUMMARY"
echo "**Status**: ${{ job.status }}" >> "$GITHUB_STEP_SUMMARY"
echo "" >> "$GITHUB_STEP_SUMMARY"
echo "### Best Practices Applied" >> "$GITHUB_STEP_SUMMARY"
echo "- ✓ Use \`set -e\` for error handling" >> "$GITHUB_STEP_SUMMARY"
echo "- ✓ Quote variables to prevent word splitting" >> "$GITHUB_STEP_SUMMARY"
echo "- ✓ Use \`[[ ]]\` for conditionals" >> "$GITHUB_STEP_SUMMARY"
echo "- ✓ Add meaningful comments" >> "$GITHUB_STEP_SUMMARY"
echo "- ✓ Break scripts into functions" >> "$GITHUB_STEP_SUMMARY"
bash-formatting:
name: Bash Code Quality
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Check for common issues
continue-on-error: true
run: |
echo "🔍 Checking bash script quality..."
ISSUES=0
# Find all shell scripts
while IFS= read -r script; do
if [[ -n "$script" ]]; then
echo ""
echo "📄 Checking: $script"
# Check for set -e
if ! grep -q "set -e" "$script"; then
echo " ⚠️ Consider adding 'set -e' for error handling"
fi
# Check for proper shebang
if ! head -n1 "$script" | grep -q "#!/usr/bin/env bash"; then
echo " ⚠️ Should use '#!/usr/bin/env bash' shebang"
fi
# Check for unquoted variables
if grep -E '\$[A-Za-z_]+[A-Za-z0-9_]*[^"]' "$script" | grep -v '^\s*#' > /dev/null; then
echo " ⚠️ Found potentially unquoted variables"
fi
fi
done < <(find . -type f -name "*.sh" ! -path "./.git/*" ! -path "./node_modules/*" ! -path "./vendor/*")
test-scripts:
name: Run Bash Script Tests
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y git
- name: Test bash scripts (basic syntax)
continue-on-error: true
run: |
echo "🧪 Testing bash script syntax..."
while IFS= read -r script; do
if [[ -n "$script" ]]; then
echo "Testing: $script"
bash -n "$script" || echo " ❌ Syntax error in $script"
fi
done < <(find . -type f -name "*.sh" ! -path "./.git/*" ! -path "./node_modules/*" ! -path "./vendor/*")
report:
name: Linting Report
runs-on: ubuntu-latest
needs: [shellcheck, bash-formatting, test-scripts]
if: always()
steps:
- name: Check workflow status
run: |
if [[ "${{ needs.shellcheck.result }}" == "failure" ]]; then
echo "❌ ShellCheck failed - please fix linting errors"
exit 1
fi
echo "✅ All checks passed!"