Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable-2.16] nox: add actionlint to lint Github Actions workflows (#1848) #1919

Draft
wants to merge 2 commits into
base: stable-2.16
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/reusable-nox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ jobs:
python-versions: "3.11"
- session: "checkers(docs-build)"
python-versions: "3.11"
- session: "actionlint"
python-versions: "3.11"
name: "Run nox ${{ matrix.session }} session"
steps:
- name: Check out repo
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/reusable-pip-compile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,8 @@ jobs:
# Ensure the latest pip version is used
VIRTUALENV_DOWNLOAD: '1'
#
nox_args: "${{ inputs.nox-args }}"
run: |
nox ${nox_args}
nox ${{ inputs.nox-args }}
- name: Push new dependency versions and create a PR
env:
GITHUB_TOKEN: ${{ steps.create_token.outputs.token }}
Expand All @@ -115,7 +114,9 @@ jobs:
run: |
set -x
git diff || :
# shellcheck disable=SC2086
git add ${changed_files}
# shellcheck disable=SC2086
if git diff-index --quiet HEAD ${changed_files}; then
echo "Nothing to do!"
exit
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ The `nox` configuration also contains session to run automated docs checkers.
nox -s lint
```

The `actionlint` linter that is run as part of the `lint` session requires
`podman` or `docker` to be installed.
If both container engines are installed, `podman` is preferred.
Set `CONTAINER_ENGINE=docker` to change this behavior.

### Checking spelling

Use [`codespell`](https://github.com/codespell-project/codespell) to check for common spelling mistakes in the documentation source.
Expand Down
47 changes: 47 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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


CONTAINER_ENGINES = ("podman", "docker")
CHOSEN_CONTAINER_ENGINE = os.environ.get("CONTAINER_ENGINE")
ACTIONLINT_IMAGE = "docker.io/rhysd/actionlint"


def _get_container_engine(session: nox.Session) -> str:
path: str | None = None
if CHOSEN_CONTAINER_ENGINE:
path = shutil.which(CHOSEN_CONTAINER_ENGINE)
if not path:
session.error(
f"CONTAINER_ENGINE {CHOSEN_CONTAINER_ENGINE!r} does not exist!"
)
return path
for engine in CONTAINER_ENGINES:
if path := shutil.which(engine):
return path
session.error(
f"None of the following container engines were found: {CONTAINER_ENGINES}."
f" {session.name} requires a container engine installed."
)


@nox.session
def static(session: nox.Session):
"""
Expand Down Expand Up @@ -92,12 +116,35 @@ def spelling(session: nox.Session):
)


@nox.session
def actionlint(session: nox.Session) -> None:
"""
Run actionlint to lint Github Actions workflows.
The actionlint tool is run in a Podman/Docker container.
"""
engine = _get_container_engine(session)
session.run_always(engine, "pull", ACTIONLINT_IMAGE, external=True)
session.run(
engine,
"run",
"--rm",
# fmt: off
"--volume", f"{Path.cwd()}:/pwd:z",
"--workdir", "/pwd",
# fmt: on
ACTIONLINT_IMAGE,
*session.posargs,
external=True,
)


@nox.session
def lint(session: nox.Session):
session.notify("typing")
session.notify("static")
session.notify("formatters")
session.notify("spelling")
session.notify("actionlint")


requirements_files = list(
Expand Down
Loading