Back to home

PrerequisitesGetting startedCreating the workflowAdding our PyPi token to GitHubTesting our workflowSummaryResources and further reading
Automating Pip Package Deployment With Github Actions main image

Automating Pip Package Deployment With Github Actions

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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 # 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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 name: Publish Python 🐍 distributions 📦 to PyPI on: push jobs: build-and-publish: name: Build and publish Python 🐍 distributions 📦 to PyPI runs-on: ubuntu-latest steps: - name: Check out repository code uses: actions/checkout@v2 # Setup Python (faster than using Python container) - name: Setup Python uses: actions/setup-python@v2 with: python-version: "3.x" - name: Install pipenv run: | python -m pip install --upgrade pipenv wheel - id: cache-pipenv uses: actions/cache@v1 with: path: ~/.local/share/virtualenvs key: ${{ runner.os }}-pipenv-${{ hashFiles('**/Pipfile.lock') }} - name: Install dependencies if: steps.cache-pipenv.outputs.cache-hit != 'true' run: | pipenv install --deploy --dev - name: Build a binary wheel and a source tarball run: | pipenv run build - name: Publish distribution 📦 to PyPI uses: pypa/gh-action-pypi-publish@master with: password: ${{ secrets.PYPI_API_TOKEN }}

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.

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:

1 2 3 4 5 6 7 8 9 # 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:

1 2 3 4 5 6 7 8 9 10 11 $ 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. To https://github.com/your-gh-username/automating-pip-package-deployment-with-github-actions.git * [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

GitHub action success

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

Update version 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

Photo credit: worldsbetweenlines

Dennis O'Keeffe

@dennisokeeffe92
  • Melbourne, Australia

Hi, I am a professional Software Engineer. Formerly of Culture Amp, UsabilityHub, Present Company and NightGuru.
I am currently working on workingoutloud.dev, Den Dribbles and LandPad .

Related articles


1,200+ PEOPLE ALREADY JOINED ❤️️

Get fresh posts + news direct to your inbox.

No spam. We only send you relevant content.