Your resource for web content, online publishing
and the distribution of digital products.
S M T W T F S
 
 
 
 
 
1
 
2
 
3
 
4
 
5
 
6
 
7
 
8
 
9
 
 
 
 
 
 
 
16
 
17
 
18
 
19
 
20
 
21
 
22
 
23
 
24
 
25
 
26
 
27
 
28
 
29
 
30
 

Automate Python Package Publishing with GitHub Actions

DATE POSTED:September 27, 2024

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.

  1. Log in to your PyPI account.
  2. Go to your account settings and create a new API token.
  3. 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:

  1. Go to your GitHub repository.
  2. Click on Settings > Secrets and variables > Actions > New repository secret.
  3. 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.

  1. Create a directory .github/workflows/.
  2. 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.