Four Github Actions for Python


πŸŽ„This post has fully grown

If you are developing python packages and using GitHub here are four actions that you can use today to automate your release workflow. Since python tools generally have such a simple cli I have opted to use the cli for most of these, that way I know exactly what is happening and have more control over it if I need.

  • Lint
  • Test
  • Package
  • Upload to PyPi

Lint With flake8

flake8 is pythons quintessential linting tool to ensure that your code is up to the standards that you have set for the project, and to help prevent hidden bugs. I am a heavy user of black and isort as well, but for ci flake8 is typically considered the gold standard. black and isort will help you automate many fixes suggested by flake8.

    - name: Lint with flake8
      run: |
        pip install flake8 isort black
        # stop the build if there are Python syntax errors or undefined names
        flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
        # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
        flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics

Testing with pytest

pytest is such an amazing project, definitely one to check out and start using if you are not already doing so.

    - name: Test with pytest
      run: |
         pip install pytest

Building with setuptools

I am still using the older, less hipster, setuptools to build my projects. Primarily because I am used to to, partly because things such as editable installs are not possible with the newer build tools, and I am a HEAVY user of editable installs.

    - name: build
      run: |
        pip install wheel
        python sdist bdist_wheel

Publishing to pypi

Here I am going to use an amazing action from the GitHub marketplace by @webKnjaZ. It is super simple. First you need to log into your account, go to account settings, enable 2FA, and add a Token, then paste that toke into a secret inside your repos settings. Next just drop the name of that secret into the password field of the action and you are off.

note: I did put a check in to make sure that push event comes from main.

    - name: pypi-publish
      if: github.ref == 'refs/heads/main'
      uses: pypa/gh-action-pypi-publish@v1.1.0
        # PyPI user
        # Password for your PyPI user or an access token
        password: ${{ secrets.pypi_password }}
        # The repository URL to use
        # repository_url: # optional
        # The target directory for distribution
        # packages_dir: # optional, default is dist

That's my four top python actions

These are the easiest and most basic four actions that every python project on GitHub should have. Now that actions are available for free on any public repo there is no reason not to use GitHub Actions for any new project.

πŸ‘€ see an issue, edit this post on GitHub

If you found value in this post
and want to send a tip.

Buy Me A Coffee

tweet about this post and it will show up here.

←An IndieWeb Webring πŸ•ΈπŸ’β†’