# Clone the repo
$ git clone https://github.com/okeeffed/semantic-versioning-in-python-with-git-hooks automating-pip-package-deployment-with-github-actions
$ cd automating-pip-package-deployment-with-github-actions
# I am going to reset the Git history for my own repo.
$ rm -rf .git
$ git init
$ git add --all
$ git commit -m "feat: init commit"
$ git tag -a 0.1.0 -m "Init version"
# Install all dependencies
$ pipenv install --dev
# Create a new GitHub action to run our deployment from
$ touch .github/workflows/publish-to-pypi.yml
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:
# First we need a remote repo to push to
$ gh repo create your-gh-username/automating-pip-package-deployment-with-github-actions --public -y
✓ Created repository your-gh-username/automating-pip-package-deployment-with-github-actions on GitHub
✓ Added remote https://github.com/your-gh-username/automating-pip-package-deployment-with-github-actions.git
# Now we create our token
$ gh secret set PYPI_API_TOKEN
? Paste your secret ********************************************************************
✓ Set secret PYPI_API_TOKEN for your-repo/automating-pip-package-deployment-with-github-actions
Testing our workflow
At this stage, all we need to do is push to remote:
$ git push
Enumerating objects: 28, done.
Counting objects: 100% (28/28), done.
Delta compression using up to 8 threads
Compressing objects: 100% (25/25), done.
Writing objects: 100% (28/28), 12.99 KiB | 1.44 MiB/s, done.
Total 28 (delta 6), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (6/6), done.
* [new branch] main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.
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
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.