Skip to content

Commit 13f0c4e

Browse files
authored
Add shellcheck for shell script linting (#991)
* Add .shellcheckrc This adds a config file for .shellcheckrc, and sets a few optional flags to try to make shellcheck slightly more thorough. * Add shellcheck problem matcher * Add a heading to help break up the text a little * Add shellcheck to CI checks
1 parent 85830b0 commit 13f0c4e

File tree

4 files changed

+77
-1
lines changed

4 files changed

+77
-1
lines changed

.github/problem-matchers/README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ prominently in the GitHub user interface.
1010
This directory contains Problem Matchers used by the GitHub Actions workflows
1111
in the [`workflows`](./workflows) subdirectory.
1212

13+
## Original sources
14+
1315
The following problem matcher JSON files found in this directory were copied
1416
from the [Home Assistant](https://github.com/home-assistant/core) project on
1517
GitHub. The Home Assistant project is licensed under the Apache 2.0 open-source
@@ -28,4 +30,10 @@ The following problem matcher JSON file came from the
2830
[hadolint-action](https://github.com/hadolint/hadolint-action) repository
2931
(copied on 2025-02-17, version 3.1.0):
3032

31-
- [`problem-matcher.json`](https://github.com/hadolint/hadolint-action/blob/master/problem-matcher.json)
33+
- [`hadolint.json`](https://github.com/hadolint/hadolint-action/blob/master/problem-matcher.json)
34+
35+
The Shellcheck problem matcher JSON file came from the
36+
[shellcheck-problem-matchers](uhttps://github.com/lumaxis/shellcheck-problem-matchers)
37+
repository (copied 2025-02-26, version v2.1.0).
38+
39+
- [`shellcheck-tty.json`](https://github.com/lumaxis/shellcheck-problem-matchers/blob/main/.github/shellcheck-tty.json)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"problemMatcher": [
3+
{
4+
"owner": "shellcheck",
5+
"pattern": [
6+
{
7+
"regexp": "^In\\s(.+)\\sline\\s(\\d+):$",
8+
"file": 1,
9+
"line": 2
10+
},
11+
{
12+
"regexp": ".*"
13+
},
14+
{
15+
"regexp": "SC(\\d+)(\\s\\((note|warning|error)\\))?:\\s(.+)$",
16+
"code": 1,
17+
"severity": 3,
18+
"message": 4,
19+
"loop": true
20+
}
21+
]
22+
}
23+
]
24+
}

.github/workflows/ci.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ jobs:
9191
json_files: ${{steps.filter.outputs.json_files}}
9292
docker: ${{steps.filter.outputs.docker}}
9393
docker_files: ${{steps.filter.outputs.docker_files}}
94+
shell: ${{steps.filter.outputs.shell}}
95+
shell_files: ${{steps.filter.outputs.shell_files}}
9496
steps:
9597
# When invoked manually, use the given SHA to figure out the change list.
9698
- if: github.event_name == 'workflow_dispatch'
@@ -146,6 +148,9 @@ jobs:
146148
docker:
147149
- '**/dockerfile'
148150
- '**/Dockerfile'
151+
shell:
152+
- '**/*.sh'
153+
- 'check/*'
149154
150155
setup:
151156
if: needs.changes.outputs.python == 'true'
@@ -518,3 +523,23 @@ jobs:
518523
- name: Verify that all GitHub Actions workflows are valid
519524
run: |
520525
/opt/homebrew/bin/actionlint -color
526+
527+
shell-script-lint:
528+
if: needs.changes.outputs.shell == 'true'
529+
name: Shell script checks
530+
needs: changes
531+
runs-on: ubuntu-24.04
532+
timeout-minutes: 5
533+
env:
534+
changed_files: ${{needs.changes.outputs.shell_files}}
535+
steps:
536+
- name: Check out a copy of the git repository
537+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
538+
539+
- name: Set up shellcheck output problem matcher
540+
run: |
541+
echo "::add-matcher::.github/problem-matchers/shellcheck.json"
542+
543+
- name: Run shellcheck on shell scripts that have been changed
544+
run: |
545+
shellcheck ${{env.changed_files}}

.shellcheckrc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Summary: config file for shellcheck program.
2+
#
3+
# The following page includes information about the .shellcheckrc file:
4+
# https://github.com/koalaman/shellcheck/wiki/Directive#shellcheckrc-file
5+
#
6+
# Optional settings can be discovered by running "shellcheck --list-optional".
7+
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8+
9+
# We use bash for all the scripts, so tell shellcheck to assume this dialect.
10+
shell=bash
11+
12+
# Makes shellcheck include files pointed-to by the source or . statements.
13+
external-sources=true
14+
15+
# Enable check for when a script uses "set -e" but a construct may disable it.
16+
enable=check-set-e-suppressed
17+
18+
# Enable check for tests like [ "$var" ], which are best written [ -n "$var" ].
19+
enable=avoid-nullary-conditions

0 commit comments

Comments
 (0)