|
| 1 | +name: Release (Trusted Publisher) |
| 2 | + |
| 3 | +permissions: |
| 4 | + contents: write # Required for creating releases and pushing tags |
| 5 | + id-token: write # Required for PyPI Trusted Publishing |
| 6 | + |
| 7 | +on: |
| 8 | + workflow_dispatch: |
| 9 | + inputs: |
| 10 | + release_type: |
| 11 | + description: 'Release type (major, minor, patch)' |
| 12 | + required: true |
| 13 | + type: choice |
| 14 | + options: |
| 15 | + - patch |
| 16 | + - minor |
| 17 | + - major |
| 18 | + |
| 19 | +jobs: |
| 20 | + release: |
| 21 | + runs-on: ubuntu-latest |
| 22 | + |
| 23 | + steps: |
| 24 | + - name: Checkout code |
| 25 | + uses: actions/checkout@v3 |
| 26 | + with: |
| 27 | + fetch-depth: 0 # Fetch all history and tags |
| 28 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 29 | + |
| 30 | + - name: Fetch all tags |
| 31 | + run: | |
| 32 | + git fetch --tags --force |
| 33 | +
|
| 34 | + - name: Set up Python |
| 35 | + uses: actions/setup-python@v4 |
| 36 | + with: |
| 37 | + python-version: '3.9' |
| 38 | + |
| 39 | + - name: Configure Git |
| 40 | + run: | |
| 41 | + git config --global user.name "github-actions[bot]" |
| 42 | + git config --global user.email "github-actions[bot]@users.noreply.github.com" |
| 43 | +
|
| 44 | + - name: Calculate next version |
| 45 | + id: next_version |
| 46 | + shell: bash |
| 47 | + run: | |
| 48 | + # Get the latest tag, default to v0.0.0 if no tags exist |
| 49 | + LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0") |
| 50 | + echo "Latest tag: $LATEST_TAG" |
| 51 | +
|
| 52 | + # Remove 'v' prefix and split into components |
| 53 | + VERSION=${LATEST_TAG#v} |
| 54 | + IFS='.' read -r -a VERSION_PARTS <<< "$VERSION" |
| 55 | + MAJOR="${VERSION_PARTS[0]:-0}" |
| 56 | + MINOR="${VERSION_PARTS[1]:-0}" |
| 57 | + PATCH="${VERSION_PARTS[2]:-0}" |
| 58 | +
|
| 59 | + echo "Current version: $MAJOR.$MINOR.$PATCH" |
| 60 | +
|
| 61 | + # Increment based on release type |
| 62 | + case "${{ github.event.inputs.release_type }}" in |
| 63 | + major) |
| 64 | + MAJOR=$((MAJOR + 1)) |
| 65 | + MINOR=0 |
| 66 | + PATCH=0 |
| 67 | + ;; |
| 68 | + minor) |
| 69 | + MINOR=$((MINOR + 1)) |
| 70 | + PATCH=0 |
| 71 | + ;; |
| 72 | + patch) |
| 73 | + PATCH=$((PATCH + 1)) |
| 74 | + ;; |
| 75 | + esac |
| 76 | +
|
| 77 | + NEW_VERSION="v${MAJOR}.${MINOR}.${PATCH}" |
| 78 | + echo "New version: $NEW_VERSION" |
| 79 | + echo "version=$NEW_VERSION" >> $GITHUB_OUTPUT |
| 80 | +
|
| 81 | + - name: Create and push tag |
| 82 | + run: | |
| 83 | + git tag ${{ steps.next_version.outputs.version }} |
| 84 | + git push origin ${{ steps.next_version.outputs.version }} |
| 85 | +
|
| 86 | + - name: Install build dependencies |
| 87 | + run: | |
| 88 | + python -m pip install --upgrade pip |
| 89 | + python -m pip install build |
| 90 | +
|
| 91 | + - name: Build package |
| 92 | + run: | |
| 93 | + python -m build |
| 94 | +
|
| 95 | + - name: Upload to PyPI using Trusted Publisher |
| 96 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 97 | + with: |
| 98 | + packages-dir: dist/ |
| 99 | + |
| 100 | + - name: Create GitHub Release |
| 101 | + env: |
| 102 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 103 | + run: | |
| 104 | + gh release create ${{ steps.next_version.outputs.version }} \ |
| 105 | + --title "${{ steps.next_version.outputs.version }}" \ |
| 106 | + --generate-notes \ |
| 107 | + dist/* |
| 108 | +
|
| 109 | + - name: Print summary |
| 110 | + if: success() |
| 111 | + run: | |
| 112 | + echo "### :rocket: Release ${{ steps.next_version.outputs.version }} completed successfully!" >> $GITHUB_STEP_SUMMARY |
| 113 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 114 | + echo "- **Release Type:** ${{ github.event.inputs.release_type }}" >> $GITHUB_STEP_SUMMARY |
| 115 | + echo "- **New Version:** ${{ steps.next_version.outputs.version }}" >> $GITHUB_STEP_SUMMARY |
| 116 | + echo "- **PyPI Package:** Published via Trusted Publisher" >> $GITHUB_STEP_SUMMARY |
| 117 | + echo "- **GitHub Release:** Created with auto-generated notes" >> $GITHUB_STEP_SUMMARY |
| 118 | +
|
| 119 | + - name: Print failure message |
| 120 | + if: failure() |
| 121 | + run: | |
| 122 | + echo "### :x: Release failed. Please check the logs above." >> $GITHUB_STEP_SUMMARY |
0 commit comments