| 1 |
# GitHub Actions integration |
| 2 |
|
| 3 |
You can use _Black_ within a GitHub Actions workflow without setting your own Python |
| 4 |
environment. Great for enforcing that your code matches the _Black_ code style. |
| 5 |
|
| 6 |
## Compatibility |
| 7 |
|
| 8 |
This action is known to support all GitHub-hosted runner OSes. In addition, only |
| 9 |
published versions of _Black_ are supported (i.e. whatever is available on PyPI). |
| 10 |
|
| 11 |
Finally, this action installs _Black_ with the `colorama` extra so the `--color` flag |
| 12 |
should work fine. |
| 13 |
|
| 14 |
## Usage |
| 15 |
|
| 16 |
Create a file named `.github/workflows/black.yml` inside your repository with: |
| 17 |
|
| 18 |
```yaml |
| 19 |
name: Lint |
| 20 |
|
| 21 |
on: [push, pull_request] |
| 22 |
|
| 23 |
jobs: |
| 24 |
lint: |
| 25 |
runs-on: ubuntu-latest |
| 26 |
steps: |
| 27 |
- uses: actions/checkout@v6 |
| 28 |
- uses: psf/black@stable |
| 29 |
``` |
| 30 |
|
| 31 |
We recommend the use of the `@stable` tag, but per version tags also exist if you prefer |
| 32 |
that. Note that the action's version you select is independent of the version of _Black_ |
| 33 |
the action will use. |
| 34 |
|
| 35 |
### Versions |
| 36 |
|
| 37 |
The version of _Black_ the action will use can be configured via `version` or read from |
| 38 |
the `pyproject.toml` file. The action defaults to the latest release available on PyPI. |
| 39 |
|
| 40 |
`version` can be any |
| 41 |
[valid version specifier](https://packaging.python.org/en/latest/glossary/#term-Version-Specifier) |
| 42 |
or just the version number if you want an exact version. |
| 43 |
|
| 44 |
If you want to match versions covered by Black's |
| 45 |
[stability policy](labels/stability-policy), you can use the compatible release operator |
| 46 |
(`~=`): |
| 47 |
|
| 48 |
```yaml |
| 49 |
- uses: psf/black@stable |
| 50 |
with: |
| 51 |
options: "--check --verbose" |
| 52 |
src: "./src" |
| 53 |
version: "~= 26.0" |
| 54 |
``` |
| 55 |
|
| 56 |
To read the version from the `pyproject.toml` file instead, set `use_pyproject` to |
| 57 |
`true`. This will first look into the `tool.black.required-version` field, then the |
| 58 |
`dependency-groups` table, then the `project.dependencies` array and finally the |
| 59 |
`project.optional-dependencies` table. Note that this requires Python >= 3.11, so using |
| 60 |
the setup-python action may be required, for example: |
| 61 |
|
| 62 |
**Security note:** `use_pyproject` only accepts standard version specifiers for `black` |
| 63 |
(for example `==`, `~=`, `>=` and ranges like `>=25,<26`). Direct references such as |
| 64 |
`black @ https://...` are not supported. If your workflow runs on untrusted pull |
| 65 |
requests (for example from forks), prefer setting `with.version` explicitly. |
| 66 |
|
| 67 |
```yaml |
| 68 |
- uses: actions/setup-python@v6 |
| 69 |
with: |
| 70 |
python-version: "3.13" |
| 71 |
- uses: psf/black@stable |
| 72 |
with: |
| 73 |
options: "--check --verbose" |
| 74 |
src: "./src" |
| 75 |
use_pyproject: true |
| 76 |
``` |
| 77 |
|
| 78 |
Only versions available from PyPI are supported, so no commit SHAs or branch names. |
| 79 |
|
| 80 |
### Jupyter Notebooks |
| 81 |
|
| 82 |
If you want to include Jupyter Notebooks, it can be enabled by setting `jupyter` to |
| 83 |
`true` (default is `false`): |
| 84 |
|
| 85 |
```yaml |
| 86 |
- uses: psf/black@stable |
| 87 |
with: |
| 88 |
jupyter: true |
| 89 |
``` |
| 90 |
|
| 91 |
See the [Jupyter Notebooks guide](../guides/using_black_with_jupyter_notebooks.md) for |
| 92 |
more details. |
| 93 |
|
| 94 |
### CLI Options |
| 95 |
|
| 96 |
You can also configure the arguments passed to _Black_ via `options` (defaults to |
| 97 |
`'--check --diff'`) and `src` (default is `'.'`). Please note that the |
| 98 |
[`--check` flag](labels/exit-code) is required so that the workflow fails if _Black_ |
| 99 |
finds files that need to be formatted. |
| 100 |
|
| 101 |
Here's an example configuration: |
| 102 |
|
| 103 |
```yaml |
| 104 |
- uses: psf/black@stable |
| 105 |
with: |
| 106 |
options: "--check --verbose" |
| 107 |
src: "./src" |
| 108 |
jupyter: true |
| 109 |
version: "21.5b1" |
| 110 |
``` |