DEV Community

loading...

I wrote a custom GitHub Action that adds continuous integration to your Python project

alexanderdamiani profile image Alex Damiani ・1 min read

The GitHub Action can be found here:

https://github.com/marketplace/actions/pylinter


I've also created a YouTube video that details how to add the GitHub Action to your project, as well as a breakdown of how the Action works:

https://www.youtube.com/watch?v=oi94qEvi9Qo

All the code for the video is on GitHub as well, with the link in the video description.


Please consider subscribing if you find the video helpful. I am trying to post a new video at least once a week on various topics surrounding Python and DevOps.

Discussion (8)

pic
Editor guide
Collapse
michaelcurrin profile image
Michael • Edited

Regarding commit changes as in your docs, would you consider using the builtin token for GH Actions instead of making a custom access token? It requires zero setup and is way more secure (it isn't not visible or usable outside a workflow while an access token gets write access to all your repos so it is risky to have it leaked by any weak or malicious Actions one uses)

See the recommendation for push action docs.

github_token: ${{ secrets.GITHUB_TOKEN }}
Enter fullscreen mode Exit fullscreen mode
Collapse
alexanderdamiani profile image
Alex Damiani Author • Edited

I was honestly not aware of this. Thank you for showing me, I've added it to the action and published a new version v1.1.0. I've also added flags to optionally skip any or all of the linters.

Collapse
michaelcurrin profile image
Michael

You're welcome. The update looks great.

Collapse
michaelcurrin profile image
Michael • Edited

It is also worth mentioning when possibly not to use an action to handle linting.

I prefer to have linting in my Makefile so I can run commands locally and in my workflow, both using make.

github.com/MichaelCurrin/py-projec...

I do also have a flake8 config and dev requirements file, which I have to repeat in each project as a bit of boilerplate but I can easily make project-specific changes.

But... if I moved all that logic to an action file, it stops me from running my linting locally. And I like the fast turn around of local linting. Including as a pre push git hook, so that I am stopped from pushing commits that fail local checks and therefore would fail on Actions and I might only notice the next day

Maybe the middle ground is if I can run a way to run an action like yours locally. If your action is setup as a Docker container, then I can run it locally as a binary. And it will have its dependencies sandboxed so it would conflict with my project packages

Collapse
michaelcurrin profile image
Michael

Thanks for your share.

Having a linter to do 3 tasks and commit changes, without configuring and installing each, sounds powerful.

Collapse
michaelcurrin profile image
Michael

Can you explain how your action differs from others in functionality or what you found missing in the marketplace?

For example I found this one before which handles Black, Flake8 and MyPy, plus a ton of non Python liners

github.com/marketplace/actions/lin...

Collapse
alexanderdamiani profile image
Alex Damiani Author • Edited

When I was doing a search for similar actions I limited my search to Python specific actions and didn't find any action that was doing all of:

  1. linting with flake8, mypy, and isort
  2. recursively running mypy (it doesn't by default)
  3. printing out the errors that caused the job to fail

So that's what motivated me to write the action.

Reviewing the one you linked though, I am noticing two things it's lacking that my actions solves:

  1. it does not include isort
  2. it is not running mypy recursively

The action you linked does have black though, and that is something I'm going to add to my action in the near future for v1.2.0.

Thread Thread
michaelcurrin profile image
Michael

I like how you have docs setup with optional flags and examples.

Black is a formatter for code style and not quite a linter I don't think, so maybe it doesn't make sense in your project called pylinter. It depends how you define linting.
Flake8 does both linting for errors and formatting around PEP8 so it is in both areas.

Or maybe you want to go all in one. Mypy, flake8, isort, black.

In short term, can I suggest adding a link to the existing black action.
Maybe you don't have to reinvent it in your project and just keep the external reference.

The black repo itself actually recommends an action in their docs

github.com/psf/black#github-actions

Instead of a separate repo, they actually keep their action code in the action subfolder of the same repo.

github.com/psf/black/tree/master/a...