|
| 1 | +name: Create Release |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + bump: |
| 7 | + description: "Version bump type" |
| 8 | + required: true |
| 9 | + type: choice |
| 10 | + options: |
| 11 | + - major |
| 12 | + - minor |
| 13 | + - patch |
| 14 | + |
| 15 | +jobs: |
| 16 | + build-release-publish: |
| 17 | + name: Build, Release, Publish |
| 18 | + runs-on: ubuntu-latest |
| 19 | + permissions: |
| 20 | + contents: write |
| 21 | + id-token: write |
| 22 | + steps: |
| 23 | + - name: Checkout code |
| 24 | + uses: actions/checkout@v4 |
| 25 | + with: |
| 26 | + fetch-depth: 0 |
| 27 | + |
| 28 | + - name: Install Python |
| 29 | + uses: actions/setup-python@v6 |
| 30 | + with: |
| 31 | + python-version: "3.13" |
| 32 | + |
| 33 | + - name: Install uv |
| 34 | + uses: astral-sh/setup-uv@v7 |
| 35 | + |
| 36 | + - name: Install dependencies |
| 37 | + run: uv sync --extra dev |
| 38 | + |
| 39 | + - name: Run tests |
| 40 | + run: uv run pytest tests |
| 41 | + |
| 42 | + - name: Bump version in pyproject.toml |
| 43 | + id: bump_version |
| 44 | + env: |
| 45 | + BUMP_TYPE: ${{ github.event.inputs.bump }} |
| 46 | + run: | |
| 47 | + python - <<'PY' |
| 48 | + import os |
| 49 | + import re |
| 50 | +
|
| 51 | + path = "pyproject.toml" |
| 52 | + bump = os.environ["BUMP_TYPE"] |
| 53 | +
|
| 54 | + with open(path, "r", encoding="utf-8") as f: |
| 55 | + content = f.read() |
| 56 | +
|
| 57 | + m = re.search(r'^version\s*=\s*"(\d+)\.(\d+)\.(\d+)"\s*$', content, re.MULTILINE) |
| 58 | + if not m: |
| 59 | + raise SystemExit("Could not find project version in pyproject.toml") |
| 60 | +
|
| 61 | + major, minor, patch = map(int, m.groups()) |
| 62 | + if bump == "major": |
| 63 | + major += 1 |
| 64 | + minor = 0 |
| 65 | + patch = 0 |
| 66 | + elif bump == "minor": |
| 67 | + minor += 1 |
| 68 | + patch = 0 |
| 69 | + elif bump == "patch": |
| 70 | + patch += 1 |
| 71 | + else: |
| 72 | + raise SystemExit(f"Unsupported bump type: {bump}") |
| 73 | +
|
| 74 | + new_version = f"{major}.{minor}.{patch}" |
| 75 | + new_line = f'version = "{new_version}"' |
| 76 | + content = re.sub(r'^version\s*=\s*"\d+\.\d+\.\d+"\s*$', new_line, content, count=1, flags=re.MULTILINE) |
| 77 | +
|
| 78 | + with open(path, "w", encoding="utf-8") as f: |
| 79 | + f.write(content) |
| 80 | +
|
| 81 | + with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as f: |
| 82 | + f.write(f"new_version={new_version}\n") |
| 83 | + PY |
| 84 | +
|
| 85 | + - name: Commit, tag, and push |
| 86 | + env: |
| 87 | + NEW_VERSION: ${{ steps.bump_version.outputs.new_version }} |
| 88 | + run: | |
| 89 | + git config user.name "github-actions[bot]" |
| 90 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 91 | + git add pyproject.toml |
| 92 | + git commit -m "chore: release ${NEW_VERSION}" |
| 93 | + git tag "${NEW_VERSION}" |
| 94 | + git push origin HEAD |
| 95 | + git push origin "${NEW_VERSION}" |
| 96 | +
|
| 97 | + - name: Build |
| 98 | + run: uv build |
| 99 | + |
| 100 | + - name: Create GitHub release and upload artifacts |
| 101 | + env: |
| 102 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 103 | + NEW_VERSION: ${{ steps.bump_version.outputs.new_version }} |
| 104 | + run: | |
| 105 | + gh release create "${NEW_VERSION}" dist/* --title "${NEW_VERSION}" --notes "Release ${NEW_VERSION}" |
| 106 | +
|
| 107 | + - name: Publish package to PyPI |
| 108 | + uses: pypa/gh-action-pypi-publish@release/v1 |
0 commit comments