Automating Python Releases: GitHub Actions, Poetry, and Trusted Publishers
Overview of Automated Publishing
Publishing a package manually is a recipe for inconsistency. Between managing version tags in , building distribution wheels, and uploading to the , there are too many manual touchpoints where human error can creep in. Automation turns this fragile process into a repeatable pipeline. By using , you can trigger a release workflow simply by pushing a version tag. This ensures that the code on exactly matches the version users install via pip, providing a single source of truth for your software distribution.
Prerequisites and Tools
To follow this workflow, you need a basic understanding of project structure and command-line operations. We use for dependency management and building because it handles versioning and project metadata more elegantly than legacy tools. You will also need a account and a repository hosted on . Familiarity with syntax is helpful, as that is the language used to define workflows.

Key Libraries & Tools
- : A comprehensive tool for packaging and dependency management.
- : The official third-party software repository for .
- : A CI/CD platform that allows you to automate your build, test, and deployment pipeline.
- Trusted Publishers: A security feature that uses short-lived tokens instead of permanent API keys.
Configuring Trusted Publishers
Security is paramount when connecting to . While API keys work, the Trusted Publisher mechanism is superior. It establishes a handshake between and using a short-lived identity token. To set this up, navigate to the publishing settings on your project and add a new publisher. You must provide your repository owner name, the repository name, and the specific workflow filename (e.g., release.yml). This prevents long-lived credentials from being stolen or leaked.
Building the Workflow
The heart of the automation is the .github/workflows/release.yml file. This workflow should trigger only when a specific tag pattern is pushed. Using regular expressions ensures that only valid version tags following trigger a release.
on:
push:
tags:
- 'v[0-9]+.[0-9]+.[0-9]+'
The workflow is divided into logical jobs: fetching details, checking if the version already exists on , building the package, and finally publishing.
jobs:
setup-and-build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Poetry
run: pipx install poetry
- name: Build
run: poetry build
We utilize poetry build to generate the source distribution and the wheel file. These artifacts are then passed to the pypa/gh-action-pypi-publish action for the final upload.
Syntax Notes and Best Practices
Pay close attention to GitHub Output. When passing data between jobs, you cannot use standard environment variables because each job runs on a fresh virtual machine. Instead, use echo "key=value" >> $GITHUB_OUTPUT to persist data like version numbers for subsequent jobs. Additionally, always follow for versioning to maintain compatibility with and .
Tips & Gotchas
does not allow you to overwrite a version once it has been published. If you upload 1.0.0 and realize there is a bug, you cannot simply delete and re-upload 1.0.0. You must bump the version to 1.0.1. Implementing a check job in your workflow that queries the API before building can save significant CI/CD resources by failing early if a version conflict is detected.
- 31%· software
- 13%· software
- 13%· software
- 13%· languages
- 9%· software
- Other topics
- 22%

GitHub Actions for Python Packages: How to Automate Releases to PyPi
WatchArjanCodes // 20:25
On this channel, I post videos about programming and software design to help you take your coding skills to the next level. I'm an entrepreneur and a university lecturer in computer science, with more than 20 years of experience in software development and design. If you're a software developer and you want to improve your development skills, and learn more about programming in general, make sure to subscribe for helpful videos. I post a video here every Friday. If you have any suggestion for a topic you'd like me to cover, just leave a comment on any of my videos and I'll take it under consideration. Thanks for watching!