Skip to content

Commit 4906d5a

Browse files
committed
nox: add actionlint to lint Github Actions workflows (#1848)
(cherry picked from commit 3b43d64)
1 parent caeeeba commit 4906d5a

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

.github/workflows/reusable-nox.yml

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ jobs:
2323
python-versions: "3.11"
2424
- session: "checkers(docs-build)"
2525
python-versions: "3.11"
26+
- session: "actionlint"
27+
python-versions: "3.11"
2628
name: "Run nox ${{ matrix.session }} session"
2729
steps:
2830
- name: Check out repo

.github/workflows/reusable-pip-compile.yml

+2
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ jobs:
114114
run: |
115115
set -x
116116
git diff || :
117+
# shellcheck disable=SC2086
117118
git add ${changed_files}
119+
# shellcheck disable=SC2086
118120
if git diff-index --quiet HEAD ${changed_files}; then
119121
echo "Nothing to do!"
120122
exit

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ The `nox` configuration also contains session to run automated docs checkers.
7979
nox -s lint
8080
```
8181

82+
The `actionlint` linter that is run as part of the `lint` session requires
83+
`podman` or `docker` to be installed.
84+
If both container engines are installed, `podman` is preferred.
85+
Set `CONTAINER_ENGINE=docker` to change this behavior.
86+
8287
### Checking spelling
8388

8489
Use [`codespell`](https://github.com/codespell-project/codespell) to check for common spelling mistakes in the documentation source.

noxfile.py

+47
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import os
44
import shlex
5+
import shutil
56
from argparse import ArgumentParser, BooleanOptionalAction
67
from glob import iglob
78
from pathlib import Path
@@ -44,6 +45,29 @@ def install(session: nox.Session, *args, req: str, **kwargs):
4445
session.install("-r", f"tests/{req}.in", *args, **kwargs)
4546

4647

48+
CONTAINER_ENGINES = ("podman", "docker")
49+
CHOSEN_CONTAINER_ENGINE = os.environ.get("CONTAINER_ENGINE")
50+
ACTIONLINT_IMAGE = "docker.io/rhysd/actionlint"
51+
52+
53+
def _get_container_engine(session: nox.Session) -> str:
54+
path: str | None = None
55+
if CHOSEN_CONTAINER_ENGINE:
56+
path = shutil.which(CHOSEN_CONTAINER_ENGINE)
57+
if not path:
58+
session.error(
59+
f"CONTAINER_ENGINE {CHOSEN_CONTAINER_ENGINE!r} does not exist!"
60+
)
61+
return path
62+
for engine in CONTAINER_ENGINES:
63+
if path := shutil.which(engine):
64+
return path
65+
session.error(
66+
f"None of the following container engines were found: {CONTAINER_ENGINES}."
67+
f" {session.name} requires a container engine installed."
68+
)
69+
70+
4771
@nox.session
4872
def static(session: nox.Session):
4973
"""
@@ -92,12 +116,35 @@ def spelling(session: nox.Session):
92116
)
93117

94118

119+
@nox.session
120+
def actionlint(session: nox.Session) -> None:
121+
"""
122+
Run actionlint to lint Github Actions workflows.
123+
The actionlint tool is run in a Podman/Docker container.
124+
"""
125+
engine = _get_container_engine(session)
126+
session.run_always(engine, "pull", ACTIONLINT_IMAGE, external=True)
127+
session.run(
128+
engine,
129+
"run",
130+
"--rm",
131+
# fmt: off
132+
"--volume", f"{Path.cwd()}:/pwd:z",
133+
"--workdir", "/pwd",
134+
# fmt: on
135+
ACTIONLINT_IMAGE,
136+
*session.posargs,
137+
external=True,
138+
)
139+
140+
95141
@nox.session
96142
def lint(session: nox.Session):
97143
session.notify("typing")
98144
session.notify("static")
99145
session.notify("formatters")
100146
session.notify("spelling")
147+
session.notify("actionlint")
101148

102149

103150
requirements_files = list(

0 commit comments

Comments
 (0)