|
| 1 | +name: Build and Release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: ['v*'] |
| 6 | + workflow_dispatch: |
| 7 | + |
| 8 | +jobs: |
| 9 | + build: |
| 10 | + permissions: |
| 11 | + contents: write |
| 12 | + strategy: |
| 13 | + fail-fast: false |
| 14 | + matrix: |
| 15 | + include: |
| 16 | + - platform: macos-latest |
| 17 | + target: aarch64-apple-darwin |
| 18 | + name: macos-aarch64 |
| 19 | + - platform: macos-latest |
| 20 | + target: x86_64-apple-darwin |
| 21 | + name: macos-x86_64 |
| 22 | + - platform: ubuntu-22.04 |
| 23 | + target: x86_64-unknown-linux-gnu |
| 24 | + name: linux-x86_64 |
| 25 | + - platform: windows-latest |
| 26 | + target: x86_64-pc-windows-msvc |
| 27 | + name: windows-x86_64 |
| 28 | + |
| 29 | + runs-on: ${{ matrix.platform }} |
| 30 | + |
| 31 | + steps: |
| 32 | + - uses: actions/checkout@v4 |
| 33 | + |
| 34 | + - name: Install Rust stable |
| 35 | + uses: dtolnay/rust-toolchain@stable |
| 36 | + with: |
| 37 | + targets: ${{ matrix.target }} |
| 38 | + |
| 39 | + - name: Rust cache |
| 40 | + uses: swatinem/rust-cache@v2 |
| 41 | + with: |
| 42 | + key: ${{ matrix.target }} |
| 43 | + |
| 44 | + - name: Build release binary |
| 45 | + run: cargo build --release --target ${{ matrix.target }} |
| 46 | + |
| 47 | + - name: Create release package |
| 48 | + shell: bash |
| 49 | + run: | |
| 50 | + mkdir -p release-package |
| 51 | + |
| 52 | + # 获取完整的 tag 名称 |
| 53 | + TAG_NAME="${{ github.ref_name }}" |
| 54 | + |
| 55 | + # 确定二进制文件名和扩展名 |
| 56 | + if [[ "${{ matrix.platform }}" == "windows-latest" ]]; then |
| 57 | + BINARY_NAME="code-nexus.exe" |
| 58 | + ARCHIVE_EXT="zip" |
| 59 | + else |
| 60 | + BINARY_NAME="code-nexus" |
| 61 | + ARCHIVE_EXT="tar.gz" |
| 62 | + fi |
| 63 | + |
| 64 | + # 复制二进制文件 |
| 65 | + cp "target/${{ matrix.target }}/release/$BINARY_NAME" release-package/ |
| 66 | + |
| 67 | + # 复制其他文件 |
| 68 | + cp README.md release-package/ || true |
| 69 | + cp LICENSE release-package/ || true |
| 70 | + |
| 71 | + # 创建压缩包 |
| 72 | + cd release-package |
| 73 | + if [[ "${{ matrix.platform }}" == "windows-latest" ]]; then |
| 74 | + 7z a "../code-nexus-${TAG_NAME}-${{ matrix.name }}.zip" * |
| 75 | + else |
| 76 | + tar -czf "../code-nexus-${TAG_NAME}-${{ matrix.name }}.tar.gz" * |
| 77 | + fi |
| 78 | +
|
| 79 | + - name: Upload artifacts |
| 80 | + uses: actions/upload-artifact@v4 |
| 81 | + with: |
| 82 | + name: code-nexus-${{ matrix.name }} |
| 83 | + path: | |
| 84 | + code-nexus-*.tar.gz |
| 85 | + code-nexus-*.zip |
| 86 | + if-no-files-found: ignore |
| 87 | + |
| 88 | + release: |
| 89 | + name: Create Release |
| 90 | + needs: build |
| 91 | + runs-on: ubuntu-latest |
| 92 | + if: startsWith(github.ref, 'refs/tags/v') |
| 93 | + |
| 94 | + steps: |
| 95 | + - name: Checkout code |
| 96 | + uses: actions/checkout@v4 |
| 97 | + with: |
| 98 | + fetch-depth: 0 |
| 99 | + |
| 100 | + - name: Verify version consistency |
| 101 | + run: | |
| 102 | + # 获取 git tag 版本号(去掉 v 前缀) |
| 103 | + TAG_NAME="${{ github.ref_name }}" |
| 104 | + TAG_VERSION_NUMBER=${TAG_NAME#v} |
| 105 | + |
| 106 | + # 从 Cargo.toml 读取版本号 |
| 107 | + PROJECT_VERSION=$(grep '^version = ' Cargo.toml | head -1 | cut -d'"' -f2) |
| 108 | + |
| 109 | + echo "Git tag version: ${TAG_VERSION_NUMBER}" |
| 110 | + echo "Cargo.toml version: ${PROJECT_VERSION}" |
| 111 | + |
| 112 | + # 检查版本是否一致 |
| 113 | + if [ "${TAG_VERSION_NUMBER}" != "${PROJECT_VERSION}" ]; then |
| 114 | + echo "❌ Version mismatch detected!" |
| 115 | + echo "Git tag version: ${TAG_VERSION_NUMBER}" |
| 116 | + echo "Cargo.toml version: ${PROJECT_VERSION}" |
| 117 | + echo "" |
| 118 | + echo "Please ensure the git tag matches the version in Cargo.toml." |
| 119 | + echo "You can either:" |
| 120 | + echo "1. Update Cargo.toml to match tag: ${TAG_VERSION_NUMBER}" |
| 121 | + echo "2. Create a new tag that matches Cargo.toml version: v${PROJECT_VERSION}" |
| 122 | + exit 1 |
| 123 | + fi |
| 124 | + |
| 125 | + echo "✅ Version consistency check passed: ${TAG_VERSION_NUMBER}" |
| 126 | +
|
| 127 | + - name: Download all artifacts |
| 128 | + uses: actions/download-artifact@v4 |
| 129 | + with: |
| 130 | + path: artifacts |
| 131 | + |
| 132 | + - name: Install git-cliff |
| 133 | + uses: taiki-e/install-action@git-cliff |
| 134 | + |
| 135 | + - name: Generate changelog and release title |
| 136 | + id: changelog |
| 137 | + run: | |
| 138 | + # 从 GitHub API 获取最新的 release 版本 |
| 139 | + echo "Fetching latest release from GitHub API..." |
| 140 | + LATEST_RELEASE=$(curl -s "https://api.github.com/repos/${{ github.repository }}/releases/latest" | jq -r '.tag_name // empty' 2>/dev/null || echo "") |
| 141 | + |
| 142 | + if [ -z "$LATEST_RELEASE" ] || [ "$LATEST_RELEASE" = "null" ]; then |
| 143 | + echo "GitHub API failed or no previous release found, falling back to git tags" |
| 144 | + # 回退到使用 git tag 获取上一个版本 |
| 145 | + PREVIOUS_TAG=$(git tag --sort=-version:refname | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | head -2 | tail -1) |
| 146 | + if [ -z "$PREVIOUS_TAG" ]; then |
| 147 | + echo "No previous tags found, generating full changelog" |
| 148 | + else |
| 149 | + echo "Found previous tag: $PREVIOUS_TAG" |
| 150 | + fi |
| 151 | + else |
| 152 | + PREVIOUS_TAG="$LATEST_RELEASE" |
| 153 | + echo "Found previous release: $PREVIOUS_TAG" |
| 154 | + fi |
| 155 | + |
| 156 | + echo "Current tag: ${{ github.ref_name }}" |
| 157 | + echo "Previous version: $PREVIOUS_TAG" |
| 158 | + |
| 159 | + if [ -z "$PREVIOUS_TAG" ]; then |
| 160 | + # 如果没有上一个版本,生成所有提交的 changelog |
| 161 | + git-cliff --tag ${{ github.ref_name }} --output changelog.md |
| 162 | + else |
| 163 | + # 只生成从上一个版本到当前版本的 changelog |
| 164 | + git-cliff $PREVIOUS_TAG..${{ github.ref_name }} --output changelog.md |
| 165 | + # 添加 Full Changelog 链接 |
| 166 | + echo "" >> changelog.md |
| 167 | + echo "**Full Changelog**: [$PREVIOUS_TAG...${{ github.ref_name }}](https://github.com/${{ github.repository }}/compare/$PREVIOUS_TAG...${{ github.ref_name }})" >> changelog.md |
| 168 | + fi |
| 169 | + |
| 170 | + # 生成发布标题 |
| 171 | + VERSION_NUMBER="${{ github.ref_name }}" |
| 172 | + VERSION_NUMBER=${VERSION_NUMBER#v} # 移除 v 前缀 |
| 173 | + |
| 174 | + # 从最近的提交中提取主要功能作为标题 |
| 175 | + if [ -z "$PREVIOUS_TAG" ]; then |
| 176 | + COMMIT_RANGE="${{ github.ref_name }}" |
| 177 | + else |
| 178 | + COMMIT_RANGE="$PREVIOUS_TAG..${{ github.ref_name }}" |
| 179 | + fi |
| 180 | + |
| 181 | + MAIN_FEATURE=$(git log --oneline $COMMIT_RANGE | grep -E "^[a-f0-9]+ (feat|fix)" | head -1 | sed 's/^[a-f0-9]* //' | sed 's/^feat: /✨ /' | sed 's/^fix: /🐞 /') |
| 182 | + |
| 183 | + if [ -z "$MAIN_FEATURE" ]; then |
| 184 | + RELEASE_TITLE="$VERSION_NUMBER 📦 版本更新" |
| 185 | + else |
| 186 | + RELEASE_TITLE="$VERSION_NUMBER $MAIN_FEATURE" |
| 187 | + fi |
| 188 | + |
| 189 | + echo "release_title=$RELEASE_TITLE" >> $GITHUB_OUTPUT |
| 190 | + echo "Generated release title: $RELEASE_TITLE" |
| 191 | +
|
| 192 | + - name: Create Release |
| 193 | + uses: softprops/action-gh-release@v1 |
| 194 | + with: |
| 195 | + files: | |
| 196 | + artifacts/*/code-nexus-*.tar.gz |
| 197 | + artifacts/*/code-nexus-*.zip |
| 198 | + draft: false |
| 199 | + prerelease: false |
| 200 | + generate_release_notes: false |
| 201 | + name: ${{ steps.changelog.outputs.release_title }} |
| 202 | + body_path: changelog.md |
| 203 | + env: |
| 204 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments