Skip to content

🔖 发布版本 0.1.2 #2

🔖 发布版本 0.1.2

🔖 发布版本 0.1.2 #2

Workflow file for this run

name: Build and Release
on:
push:
tags: ['v*']
workflow_dispatch:
jobs:
build:
permissions:
contents: write
strategy:
fail-fast: false
matrix:
include:
- platform: macos-latest
target: aarch64-apple-darwin
name: macos-aarch64
- platform: macos-latest
target: x86_64-apple-darwin
name: macos-x86_64
- platform: ubuntu-22.04
target: x86_64-unknown-linux-gnu
name: linux-x86_64
- platform: windows-latest
target: x86_64-pc-windows-msvc
name: windows-x86_64
runs-on: ${{ matrix.platform }}
steps:
- uses: actions/checkout@v4
- name: Install Rust stable
uses: dtolnay/rust-toolchain@stable
with:
targets: ${{ matrix.target }}
- name: Rust cache
uses: swatinem/rust-cache@v2
with:
key: ${{ matrix.target }}
- name: Build release binary
run: cargo build --release --target ${{ matrix.target }}
- name: Create release package
shell: bash
run: |
mkdir -p release-package
# 获取完整的 tag 名称
TAG_NAME="${{ github.ref_name }}"
# 确定二进制文件名和扩展名
if [[ "${{ matrix.platform }}" == "windows-latest" ]]; then
BINARY_NAME="code-nexus.exe"
ARCHIVE_EXT="zip"
else
BINARY_NAME="code-nexus"
ARCHIVE_EXT="tar.gz"
fi
# 复制二进制文件
cp "target/${{ matrix.target }}/release/$BINARY_NAME" release-package/
# 复制其他文件
cp README.md release-package/ || true
cp LICENSE release-package/ || true
# 创建压缩包
cd release-package
if [[ "${{ matrix.platform }}" == "windows-latest" ]]; then
7z a "../code-nexus-${TAG_NAME}-${{ matrix.name }}.zip" *
else
tar -czf "../code-nexus-${TAG_NAME}-${{ matrix.name }}.tar.gz" *
fi
- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: code-nexus-${{ matrix.name }}
path: |
code-nexus-*.tar.gz
code-nexus-*.zip
if-no-files-found: ignore
release:
name: Create Release
needs: build
runs-on: ubuntu-latest
if: startsWith(github.ref, 'refs/tags/v')
permissions:
contents: write
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Verify version consistency
run: |
# 获取 git tag 版本号(去掉 v 前缀)
TAG_NAME="${{ github.ref_name }}"
TAG_VERSION_NUMBER=${TAG_NAME#v}
# 从 Cargo.toml 读取版本号
PROJECT_VERSION=$(grep '^version = ' Cargo.toml | head -1 | cut -d'"' -f2)
echo "Git tag version: ${TAG_VERSION_NUMBER}"
echo "Cargo.toml version: ${PROJECT_VERSION}"
# 检查版本是否一致
if [ "${TAG_VERSION_NUMBER}" != "${PROJECT_VERSION}" ]; then
echo "❌ Version mismatch detected!"
echo "Git tag version: ${TAG_VERSION_NUMBER}"
echo "Cargo.toml version: ${PROJECT_VERSION}"
echo ""
echo "Please ensure the git tag matches the version in Cargo.toml."
echo "You can either:"
echo "1. Update Cargo.toml to match tag: ${TAG_VERSION_NUMBER}"
echo "2. Create a new tag that matches Cargo.toml version: v${PROJECT_VERSION}"
exit 1
fi
echo "✅ Version consistency check passed: ${TAG_VERSION_NUMBER}"
- name: Download all artifacts
uses: actions/download-artifact@v4
with:
path: artifacts
- name: Install git-cliff
uses: taiki-e/install-action@git-cliff
- name: Generate changelog and release title
id: changelog
run: |
# 从 GitHub API 获取最新的 release 版本
echo "Fetching latest release from GitHub API..."
LATEST_RELEASE=$(curl -s "https://api.github.com/repos/${{ github.repository }}/releases/latest" | jq -r '.tag_name // empty' 2>/dev/null || echo "")
if [ -z "$LATEST_RELEASE" ] || [ "$LATEST_RELEASE" = "null" ]; then
echo "GitHub API failed or no previous release found, falling back to git tags"
# 回退到使用 git tag 获取上一个版本
PREVIOUS_TAG=$(git tag --sort=-version:refname | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | head -2 | tail -1)
if [ -z "$PREVIOUS_TAG" ]; then
echo "No previous tags found, generating full changelog"
else
echo "Found previous tag: $PREVIOUS_TAG"
fi
else
PREVIOUS_TAG="$LATEST_RELEASE"
echo "Found previous release: $PREVIOUS_TAG"
fi
echo "Current tag: ${{ github.ref_name }}"
echo "Previous version: $PREVIOUS_TAG"
if [ -z "$PREVIOUS_TAG" ]; then
# 如果没有上一个版本,生成所有提交的 changelog
git-cliff --tag ${{ github.ref_name }} --output changelog.md
else
# 只生成从上一个版本到当前版本的 changelog
git-cliff $PREVIOUS_TAG..${{ github.ref_name }} --output changelog.md
# 添加 Full Changelog 链接
echo "" >> changelog.md
echo "**Full Changelog**: [$PREVIOUS_TAG...${{ github.ref_name }}](https://github.com/${{ github.repository }}/compare/$PREVIOUS_TAG...${{ github.ref_name }})" >> changelog.md
fi
# 生成发布标题
VERSION_NUMBER="${{ github.ref_name }}"
VERSION_NUMBER=${VERSION_NUMBER#v} # 移除 v 前缀
# 从最近的提交中提取主要功能作为标题
if [ -z "$PREVIOUS_TAG" ]; then
COMMIT_RANGE="${{ github.ref_name }}"
else
COMMIT_RANGE="$PREVIOUS_TAG..${{ github.ref_name }}"
fi
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: /🐞 /')
if [ -z "$MAIN_FEATURE" ]; then
RELEASE_TITLE="$VERSION_NUMBER 📦 版本更新"
else
RELEASE_TITLE="$VERSION_NUMBER $MAIN_FEATURE"
fi
echo "release_title=$RELEASE_TITLE" >> $GITHUB_OUTPUT
echo "Generated release title: $RELEASE_TITLE"
- name: Create Release
uses: softprops/action-gh-release@v1
with:
files: |
artifacts/*/code-nexus-*.tar.gz
artifacts/*/code-nexus-*.zip
draft: false
prerelease: false
generate_release_notes: false
name: ${{ steps.changelog.outputs.release_title }}
body_path: changelog.md
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}