I'm using the official black
GitHub Action. Currently, whenever I push changes, black
runs on the entire repository. However, I only want it to run on the changed files. I've tried using some of the GitHub environment variables, but to no avail. Here's my workflow yaml:
name: Lint
on: [push, pull_request]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run : echo ${{ github.sha }} # this outputs a SHA
- run : echo ${{ github.run_attempt }} # this outputs an int
- run: echo ${{ github.head_ref }} # outputs nothing
- run: echo ${{ github.base_ref }} # outputs nothing
- uses: actions/setup-python@v3
with:
python-version: '3.9.12'
name: Run black on diffed files
- run: echo ${{ github.head_ref }} # outputs nothing
- run: echo ${{ github.base_ref }} # outputs nothing
- run: pip install black && black $(git diff --name-only ${{ github.base_ref}} ${{ github.head_ref }} | grep .py)
The workflow successfully installs and runs black
, but it fails because no files are being passed to the black
command.
I'm not sure what I'm doing wrong here.
Found a solution that works for me, which also takes into consideration the advice of @jonrsharpe (see his comments on the original question).
Utilized another GitHub action to get all changed files. Then, I run
black
andreorder-python-imports
only on the changed files. That way, the user knows which of their files have issues. However, for CI reasons (thanks Jon!), I also then run black on the full codebase using Black's official GHA.NOTE: At present, the following GHA works as desired on pull-requests. However, if you push multiple commits, it only runs the action on the final commit in the group. I'm trying to figure out how to make it run on all commits that are pushed, rather than just the final one.