name: Auto Release on Version Update on: workflow_dispatch: push: branches: - main paths: - 'metadata.yaml' jobs: check-version: runs-on: ubuntu-latest outputs: version_changed: ${{ steps.check.outputs.changed }} new_version: ${{ steps.check.outputs.version }} old_version: ${{ steps.check.outputs.old_version }} steps: - name: Checkout code uses: actions/checkout@v4 with: fetch-depth: 2 - name: Check version change id: check run: | # 获取当前版本 CURRENT_VERSION=$(grep '^version:' metadata.yaml | awk '{print $2}') echo "Current version: $CURRENT_VERSION" # 获取上一次提交的版本 git checkout HEAD~1 -- metadata.yaml 2>/dev/null || echo "First commit" if [ -f metadata.yaml ]; then PREVIOUS_VERSION=$(grep '^version:' metadata.yaml | awk '{print $2}') git checkout HEAD -- metadata.yaml else PREVIOUS_VERSION="none" fi echo "Previous version: $PREVIOUS_VERSION" # 比较版本 if [ "$CURRENT_VERSION" != "$PREVIOUS_VERSION" ]; then echo "changed=true" >> $GITHUB_OUTPUT echo "version=$CURRENT_VERSION" >> $GITHUB_OUTPUT echo "old_version=$PREVIOUS_VERSION" >> $GITHUB_OUTPUT echo "Version changed from $PREVIOUS_VERSION to $CURRENT_VERSION" else echo "changed=false" >> $GITHUB_OUTPUT echo "No version change detected" fi create-release: needs: check-version if: needs.check-version.outputs.version_changed == 'true' runs-on: ubuntu-latest permissions: contents: write steps: - name: Checkout code uses: actions/checkout@v4 with: fetch-depth: 0 - name: Get current commit hash id: get_commit run: | COMMIT_SHORT=$(echo "${{ github.sha }}" | cut -c1-7) echo "COMMIT_SHORT=$COMMIT_SHORT" >> $GITHUB_OUTPUT - name: Generate release notes id: release_notes run: | VERSION="${{ needs.check-version.outputs.new_version }}" OLD_VERSION="${{ needs.check-version.outputs.old_version }}" # 1. 尝试提取 CHANGELOG.md if [ -f "CHANGELOG.md" ]; then awk -v ver="$VERSION" ' /^## \[?v?/ && index($0, ver) { flag=1 } /^## \[?v?/ && !index($0, ver) { flag=0 } flag ' CHANGELOG.md > release_notes.md fi # 确保 release_notes.md 存在 touch release_notes.md # 2. 生成提交列表 echo "" >> release_notes.md echo "### 🔍 Commits" >> release_notes.md process_log() { local range="$1" git log --pretty=format:"%s|%h|%H" "$range" > commits.txt # Helper to extract categories print_category() { local marker="$1" local title="$2" local pattern="$3" local commits=$(grep -iE "$pattern" commits.txt) if [ ! -z "$commits" ]; then echo "#### $marker $title" >> release_notes.md echo "$commits" | while IFS='|' read -r msg hash full_hash; do echo "- $msg ([$hash](https://github.com/${{ github.repository }}/commit/$full_hash))" >> release_notes.md done echo "" >> release_notes.md fi } print_category "✨" "Features" "^(feat|feature)(\(.*\))?:" print_category "🐛" "Fixes" "^fix(\(.*\))?:" print_category "📚" "Documentation" "^docs(\(.*\))?:" print_category "💄" "Style & Refactor" "^(style|refactor|perf)(\(.*\))?:" print_category "🔧" "Chore & Test" "^(chore|test|ci|build)(\(.*\))?:" # Other changes (inverse grep) local others=$(grep -ivE "^(feat|feature|fix|docs|style|refactor|perf|chore|test|ci|build)(\(.*\))?:" commits.txt) if [ ! -z "$others" ]; then echo "#### 📌 Other Changes" >> release_notes.md echo "$others" | while IFS='|' read -r msg hash full_hash; do echo "- $msg ([$hash](https://github.com/${{ github.repository }}/commit/$full_hash))" >> release_notes.md done fi rm commits.txt } if [ "$OLD_VERSION" != "none" ] && git rev-parse "$OLD_VERSION" >/dev/null 2>&1; then process_log "$OLD_VERSION..HEAD" echo "" >> release_notes.md echo "**Full Changelog**: https://github.com/${{ github.repository }}/compare/$OLD_VERSION...${{ github.ref_name }}" >> release_notes.md else echo "Initial release or previous version tag not found." >> release_notes.md echo "#### 🆕 Initial Commits" >> release_notes.md git log -10 --pretty=format:"- %s ([%h](https://github.com/${{ github.repository }}/commit/%H))" >> release_notes.md fi cat release_notes.md - name: Create Release uses: softprops/action-gh-release@v1 with: tag_name: ${{ needs.check-version.outputs.new_version }} name: Release ${{ needs.check-version.outputs.new_version }} body_path: release_notes.md draft: false prerelease: false generate_release_notes: false env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: Notify release created run: | echo "✅ Release ${{ needs.check-version.outputs.new_version }} has been created successfully!" echo "🔗 View release at: https://github.com/${{ github.repository }}/releases/tag/${{ needs.check-version.outputs.new_version }}"