This content originally appeared on HackerNoon and was authored by Saurabh Dhariwal
Setting up GitHub Actions to automate the publishing of Python packages to the Python Package Index (PyPI) involves creating a workflow that runs when you push changes to your repository. Here’s a step-by-step guide to help you integrate GitHub Actions with PyPI for your Python projects.
Step 1: Prepare Your Python Package
Ensure your Python package is ready for distribution. You should have the following files in your project directory:
setup.py
: This file contains metadata about your package.pyproject.toml
: This file is often used for specifying build dependencies.- Your package source code in a directory (e.g.,
my_package/
).
Here's a simple example of what your setup.py
might look like:
from setuptools import setup, find_packages
setup(
name='my_package',
version='0.1.0',
packages=find_packages(),
install_requires=[
# List your package dependencies here
],
)
Step 2: Create a PyPI Account
If you haven’t already, create an account on PyPI. Once registered, you can create an API token for authentication.
- Log in to your PyPI account.
- Go to your account settings and create a new API token.
- Copy the generated token, as you will need it later.
Step 3: Store the PyPI Token in GitHub Secrets
To securely use your PyPI token in GitHub Actions, add it to your repository secrets:
- Go to your GitHub repository.
- Click on Settings > Secrets and variables > Actions > New repository secret.
- Name the secret
PYPI_TOKEN
and paste your PyPI API token in the value field.
Step 4: Create the GitHub Actions Workflow
Create a workflow file in your repository to define the steps for publishing to PyPI.
- Create a directory
.github/workflows/
. - Inside that directory, create a file named
publish.yml
.
Here’s a sample publish.yml
file:
name: Publish to PyPI
on:
push:
tags:
- 'v*' # Trigger on version tags like v1.0.0
jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.8' # Specify your Python version
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine
- name: Build package
run: |
python setup.py sdist bdist_wheel
- name: Publish to PyPI
env:
TWINE_USERNAME: __token__ # Use the token authentication
TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }}
run: |
twine upload dist/*
Step 5: Create a Tag for the Release
To trigger the workflow, you need to create a Git tag that matches the pattern defined in your workflow file (e.g., v1.0.0
). You can do this using the following Git commands:
git tag v1.0.0
git push origin v1.0.0
Step 6: Check GitHub Actions
After pushing the tag, go to your GitHub repository and check the Actions tab. You should see the workflow running. If everything is set up correctly, it will build your package and publish it to PyPI.
Conclusion
By following these steps, you can successfully set up GitHub Actions to automate the process of publishing your Python development projects to PyPI. This integration not only streamlines your deployment process but also ensures that your package is consistently updated with the latest changes.
This content originally appeared on HackerNoon and was authored by Saurabh Dhariwal
Saurabh Dhariwal | Sciencx (2024-09-27T12:59:46+00:00) Automate Python Package Publishing with GitHub Actions. Retrieved from https://www.scien.cx/2024/09/27/automate-python-package-publishing-with-github-actions/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.