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
- Familiarity with Pipenv. See here for my post on Pipenv.
- You will need an access token from PyPi to deploy your package. You can create one for your project here.
- Read “PyTest With GitHub Actions” to get an initial taste of Python GitHub actions.
- 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.
The GitHub action also provides a link to your package 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
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.