Skip to content

Commit 04d12c8

Browse files
committed
Add GitHub Actions workflow for publishing to PyPI and TestPyPI
- Created a new workflow to automate the publishing process to both TestPyPI and PyPI based on release events and tag patterns. - Included steps for building the package, checking it, and uploading artifacts. - Configured environment input options for manual deployment to specified environments.
1 parent 13f3398 commit 04d12c8

1 file changed

Lines changed: 88 additions & 0 deletions

File tree

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
name: Publish to PyPI (Token)
2+
3+
on:
4+
release:
5+
types: [published]
6+
push:
7+
tags:
8+
- 'v*-test' # Trigger TestPyPI on tags like v0.1.0-test
9+
workflow_dispatch:
10+
inputs:
11+
environment:
12+
description: 'Environment to deploy to'
13+
required: true
14+
default: 'testpypi'
15+
type: choice
16+
options:
17+
- testpypi
18+
- pypi
19+
20+
jobs:
21+
build:
22+
name: Build distribution
23+
runs-on: ubuntu-latest
24+
25+
steps:
26+
- name: Checkout code
27+
uses: actions/checkout@v4
28+
29+
- name: Set up Python
30+
uses: actions/setup-python@v4
31+
with:
32+
python-version: '3.11'
33+
34+
- name: Install build dependencies
35+
run: |
36+
python -m pip install --upgrade pip
37+
pip install build twine
38+
39+
- name: Build package
40+
run: python -m build
41+
42+
- name: Check package
43+
run: twine check dist/*
44+
45+
- name: Upload artifacts
46+
uses: actions/upload-artifact@v4
47+
with:
48+
name: python-package-distributions
49+
path: dist/
50+
51+
publish-to-testpypi:
52+
name: Publish to TestPyPI
53+
if: |
54+
(github.event_name == 'workflow_dispatch' && github.event.inputs.environment == 'testpypi') ||
55+
(github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') && contains(github.ref, '-test'))
56+
needs: [build]
57+
runs-on: ubuntu-latest
58+
59+
steps:
60+
- name: Download artifacts
61+
uses: actions/download-artifact@v4
62+
with:
63+
name: python-package-distributions
64+
path: dist/
65+
66+
- name: Publish to TestPyPI
67+
uses: pypa/gh-action-pypi-publish@release/v1
68+
with:
69+
repository-url: https://test.pypi.org/legacy/
70+
password: ${{ secrets.TEST_PYPI_API_TOKEN }}
71+
72+
publish-to-pypi:
73+
name: Publish to PyPI
74+
if: github.event_name == 'release' || (github.event_name == 'workflow_dispatch' && github.event.inputs.environment == 'pypi')
75+
needs: [build]
76+
runs-on: ubuntu-latest
77+
78+
steps:
79+
- name: Download artifacts
80+
uses: actions/download-artifact@v4
81+
with:
82+
name: python-package-distributions
83+
path: dist/
84+
85+
- name: Publish to PyPI
86+
uses: pypa/gh-action-pypi-publish@release/v1
87+
with:
88+
password: ${{ secrets.PYPI_API_TOKEN }}

0 commit comments

Comments
 (0)