Automate Python Linting and Code Style Enforcement with Ruff and GitHub Actions

Automate Python Linting and Code Style Enforcement with Ruff and GitHub Actions

Introduction

Maintaining a consistent code style and following best practices are important for any Python project. However, manually fixing linting issues can be tedious and easy to forget. In this tutorial, we'll see how to set up GitHub Actions to automatically lint Python code with Ruff on every push and commit any fixes.

In the previous tutorial, we saw how to use Ruff to lint Python code. However, manually fixing linting issues can be tedious and easy to forget.

Prerequisites

  • A GitHub repository with Python code

  • GitHub Actions enabled

  • Ruff installed locally

Creating the Workflow

The workflow will run on pushes to lint with Ruff and commit fixes:

name: Lint and Commit
on: push

jobs:
  lint:
    runs-on: ubuntu-latest 
    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-python@v2
    - run: pip install ruff
    - run: | 
      ruff check .
      ruff fix .
    - uses: stefanzweifel/git-auto-commit-action@v4
      with:
        commit_message: 'style fixes by ruff'

The new key step is the git-auto-commit-action, which will detect changes made by Ruff and commit them with a message.

Configuring Ruff

We can create a .ruff.toml or ruff.toml file to customize Ruff:


line-length = 120
target-version = "py39"
select = ["E", "W"]

The code above sets the line length to 120 characters, the target Python version to 3.9, and the rules to E and W. Read more about the configuration options here

Using the Ruff GitHub Action

We can also use the Ruff GitHub Action to run Ruff in our workflow:

name: Lint and Commit
on: [ push, pull_request ]

jobs:
  lint:
    runs-on: ubuntu-latest 
    steps:
    - uses: actions/checkout@v3
    - uses: actions/setup-python@v2
    - uses: chartboost/ruff-action@v1
      with:
        args: --check .
        fix_args: --fix .
    - uses: stefanzweifel/git-auto-commit-action@v4
      with:
        commit_message: 'style fixes by ruff'

The code above is equivalent to the previous workflow. The main difference is that we are using the Ruff GitHub Action instead of running Ruff directly. Read more about the Ruff GitHub Action here

Each approach has its advantages:

  • Running Ruff directly gives us more control over the Ruff process. We can specify a custom configuration file, for example.

  • Using the Ruff GitHub Action is simpler and requires less code.

  • Running Ruff directly is marginally faster because we don't need to install Ruff every time.

This will run Ruff with the --check and --fix arguments. We can also use the config argument to specify a custom configuration file:

name: Lint and Commit
on: push

jobs:
  lint:
    runs-on: ubuntu-latest 
    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-python@v2
    - uses: chartboost/ruff-action@v1
      with:
        args: --check .
        fix_args: --fix .
        config: .ruff.toml
    - uses: stefanzweifel/git-auto-commit-action@v4
      with:
        commit_message: 'style fixes by ruff'

This will adjust the rules to our preference. This is useful if we want to use different rules for different projects.

Pick the approach that works best for your project.

Auto Commit Action

We are also using the auto-commit action to commit the changes. We can configure the commit message and other options. Read more about the auto-commit action here. This is optional - we can also use the git action to commit the changes manually.

The combination of Ruff and the auto-commit action allows us to automatically fix linting issues and commit the changes on every push. This helps us maintain a consistent code style and follow best practices over time.

Viewing Results

The Actions output will now show files changed and committed by Ruff. The commits will also be visible in the repository history.

Workflow run

Feel free to check out my repository for an example of workflow usage/run.

Conclusion

With auto-commit enabled, we no longer need to manually fix linting issues flagged by Ruff - the fixes are applied automatically on each push. This helps enforce a consistent code style over time.

Some next steps:

  • Set Ruff to run on pull requests too.

  • Configure commit options like commit user and branch.

  • Set up Slack/email notifications.

By integrating linters like Ruff into our GitHub workflows, we can automate parts of the coding process and reduce manual work.

Let's connect on Twitter and LinkedIn.

References

Next time gif