|
| 1 | +name: Release |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + version: |
| 7 | + description: "Version to release (e.g. 0.1.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/cpp-example-plugin' |
| 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 CHANGELOG.md |
| 66 | + if: ${{ !inputs.dry_run }} |
| 67 | + run: | |
| 68 | + DATE=$(date -u +%Y-%m-%d) |
| 69 | + sed -i "s/^## \[Unreleased\]/## [Unreleased]\n\n## [$VERSION] - $DATE/" CHANGELOG.md |
| 70 | +
|
| 71 | + - name: Commit and tag |
| 72 | + if: ${{ !inputs.dry_run }} |
| 73 | + run: | |
| 74 | + git config user.name "github-actions[bot]" |
| 75 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 76 | + git add CHANGELOG.md |
| 77 | + git commit -m "Release $VERSION" |
| 78 | + git tag "v$VERSION" |
| 79 | +
|
| 80 | + - name: Push |
| 81 | + if: ${{ !inputs.dry_run }} |
| 82 | + run: | |
| 83 | + git push origin main |
| 84 | + git push origin "v$VERSION" |
| 85 | +
|
| 86 | + - name: Create GitHub release |
| 87 | + if: ${{ !inputs.dry_run }} |
| 88 | + env: |
| 89 | + GH_TOKEN: ${{ github.token }} |
| 90 | + run: | |
| 91 | + PREV_TAG=$(git tag --sort=-v:refname | sed -n '2p') |
| 92 | + if [ -n "$PREV_TAG" ]; then |
| 93 | + printf '\n**Full Changelog**: https://github.com/%s/compare/%s...v%s\n' \ |
| 94 | + "${{ github.repository }}" "$PREV_TAG" "$VERSION" >> /tmp/release_body.md |
| 95 | + fi |
| 96 | + gh release create "v$VERSION" \ |
| 97 | + --title "v$VERSION" \ |
| 98 | + --notes-file /tmp/release_body.md |
| 99 | +
|
| 100 | + build_windows: |
| 101 | + if: ${{ !inputs.dry_run }} |
| 102 | + needs: [ release ] |
| 103 | + runs-on: windows-2022 |
| 104 | + permissions: |
| 105 | + contents: write |
| 106 | + steps: |
| 107 | + - name: Checkout Code |
| 108 | + uses: actions/checkout@v6 |
| 109 | + with: |
| 110 | + ref: v${{ inputs.version }} |
| 111 | + |
| 112 | + - name: Set up MSVC |
| 113 | + uses: ilammy/msvc-dev-cmd@v1 |
| 114 | + with: |
| 115 | + arch: x86_64 |
| 116 | + |
| 117 | + - name: Set up CMake and Ninja |
| 118 | + uses: lukka/get-cmake@latest |
| 119 | + |
| 120 | + - name: Build with CMake |
| 121 | + run: | |
| 122 | + cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo |
| 123 | + cmake --build build |
| 124 | +
|
| 125 | + - name: Attach to release |
| 126 | + env: |
| 127 | + GH_TOKEN: ${{ github.token }} |
| 128 | + run: | |
| 129 | + gh release upload "v${{ inputs.version }}" build/endstone_*.dll build/endstone_*.pdb |
| 130 | +
|
| 131 | + build_linux: |
| 132 | + if: ${{ !inputs.dry_run }} |
| 133 | + needs: [ release ] |
| 134 | + runs-on: ubuntu-22.04 |
| 135 | + permissions: |
| 136 | + contents: write |
| 137 | + steps: |
| 138 | + - name: Checkout Code |
| 139 | + uses: actions/checkout@v6 |
| 140 | + with: |
| 141 | + ref: v${{ inputs.version }} |
| 142 | + |
| 143 | + - name: Set up Clang 18 |
| 144 | + env: |
| 145 | + LLVM_VERSION: 18 |
| 146 | + run: | |
| 147 | + sudo apt-get update -y -q |
| 148 | + sudo apt-get install -y -q lsb-release wget software-properties-common gnupg |
| 149 | + sudo wget https://apt.llvm.org/llvm.sh |
| 150 | + sudo chmod +x llvm.sh |
| 151 | + sudo ./llvm.sh ${LLVM_VERSION} |
| 152 | + sudo apt-get install -y -q libc++-${LLVM_VERSION}-dev libc++abi-${LLVM_VERSION}-dev |
| 153 | + sudo update-alternatives --install /usr/bin/cc cc /usr/bin/clang-${LLVM_VERSION} 100 |
| 154 | + sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/clang++-${LLVM_VERSION} 100 |
| 155 | +
|
| 156 | + - name: Set up CMake and Ninja |
| 157 | + uses: lukka/get-cmake@latest |
| 158 | + |
| 159 | + - name: Build with CMake |
| 160 | + run: | |
| 161 | + cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo |
| 162 | + cmake --build build |
| 163 | +
|
| 164 | + - name: Attach to release |
| 165 | + env: |
| 166 | + GH_TOKEN: ${{ github.token }} |
| 167 | + run: gh release upload "v${{ inputs.version }}" build/endstone_*.so |
0 commit comments