Skip to content

CI: Separate jobs for publishing to TestPyPI and PyPI (#3742) #1910

CI: Separate jobs for publishing to TestPyPI and PyPI (#3742)

CI: Separate jobs for publishing to TestPyPI and PyPI (#3742) #1910

# Publish archives to PyPI and TestPyPI using GitHub Actions
#
# This workflow is ran to publish PyGMT archives (source and binary distributions)
# to TestPyPI (for testing only) and PyPI.
#
# Archives will be pushed to TestPyPI for every commit to the main branch and
# tagged releases, and to PyPI for tagged releases only.
#
# Note that authentication to TestPyPI/PyPI is done via OpenID Connect, see also
# https://github.com/pypa/gh-action-pypi-publish/tree/release/v1#publishing-with-openid-connect
#
# Important: this workflow filename must be publish-to-pypi.yml to match the
# settings in PyPI and TestPyPI so that OIDC publishing works.
#
name: Publish to PyPI
# Only run for pushes to the main branch and releases.
on:
push:
branches: [ main ]
paths:
- 'pygmt/**'
- '!pygmt/tests/**'
- 'Makefile'
- 'MANIFEST.in'
- 'pyproject.toml'
- 'README.md'
- '.github/workflows/publish-to-pypi.yml'
release:
types:
- published
# Runs for pull requests should be disabled other than for testing purposes
#pull_request:
# branches:
# - main
jobs:
build:
name: Build distribution πŸ“¦
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4.2.2
with:
# fetch all history so that setuptools-scm works
fetch-depth: 0
persist-credentials: false
- name: Set up Python
uses: actions/setup-python@v5.3.0
with:
python-version: '3.13'
- name: Install dependencies
run: python -m pip install build
# This step is only necessary for testing purposes and for TestPyPI
- name: Fix up version string for TestPyPI
if: ${{ !startsWith(github.ref, 'refs/tags') }}
run: |
# Change setuptools-scm local_scheme to "no-local-version" so the
# local part of the version isn't included, making the version string
# compatible with PyPI.
sed --in-place "s/node-and-date/no-local-version/g" pyproject.toml
- name: Build source and wheel distributions
run: |
make package
echo ""
echo "Generated files:"
ls -lh dist/
- name: Store the distribution packages
uses: actions/upload-artifact@v4.5.0
with:
name: python-package-distributions
path: dist/
publish-to-testpypi:
name: Publish Python 🐍 distribution πŸ“¦ to TestPyPI
if: github.repository == 'GenericMappingTools/pygmt'
needs:
- build
runs-on: ubuntu-latest
environment:
name: testpypi
url: https://test.pypi.org/project/pygmt
permissions:
id-token: write # IMPORTANT: mandatory for trusted OIDC publishing
steps:
- name: Download all the dists
uses: actions/download-artifact@v4.1.8
with:
name: python-package-distributions
path: dist/
- name: Publish distribution πŸ“¦ to TestPyPI
uses: pypa/gh-action-pypi-publish@v1.12.3
with:
repository-url: https://test.pypi.org/legacy/
publish-pypi:
name: Publish Python 🐍 distribution πŸ“¦ to PyPI
if: github.repository == 'GenericMappingTools/pygmt' && startsWith(github.ref, 'refs/tags/')
needs:
- build
runs-on: ubuntu-latest
environment:
name: pypi
url: https://pypi.org/project/pygmt/
permissions:
id-token: write # IMPORTANT: mandatory for trusted OIDC publishing
steps:
- name: Download all the dists
uses: actions/download-artifact@v4.1.8
with:
name: python-package-distributions
path: dist/
- name: Publish distribution πŸ“¦ to PyPI
uses: pypa/gh-action-pypi-publish@v1.12.3