Dennis O'Keeffe
3 min readSep 23, 2021
Heading image

This is Day 23 of the #100DaysOfPython challenge.

This post will use GitHub actions to help automate the deploy of your Pip package to the PyPi registry.

We will work off the code repository written in yesterday’s post “Semantic Versioning In Python With Git Hooks”

The final code can be found here.

Prerequisites

  1. Familiarity with Pipenv. See here for my post on Pipenv.
  2. You will need an access token from PyPi to deploy your package. You can create one for your project here.
  3. Read “PyTest With GitHub Actions” to get an initial taste of Python GitHub actions.
  4. This post will also use the GitHub CLI.

Getting started

We will clone the code from yesterday’s post as our starting place.

At this stage, we are ready to create our workflow.

Creating the workflow

We need to add our code to the workflow file .github/workflows/publish-to-pypi.yml.

Much of this file will get inspiration from .github/workflows/pytest.yml.

Add the following code to .github/workflows/publish-to-pypi.yml:

Our pipenv run build step comes from the script we added in a previous post.

We are now almost ready to test our workflow. The next step will be to ready the PYPI_API_TOKEN value on the final line.

Worth noting: There is also a test package index that you can use so you do not affect to main index. I am using the main index for my package.

Adding our PyPi token to GitHub

A prerequisite for this post was to setup a PyPi token to use.

With that token, we will use the GitHub CLI to post it up for use:

Testing our workflow

At this stage, all we need to do is push to remote:

Head to the link supplied to visit the repository and head to the actions tab and view the Publish Python 🐍 distributions 📦 to PyPI.

You should see your workflow succeed.

GitHub action success

The GitHub action also provides a link to your package on PyPi.

Update version on PyPi

Summary

Today’s post demonstrated how to automate your publish flow to PyPi on a push action.

Actions are a great way to automate your workflow. Today’s simple example enables a publish on each push, but you can improve on this to lock branches and even automate the versioning process.

Resources and further reading

  1. The ABCs of Pipenv
  2. Pipenv
  3. Repo code
  4. Test package index
  5. “PyTest With GitHub Actions”
  6. GitHub CLI

Photo credit: worldsbetweenlines

Originally posted on my blog. To see new posts without delay, read the posts there and subscribe to my newsletter.

I write content for AWS, Kubernetes, Python, JavaScript and more. To view all the latest content, be sure to visit my blog and subscribe to my newsletter. Follow me on Twitter.

Dennis O'Keeffe
Dennis O'Keeffe

No responses yet