Tuesday, January 21, 2025
HomePythonChecking Python Code with GitHub Actions

Checking Python Code with GitHub Actions


When you’re working in your private or work initiatives in Python, you often need to have a technique to implement code requirements. You need to use instruments like Flake8, PyLint or Ruff to lint your code. You may use Mypy to confirm kind checking. There are many different instruments at your disposal. However it may be arduous to recollect to try this each time you need to create a pull request (PR) in GitHub or GitLab.

That’s the place steady integration (CI) instruments are available in. GitHub has one thing known as GitHub Actions that help you run instruments or total workflows on sure kinds of occasions.

On this article, you’ll learn to create a GitHub Motion that runs Ruff on a PR. You may study extra about Ruff in my introductory article.

Making a Workflow

Within the root folder of your code, you will have to create a folder known as .github/workflows. Word the interval at first and the truth that you want a subfolder named workflows too. Inside that, you’ll create a YAML file.

Since you’re going to run Ruff to lint and format your Python recordsdata, you possibly can name this YAML file ruff.yml. Put the next in your new file:

identify: Ruff
on: [workflow_dispatch, pull_request]
jobs:
  construct:
    runs-on: ubuntu-latest
    steps:
      - makes use of: actions/checkout@v4
      - identify: Set up Python
        makes use of: actions/setup-python@v4
        with:
          python-version: "3.13"
      - identify: Set up dependencies
        run: |
          python -m pip set up --upgrade pip
          pip set up ruff
      # Embody `--format=github` to allow computerized inline annotations.
      - identify: Run Ruff
        run: ruff verify --output-format=github .
        continue-on-error: false
      - identify: Run Ruff format
        run: ruff format --check .
        continue-on-error: false

Word: This instance comes from my textual-cogs repo

Let’s speak about what this does. This line is fairly essential:

on: [workflow_dispatch, pull_request]

It tells GitHub to run this workflow when there’s a pull request and in addition with “workflow_dispatch”. That second choice permits you to go to the Actions tab in your GitHub repo, choose the workflow and run it manually. If you don’t embrace that, you can not run it manually in any respect. That is helpful for testing functions, however you possibly can take away it if you do not want it.

The subsequent half tells GitHub to run the construct on ubuntu-linux:

jobs:
  construct:
    runs-on: ubuntu-latest

If in case you have a GitHub subscription, you may also get Home windows as a runner, which suggests that you may additionally run these actions on Home windows along with Linux.

The steps part is the meat of this explicit workflow:

steps:
  - makes use of: actions/checkout@v4
  - identify: Set up Python
    makes use of: actions/setup-python@v4
    with:
      python-version: "3.13"
  - identify: Set up dependencies
    run: |
      python -m pip set up --upgrade pip
      pip set up ruff
  # Embody `--format=github` to allow computerized inline annotations.
  - identify: Run Ruff
    run: ruff verify --output-format=github .
    continue-on-error: false
  - identify: Run Ruff format
    run: ruff format --check .
    continue-on-error: false

Right here is makes use of a built-in checkout@v4 workflow, which is one thing that comes with GitHub. There are a lot of others you should utilize to reinforce your workflows and add new performance. Subsequent, you get setup with Python 3.13 and then you definately set up your dependencies.

As soon as your dependencies are put in, you possibly can run your instruments. On this case, you’re putting in and working Ruff. For each PR, you run ruff verify --output-format=github ., which is able to do all types of linting in your Python code. If any errors are discovered, it can add inline feedback with the error, which is what that --output-format flag is for.

You even have a separate part to run Ruff format, which is able to format your code to comply with the Black formatter (for essentially the most half).

Wrapping Up

You may add a number of different instruments to your workflows too. For instance, you may add a Mypy workflow, or some check workflows to run on PR or maybe earlier than merging to your important department. You may even need to verify your code complexity earlier than permitting a merge too!

With a bit of work, you’ll quickly be capable to use GitHub Actions to maintain your code cleaner and make it extra uniform too. Including automated testing is even higher! Give it a try to let me know what you assume.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments