Skip to content

Commit 4a71185

Browse files
committed
Add publish pypi action
1 parent b458378 commit 4a71185

1 file changed

Lines changed: 69 additions & 0 deletions

File tree

.github/workflows/publish.yml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
name: Publish release to PyPI
2+
3+
on:
4+
# Manual trigger – requires a tag input
5+
workflow_dispatch:
6+
inputs:
7+
tag:
8+
description: 'Git tag to publish (e.g. v1.8.0)'
9+
required: true
10+
# Automatic trigger when a draft becomes “published”
11+
release:
12+
types: [published]
13+
14+
###############################################################################
15+
# ⬇️ Default permissions for the whole workflow: pull code + request OIDC
16+
###############################################################################
17+
permissions:
18+
contents: read # required by gh release download
19+
id-token: write # 🔑 enables token-less “Trusted Publisher” uploads
20+
21+
jobs:
22+
publish-to-pypi:
23+
# ┌───────────── release event, skip prereleases ──────────────┐
24+
if: |
25+
(github.event_name == 'release' && github.event.release.prerelease == false) ||
26+
# └───────────── manual run – we’ll validate the tag ourselves ─┘
27+
(github.event_name == 'workflow_dispatch')
28+
runs-on: ubuntu-latest
29+
environment: pypi # GitHub Environment gate (reviewers etc.)
30+
31+
steps:
32+
- uses: actions/checkout@v3 # makes refs/tags/* available for validation
33+
34+
########################################################################
35+
# Resolve which tag to use and expose it via “outputs.tag”
36+
########################################################################
37+
- name: Determine tag
38+
id: tag
39+
run: |
40+
if [[ "${{ github.event_name }}" == "release" ]]; then
41+
echo "tag=${{ github.event.release.tag_name }}" >>"$GITHUB_OUTPUT"
42+
else
43+
echo "tag=${{ github.event.inputs.tag }}" >>"$GITHUB_OUTPUT"
44+
fi
45+
46+
# (optional) fail early if the tag does not exist in the repo
47+
if ! git rev-parse -q --verify "refs/tags/${TAG:-${{ steps.tag.outputs.tag }}}" >/dev/null; then
48+
echo "::error::Tag '${{ steps.tag.outputs.tag }}' not found in repository"
49+
exit 1
50+
fi
51+
52+
########################################################################
53+
# Download the assets that were attached to that tag’s release
54+
########################################################################
55+
- name: Download release assets
56+
env:
57+
GH_TOKEN: ${{ github.token }}
58+
run: |
59+
gh release download \
60+
"${{ steps.tag.outputs.tag }}" \
61+
--repo "${{ github.repository }}" \
62+
--dir dist
63+
64+
########################################################################
65+
# Publish everything in ./dist to PyPI via OIDC (no API token needed)
66+
########################################################################
67+
- name: Publish to PyPI
68+
uses: pypa/gh-action-pypi-publish@release/v1
69+
# default packages_dir is "dist/"

0 commit comments

Comments
 (0)