ci(workflow): 优化自动更新发布日志的 GitHub Actions 工作流

- 重命名工作流名称以更简洁
- 添加 workflow_dispatch 触发器以支持手动触发
- 简化获取上一个标签的逻辑
- 使用 action-gh-release 替代 github-script 来更新发布日志
- 改进提交记录格式,包含作者信息
This commit is contained in:
2025-09-06 18:19:59 +08:00
parent 434b21c32a
commit dd00d1f47c

View File

@@ -1,6 +1,7 @@
name: Auto Add Commits to Release Notes name: Release
on: on:
workflow_dispatch:
release: release:
types: [published] types: [published]
@@ -10,36 +11,28 @@ jobs:
steps: steps:
- name: Checkout code - name: Checkout code
uses: actions/checkout@v4 uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get previous release tag or first commit - name: Get previous tag
id: prev_tag id: prev_tag
run: | run: |
PREV_TAG=$(git describe --tags --abbrev=0 HEAD^ 2>/dev/null || git rev-list --max-parents=0 HEAD) PREV_TAG=$(git describe --tags --abbrev=0 $(git rev-list --tags --skip=1 --max-count=1))
printf "prev_tag=%s\n" "$PREV_TAG" >> $GITHUB_OUTPUT echo "PREV_TAG=$PREV_TAG" >> $GITHUB_ENV
- name: Generate commit log - name: Get commits between tags
id: commit_log id: changelog
run: | run: |
COMMIT_LOG=$(git log ${{ steps.prev_tag.outputs.prev_tag }}..HEAD --pretty=format:"- [%h] %s" --reverse) git fetch --tags
COMMIT_LOG="${COMMIT_LOG//$'\n'/\\n}" COMMITS=$(git log $PREV_TAG..${{ github.event.release.tag_name }} --pretty=format:"- %s (%an)")
echo "commit_log=$COMMIT_LOG" >> $GITHUB_OUTPUT echo "$COMMITS" > commits.txt
echo "COMMITS<<EOF" >> $GITHUB_ENV
cat commits.txt >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
- name: Update release notes - name: Update Release Notes
uses: actions/github-script@v7 uses: softprops/action-gh-release@v2
with: with:
github-token: ${{ secrets.GITHUB_TOKEN }} tag_name: ${{ github.event.release.tag_name }}
script: | body: |
const { data: release } = await github.rest.repos.getRelease({ ${{ env.COMMITS }}
owner: context.repo.owner, env:
repo: context.repo.repo, GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
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
});