black/docs/integrations/github_actions.md
Stefan Foulis 4005246f86
Add version to github action (and rewrite the whole thing while at it) (#1940)
Commit history before merge:

* Add black_version to github action
* Merge upstream/main into this branch
* Add version support for the Black action pt.2

  Since we're moving to a composite based action, quite a few changes
  were made. 1) Support was added for all OSes (Windows was painful). 
  2) Isolation from the rest of the workflow had to be done manually
  with a virtual environment.

  Other noteworthy changes:

  - Rewrote basically all of the logic and put it in a Python script
    for easy testing (not doing it here tho cause I'm lazy and I can't
    think of a reasonable way of testing it).
  - Renamed `black_version` to `version` to better fit the existing
    input naming scheme.
  - Added support for log groups, this makes our action's output a
    bit more fancy (I may or may have not added some debug output too).

* Add more to and sorta rewrite the Action's docs

  Reflect compatability and gotchas.

* Add CHANGELOG entry
* Merge main into this branch
* Remove debug; address typos; clean up action.yml

Co-authored-by: Richard Si <63936253+ichard26@users.noreply.github.com>
2021-05-31 21:45:50 -04:00

1.5 KiB

GitHub Actions integration

You can use Black within a GitHub Actions workflow without setting your own Python environment. Great for enforcing that your code matches the Black code style.

Compatiblity

This action is known to support all GitHub-hosted runner OSes. In addition, only published versions of Black are supported (i.e. whatever is available on PyPI).

Finally, this action installs Black with both the colorama and python2 extras so the --color flag and formatting Python 2 code are supported.

Usage

Create a file named .github/workflows/black.yml inside your repository with:

name: Lint

on: [push, pull_request]

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: psf/black@stable

We recommend the use of the @stable tag, but per version tags also exist if you prefer that. Note that the action's version you select is independent of the version of Black the action will use.

The version of Black the action will use can be configured via version. The action defaults to the latest release available on PyPI. Only versions available from PyPI are supported, so no commit SHAs or branch names.

You can also configure the arguments passed to Black via options (defaults to '--check --diff') and src (default is '.')

Here's an example configuration:

- uses: psf/black@stable
  with:
    options: "--check --verbose"
    src: "./src"
    version: "21.5b1"