diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml
new file mode 100644
index 0000000..c9d0e64
--- /dev/null
+++ b/.github/workflows/publish.yml
@@ -0,0 +1,77 @@
+# Publish fa-bench to PyPI with trusted publishing, so no token is stored anywhere.
+#
+# Runs when a v* tag reaches GitHub, or by hand (Actions > publish > Run workflow) for a
+# tag that already exists, such as v2.1.0, which was tagged before this workflow did.
+# PyPI side: a (pending) publisher for olewave/fa-bench, workflow publish.yml, environment pypi.
+name: publish
+
+on:
+ push:
+ tags: ["v*"]
+ workflow_dispatch:
+ inputs:
+ tag:
+ description: "Tag to publish, for example v2.1.0"
+ required: true
+
+permissions:
+ contents: read
+
+jobs:
+ build:
+ runs-on: ubuntu-latest
+ env:
+ TAG: ${{ inputs.tag || github.ref_name }}
+ steps:
+ - uses: actions/checkout@v4
+ with:
+ ref: refs/tags/${{ env.TAG }}
+ - uses: actions/setup-python@v5
+ with:
+ python-version: "3.12"
+ - name: The tag matches the package version
+ run: |
+ v=$(python -c "import re,pathlib; print(re.search(r'__version__ = \"(.+?)\"', pathlib.Path('fabench/__init__.py').read_text())[1])")
+ test "v$v" = "$TAG" || { echo "tag $TAG but fabench.__version__ is $v"; exit 1; }
+ # PyPI shows README.md with no repository to resolve relative paths against, and a
+ # release's description can't change after upload: pin every relative link to the tag.
+ - name: Point the README's relative links at GitHub, pinned to the tag
+ run: |
+ python - <<'EOF'
+ import os, re
+ from pathlib import Path
+ tag, repo = os.environ["TAG"], "olewave/fa-bench"
+ def fix(u, image):
+ if re.match(r"[a-z][a-z0-9+.-]*:|#|/", u):
+ return u
+ u = u.removeprefix("./")
+ return f"https://raw.githubusercontent.com/{repo}/{tag}/{u}" if image else f"https://github.com/{repo}/blob/{tag}/{u}"
+ p = Path("README.md")
+ t = p.read_text().replace("tycho.olewave.com", "dashboard.olewave.com")
+ t = re.sub(r'(
]*\bsrc=")([^"]+)"', lambda m: m[1] + fix(m[2], True) + '"', t)
+ t = re.sub(r'(]*\bhref=")([^"]+)"', lambda m: m[1] + fix(m[2], False) + '"', t)
+ t = re.sub(r'(!?)\[([^\]]*)\]\(([^)\s]+)\)', lambda m: f"{m[1]}[{m[2]}]({fix(m[3], m[1] == '!')})", t)
+ p.write_text(t)
+ EOF
+ - name: Build
+ run: |
+ python -m pip install --upgrade build twine
+ python -m build
+ python -m twine check --strict dist/*
+ - uses: actions/upload-artifact@v4
+ with:
+ name: dist
+ path: dist/
+
+ pypi:
+ needs: build
+ runs-on: ubuntu-latest
+ environment: pypi
+ permissions:
+ id-token: write
+ steps:
+ - uses: actions/download-artifact@v4
+ with:
+ name: dist
+ path: dist/
+ - uses: pypa/gh-action-pypi-publish@release/v1