|
| 1 | +--- |
| 2 | +name: Release |
| 3 | + |
| 4 | +on: |
| 5 | + push: |
| 6 | + branches: |
| 7 | + - main |
| 8 | + paths: |
| 9 | + - "setup.py" |
| 10 | + - ".github/workflows/release.yml" |
| 11 | + |
| 12 | +jobs: |
| 13 | + release: |
| 14 | + name: Create GitHub Release |
| 15 | + runs-on: ubuntu-latest |
| 16 | + |
| 17 | + steps: |
| 18 | + - name: Checkout source code |
| 19 | + uses: actions/checkout@v4 |
| 20 | + |
| 21 | + - name: Set up Python |
| 22 | + uses: actions/setup-python@v5 |
| 23 | + with: |
| 24 | + python-version: '3.11' |
| 25 | + |
| 26 | + - name: Read version from setup.py |
| 27 | + id: getversion |
| 28 | + run: | |
| 29 | + echo "version=$(python ./setup.py --version)" >> $GITHUB_OUTPUT |
| 30 | + echo "Version detected: $(python ./setup.py --version)" |
| 31 | +
|
| 32 | + - name: Fetch tags |
| 33 | + run: git fetch --tags origin |
| 34 | + |
| 35 | + - name: Check if tag already exists |
| 36 | + id: tagcheck |
| 37 | + run: | |
| 38 | + if git rev-parse "v${{ steps.getversion.outputs.version }}" >/dev/null 2>&1; then |
| 39 | + echo "Tag v${{ steps.getversion.outputs.version }} already exists" |
| 40 | + echo "should_release=false" >> $GITHUB_OUTPUT |
| 41 | + else |
| 42 | + echo "Tag v${{ steps.getversion.outputs.version }} does not exist - will proceed with release" |
| 43 | + echo "should_release=true" >> $GITHUB_OUTPUT |
| 44 | + fi |
| 45 | +
|
| 46 | + - name: Extract changelog for version |
| 47 | + if: steps.tagcheck.outputs.should_release == 'true' |
| 48 | + id: changelog |
| 49 | + run: | |
| 50 | + VERSION="${{ steps.getversion.outputs.version }}" |
| 51 | + # Extract content between ## [VERSION] and the next ## [ |
| 52 | + CHANGELOG=$(sed -n "/## \[$VERSION\]/,/## \[/p" CHANGELOG.md | sed '$d' | tail -n +2) |
| 53 | + if [ -z "$CHANGELOG" ]; then |
| 54 | + echo "No changelog entry found for version $VERSION" |
| 55 | + CHANGELOG="No changelog entry found. Please update CHANGELOG.md." |
| 56 | + fi |
| 57 | + # Use EOF delimiter for multi-line output |
| 58 | + echo "notes<<EOF" >> $GITHUB_OUTPUT |
| 59 | + echo "$CHANGELOG" >> $GITHUB_OUTPUT |
| 60 | + echo "EOF" >> $GITHUB_OUTPUT |
| 61 | +
|
| 62 | + - name: Create GitHub Release |
| 63 | + if: steps.tagcheck.outputs.should_release == 'true' |
| 64 | + uses: softprops/action-gh-release@v2 |
| 65 | + with: |
| 66 | + tag_name: v${{ steps.getversion.outputs.version }} |
| 67 | + name: Release v${{ steps.getversion.outputs.version }} |
| 68 | + draft: false |
| 69 | + prerelease: false |
| 70 | + body: ${{ steps.changelog.outputs.notes }} |
| 71 | + env: |
| 72 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 73 | + |
| 74 | + - name: Calculate next version |
| 75 | + if: steps.tagcheck.outputs.should_release == 'true' |
| 76 | + id: nextversion |
| 77 | + run: | |
| 78 | + CURRENT="${{ steps.getversion.outputs.version }}" |
| 79 | + # Split version into parts (assumes X.Y.Z format) |
| 80 | + IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT" |
| 81 | + # Bump patch version |
| 82 | + NEXT_PATCH=$((PATCH + 1)) |
| 83 | + NEXT_VERSION="${MAJOR}.${MINOR}.${NEXT_PATCH}" |
| 84 | + echo "next=$NEXT_VERSION" >> $GITHUB_OUTPUT |
| 85 | + echo "Next version will be: $NEXT_VERSION" |
| 86 | +
|
| 87 | + - name: Update setup.py version |
| 88 | + if: steps.tagcheck.outputs.should_release == 'true' |
| 89 | + run: | |
| 90 | + sed -i 's/version="${{ steps.getversion.outputs.version }}"/version="${{ steps.nextversion.outputs.next }}"/' setup.py |
| 91 | +
|
| 92 | + - name: Update README.md version |
| 93 | + if: steps.tagcheck.outputs.should_release == 'true' |
| 94 | + run: | |
| 95 | + sed -i 's/rev: v${{ steps.getversion.outputs.version }}/rev: v${{ steps.nextversion.outputs.next }}/g' README.md |
| 96 | +
|
| 97 | + - name: Update CHANGELOG.md |
| 98 | + if: steps.tagcheck.outputs.should_release == 'true' |
| 99 | + run: | |
| 100 | + CURRENT="${{ steps.getversion.outputs.version }}" |
| 101 | + NEXT="${{ steps.nextversion.outputs.next }}" |
| 102 | + TODAY=$(date +%Y-%m-%d) |
| 103 | +
|
| 104 | + # Create temp file with new changelog section |
| 105 | + cat > /tmp/changelog_update.txt << 'ENDOFCHANGELOG' |
| 106 | + ## [Unreleased] |
| 107 | +
|
| 108 | + Nothing yet. |
| 109 | +
|
| 110 | + ## [NEXT_VERSION] - TODAY_PLACEHOLDER |
| 111 | + ENDOFCHANGELOG |
| 112 | +
|
| 113 | + # Replace placeholders |
| 114 | + sed -i "s/NEXT_VERSION/$NEXT/g" /tmp/changelog_update.txt |
| 115 | + sed -i "s/TODAY_PLACEHOLDER/$TODAY/g" /tmp/changelog_update.txt |
| 116 | +
|
| 117 | + # Insert new section after the first "## [Unreleased]" line |
| 118 | + sed -i "/## \[Unreleased\]/r /tmp/changelog_update.txt" CHANGELOG.md |
| 119 | + # Remove the old "## [Unreleased]" and "Nothing yet." lines |
| 120 | + sed -i '1,/Nothing yet\./ { /## \[Unreleased\]/d; /Nothing yet\./d }' CHANGELOG.md |
| 121 | +
|
| 122 | + # Update version comparison links at bottom |
| 123 | + # Update [Unreleased] link |
| 124 | + sed -i "s|\[Unreleased\]:.*|[Unreleased]: https://github.com/homebysix/pre-commit-macadmin/compare/v$NEXT...HEAD|" CHANGELOG.md |
| 125 | + # Add new version link after [Unreleased] |
| 126 | + sed -i "/\[Unreleased\]:/a [$NEXT]: https://github.com/homebysix/pre-commit-macadmin/compare/v$CURRENT...v$NEXT" CHANGELOG.md |
| 127 | +
|
| 128 | + - name: Commit version bump |
| 129 | + if: steps.tagcheck.outputs.should_release == 'true' |
| 130 | + run: | |
| 131 | + git config user.name "github-actions[bot]" |
| 132 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 133 | + git add setup.py README.md CHANGELOG.md |
| 134 | + git commit -m "Bump to ${{ steps.nextversion.outputs.next }} [skip ci]" |
| 135 | +
|
| 136 | + - name: Push changes |
| 137 | + if: steps.tagcheck.outputs.should_release == 'true' |
| 138 | + run: | |
| 139 | + git push origin main |
0 commit comments