Sub topic 14
Applying the Same Pattern to Python and Other Languages
This chapter assumes you have read the earlier chapters on check-only scripts, workflow anatomy, and exit codes. You should be comfortable with basic YAML and with one linter and formatter pair, such as Ruff for Python.
The pattern is language-independent
The JavaScript workflow in this tutorial has four parts:
- Check out the code.
- Install the language runtime and the tools.
- Run a linter in check-only mode.
- Run a formatter in check-only mode.
Any language with a linter and a formatter that report problems through exit codes fits this shape. Only the setup action and the commands change. The triggers, permissions, concurrency, and required status checks work the same way.
A Python workflow with Ruff
Ruff is a Python tool that does both linting and formatting. The workflow below runs on pull requests.
name: Python lint
on:
pull_request:
jobs:
ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- run: pip install ruff
- run: ruff check --output-format=github .
- run: ruff format --check .Each step maps to a step in the JavaScript version:
| Step | JavaScript | Python |
|---|---|---|
| Set up runtime | `actions/setup-node@v4` | `actions/setup-python@v5` |
| Install tools | `npm ci` | `pip install ruff` |
| Lint | `eslint .` | `ruff check .` |
| Format check | `prettier --check .` | `ruff format --check .` |
ruff check reports lint problems and exits non-zero if it finds any. ruff format --check reports files that would change and exits non-zero without rewriting them. Plain ruff format . would rewrite files on the runner, and nobody would see the result.
The --output-format=github option makes Ruff print problems in a format that GitHub turns into inline annotations on the pull request diff.
Pin tool versions
Installing ruff without a version gets the latest release. A new release can add rules and fail a pull request that did nothing wrong. Pin the version in a requirements file and install from it.
- run: pip install -r requirements-dev.txtThis is the Python equivalent of npm ci with a lockfile. It also lets you add caching with cache: pip in actions/setup-python, as you did with cache: npm.
Other languages
The same approach works elsewhere. Look for a check-only mode in each tool.
| Language | Setup action | Lint | Format check |
|---|---|---|---|
| Go | `actions/setup-go` | `go vet ./...` | `test -z "$(gofmt -l .)"` |
| Rust | `dtolnay/rust-toolchain` | `cargo clippy -- -D warnings` | `cargo fmt --check` |
| Ruby | `ruby/setup-ruby` | `bundle exec rubocop` | `bundle exec rubocop` |
Two details deserve attention:
gofmt -llists unformatted files but exits with status 0 even when it finds some. Thetest -zwrapper turns a non-empty list into a failure.cargo clippyonly warns by default.-D warningspromotes warnings to errors so the exit code is non-zero.
Mixed repositories
If a repository holds more than one language, use separate jobs, one per language. Each job then appears as its own status check, and you can require each one before merging. A path filter on the trigger can skip a job when its files did not change, but be careful: a required check that never runs can block merging.
Exercise
You add a Go lint job that runs gofmt -l . as its format check. A pull request with badly formatted Go code passes. Why, and how do you fix it?
Show answer
gofmt -l prints the names of unformatted files but exits with status 0, so the step succeeds. Wrap it so that any output fails the step, for example test -z "$(gofmt -l .)".