|
| 1 | +name: Release and Publish |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - master |
| 7 | + workflow_dispatch: |
| 8 | + |
| 9 | +jobs: |
| 10 | + release: |
| 11 | + name: Create Release and Publish to PyPI |
| 12 | + runs-on: ubuntu-latest |
| 13 | + |
| 14 | + steps: |
| 15 | + - name: Check out repository |
| 16 | + uses: actions/checkout@v5 |
| 17 | + with: |
| 18 | + fetch-depth: 0 # ensure full history & tags |
| 19 | + |
| 20 | + - name: Fetch tags |
| 21 | + run: | |
| 22 | + git fetch --tags --force |
| 23 | +
|
| 24 | + - name: Set up Python |
| 25 | + uses: actions/setup-python@v6 |
| 26 | + with: |
| 27 | + python-version: "3.12" |
| 28 | + |
| 29 | + - name: Install tooling (packaging) |
| 30 | + run: | |
| 31 | + python -m pip install --upgrade pip |
| 32 | + pip install packaging |
| 33 | +
|
| 34 | + # 1) Extract the new version from pyproject.toml |
| 35 | + - name: Extract version from pyproject.toml |
| 36 | + id: get_version |
| 37 | + shell: bash |
| 38 | + run: | |
| 39 | + python - << 'PY' > version.txt |
| 40 | + import tomllib |
| 41 | + from pathlib import Path |
| 42 | +
|
| 43 | + data = tomllib.loads(Path("pyproject.toml").read_text(encoding="utf-8")) |
| 44 | + version = data["project"]["version"] |
| 45 | + print(version) |
| 46 | + PY |
| 47 | + version="$(cat version.txt)" |
| 48 | + echo "New version from pyproject.toml: $version" |
| 49 | + echo "version=$version" >> "$GITHUB_OUTPUT" |
| 50 | +
|
| 51 | + # 2) Get the previous tag in format "x.x.x" |
| 52 | + - name: Get previous semantic tag |
| 53 | + id: get_prev_tag |
| 54 | + shell: bash |
| 55 | + run: | |
| 56 | + # Look for tags that look like x.x.x (no leading "v") |
| 57 | + prev_tag="$(git tag --list '[0-9]*.[0-9]*.[0-9]*' --sort=-v:refname | head -n 1 || true)" |
| 58 | +
|
| 59 | + if [ -z "$prev_tag" ]; then |
| 60 | + echo "No previous semantic tag found (x.x.x). Exiting workflow." |
| 61 | + exit 1 |
| 62 | + else |
| 63 | + echo "Found previous tag: $prev_tag" |
| 64 | + echo "previous_tag=$prev_tag" >> "$GITHUB_OUTPUT" |
| 65 | + echo "previous_version=$prev_tag" >> "$GITHUB_OUTPUT" |
| 66 | + fi |
| 67 | +
|
| 68 | + # 3) Ensure the new version is greater than the previous tag's version |
| 69 | + - name: Ensure new version is greater than previous |
| 70 | + if: steps.get_prev_tag.outputs.previous_version != '' |
| 71 | + env: |
| 72 | + NEW_VERSION: ${{ steps.get_version.outputs.version }} |
| 73 | + OLD_VERSION: ${{ steps.get_prev_tag.outputs.previous_version }} |
| 74 | + shell: python |
| 75 | + run: | |
| 76 | + import os |
| 77 | + import sys |
| 78 | + from packaging.version import Version |
| 79 | +
|
| 80 | + new = Version(os.environ["NEW_VERSION"]) |
| 81 | + old = Version(os.environ["OLD_VERSION"]) |
| 82 | +
|
| 83 | + print(f"Previous version: {old}") |
| 84 | + print(f"New version: {new}") |
| 85 | +
|
| 86 | + if new <= old: |
| 87 | + print(f"Error: new version {new} is not greater than previous {old}.") |
| 88 | + sys.exit(1) |
| 89 | + else: |
| 90 | + print(f"OK: new version {new} is greater than previous {old}.") |
| 91 | +
|
| 92 | + - name: Install build dependencies |
| 93 | + run: | |
| 94 | + python -m pip install --upgrade pip |
| 95 | + pip install build |
| 96 | +
|
| 97 | + - name: Build distribution |
| 98 | + run: | |
| 99 | + python -m build |
| 100 | +
|
| 101 | + - name: Create GitHub Release |
| 102 | + uses: softprops/action-gh-release@v2 |
| 103 | + env: |
| 104 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 105 | + with: |
| 106 | + tag_name: "${{ steps.get_version.outputs.version }}" # e.g. "1.2.3" |
| 107 | + name: "${{ steps.get_version.outputs.version }}" |
| 108 | + generate_release_notes: true |
| 109 | + draft: false |
| 110 | + prerelease: false |
| 111 | + files: | |
| 112 | + dist/* |
| 113 | +
|
| 114 | + - name: Publish to PyPI |
| 115 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 116 | + with: |
| 117 | + password: ${{ secrets.PYPI_API_TOKEN }} |
| 118 | + # skip-existing: true |
| 119 | + |
| 120 | + - name: Configure Git for mike |
| 121 | + run: | |
| 122 | + git config user.name github-actions |
| 123 | + git config user.email [email protected] |
| 124 | +
|
| 125 | + - name: Fetch gh-pages branch for mike |
| 126 | + run: | |
| 127 | + git fetch origin gh-pages --depth=1 || true |
| 128 | +
|
| 129 | + - name: Install documentation dependencies |
| 130 | + run: | |
| 131 | + pip install .[docs] |
| 132 | +
|
| 133 | + - name: Deploy documentation with mike |
| 134 | + env: |
| 135 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 136 | + run: | |
| 137 | + mike deploy --push --update-aliases ${{ steps.get_version.outputs.version }} latest |
| 138 | + mike set-default --push latest |
0 commit comments