name: Auto Add Commits to Release Notes on: release: types: [published] jobs: build-release-notes: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v4 with: fetch-depth: 0 - name: Get previous release tag id: prev_tag run: | # 强制获取所有标签(防止标签未被拉取) git fetch --tags # 尝试获取上一个标签 PREV_TAG=$(git describe --tags --abbrev=0 HEAD^ 2>/dev/null) # 严格验证是否为有效标签(非空且存在于标签列表中) if [ -n "$PREV_TAG" ] && git tag --list | grep -q "^$PREV_TAG$"; then : # 有效标签,不做处理 else PREV_TAG="initial-commit" fi # 彻底清理输出值(移除所有特殊字符) echo "prev_tag=$(echo "$PREV_TAG" | tr -d '\n')" >> "$GITHUB_OUTPUT" - name: Generate commit log id: commit_log run: | if [ "${{ steps.prev_tag.outputs.prev_tag }}" = "initial-commit" ]; then COMMIT_LOG=$(git log --pretty=format:"- [%h] %s" --reverse) else COMMIT_LOG=$(git log ${{ steps.prev_tag.outputs.prev_tag }}..HEAD --pretty=format:"- [%h] %s" --reverse) fi COMMIT_LOG="${COMMIT_LOG//$'\n'/\\n}" echo "commit_log=$COMMIT_LOG" >> "$GITHUB_OUTPUT" - name: Update release notes uses: actions/github-script@v7 with: github-token: ${{ secrets.GITHUB_TOKEN }} script: | const { data: release } = await github.rest.repos.getRelease({ owner: context.repo.owner, repo: context.repo.repo, release_id: context.payload.release.id }); const newBody = `${release.body || ''}\n\n## 本次更新 Commit 记录\n${{ steps.commit_log.outputs.commit_log }}`; await github.rest.repos.updateRelease({ owner: context.repo.owner, repo: context.repo.repo, release_id: context.payload.release.id, body: newBody });