|
| 1 | +name: Release |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + version: |
| 7 | + description: "Version to release (e.g. 0.2.0)" |
| 8 | + required: true |
| 9 | + type: string |
| 10 | + dry_run: |
| 11 | + description: "Preview without making changes" |
| 12 | + required: false |
| 13 | + type: boolean |
| 14 | + default: false |
| 15 | + |
| 16 | +jobs: |
| 17 | + release: |
| 18 | + if: github.repository == 'EndstoneMC/dwarf2cpp' |
| 19 | + runs-on: ubuntu-latest |
| 20 | + permissions: |
| 21 | + contents: write |
| 22 | + steps: |
| 23 | + - name: Checkout Code |
| 24 | + uses: actions/checkout@v6 |
| 25 | + with: |
| 26 | + fetch-depth: 0 |
| 27 | + |
| 28 | + - name: Validate version |
| 29 | + run: | |
| 30 | + VERSION="${{ inputs.version }}" |
| 31 | + if [[ ! "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 32 | + echo "::error::Invalid version format: $VERSION (expected X.Y.Z)" |
| 33 | + exit 1 |
| 34 | + fi |
| 35 | + if git tag -l "v$VERSION" | grep -q .; then |
| 36 | + echo "::error::Tag v$VERSION already exists" |
| 37 | + exit 1 |
| 38 | + fi |
| 39 | + echo "VERSION=$VERSION" >> "$GITHUB_ENV" |
| 40 | +
|
| 41 | + - name: Extract unreleased changelog |
| 42 | + run: | |
| 43 | + BODY=$(sed -n '/^## \[Unreleased\]/,/^## \[/{/^## \[/d; p}' CHANGELOG.md) |
| 44 | + BODY=$(echo "$BODY" | sed '/./,$!d' | sed -e :a -e '/^\n*$/{$d;N;ba}') |
| 45 | + if [ -z "$BODY" ]; then |
| 46 | + echo "::error::Unreleased section in CHANGELOG.md is empty" |
| 47 | + exit 1 |
| 48 | + fi |
| 49 | + echo "$BODY" > /tmp/release_body.md |
| 50 | + echo "Extracted changelog:" |
| 51 | + echo "$BODY" |
| 52 | +
|
| 53 | + - name: Preview |
| 54 | + if: inputs.dry_run |
| 55 | + run: | |
| 56 | + DATE=$(date -u +%Y-%m-%d) |
| 57 | + echo "=== Dry Run ===" |
| 58 | + echo "Version: $VERSION" |
| 59 | + echo "Tag: v$VERSION" |
| 60 | + echo "Date: $DATE" |
| 61 | + echo "" |
| 62 | + echo "=== Release Body ===" |
| 63 | + cat /tmp/release_body.md |
| 64 | +
|
| 65 | + - name: Update version in pyproject.toml |
| 66 | + if: ${{ !inputs.dry_run }} |
| 67 | + run: sed -i "s/^version = \".*\"/version = \"$VERSION\"/" pyproject.toml |
| 68 | + |
| 69 | + - name: Update CHANGELOG.md |
| 70 | + if: ${{ !inputs.dry_run }} |
| 71 | + run: | |
| 72 | + DATE=$(date -u +%Y-%m-%d) |
| 73 | + sed -i "s/^## \[Unreleased\]/## [Unreleased]\n\n## [$VERSION] - $DATE/" CHANGELOG.md |
| 74 | +
|
| 75 | + - name: Commit and tag |
| 76 | + if: ${{ !inputs.dry_run }} |
| 77 | + run: | |
| 78 | + git config user.name "github-actions[bot]" |
| 79 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 80 | + git add CHANGELOG.md pyproject.toml |
| 81 | + git commit -m "Release $VERSION" |
| 82 | + git tag "v$VERSION" |
| 83 | +
|
| 84 | + - name: Push |
| 85 | + if: ${{ !inputs.dry_run }} |
| 86 | + run: | |
| 87 | + git push origin main |
| 88 | + git push origin "v$VERSION" |
| 89 | +
|
| 90 | + - name: Create GitHub release |
| 91 | + if: ${{ !inputs.dry_run }} |
| 92 | + env: |
| 93 | + GH_TOKEN: ${{ github.token }} |
| 94 | + run: | |
| 95 | + PREV_TAG=$(git tag --sort=-v:refname | sed -n '2p') |
| 96 | + if [ -n "$PREV_TAG" ]; then |
| 97 | + printf '\n**Full Changelog**: https://github.com/%s/compare/%s...v%s\n' \ |
| 98 | + "${{ github.repository }}" "$PREV_TAG" "$VERSION" >> /tmp/release_body.md |
| 99 | + fi |
| 100 | + gh release create "v$VERSION" \ |
| 101 | + --title "v$VERSION" \ |
| 102 | + --notes-file /tmp/release_body.md |
| 103 | +
|
| 104 | + build_wheels: |
| 105 | + if: ${{ !inputs.dry_run }} |
| 106 | + needs: [ release ] |
| 107 | + name: Build wheels on ${{ matrix.os }} |
| 108 | + runs-on: ${{ matrix.os }} |
| 109 | + strategy: |
| 110 | + fail-fast: false |
| 111 | + matrix: |
| 112 | + os: [ ubuntu-latest, windows-latest, macos-14 ] |
| 113 | + permissions: |
| 114 | + contents: write |
| 115 | + steps: |
| 116 | + - name: Checkout Code |
| 117 | + uses: actions/checkout@v6 |
| 118 | + with: |
| 119 | + ref: v${{ inputs.version }} |
| 120 | + |
| 121 | + - name: Build wheels |
| 122 | + uses: pypa/cibuildwheel@v3.1.4 |
| 123 | + env: |
| 124 | + CIBW_SKIP: "*-win32 *-manylinux_i686" |
| 125 | + |
| 126 | + - name: Attach wheels to release |
| 127 | + env: |
| 128 | + GH_TOKEN: ${{ github.token }} |
| 129 | + run: gh release upload "v${{ inputs.version }}" wheelhouse/*.whl |
0 commit comments