Skip to content

Commit

Permalink
ci: 优化 CHANGELOG.md 更新流程
Browse files Browse the repository at this point in the history
- 使用 actions/checkout@v3 替代 v2
- 简化新条目生成逻辑,使用单行格式
- 添加检查 CHANGELOG.md 中是否存在相同日期条目的功能
- 根据情况选择追加或创建新的条目
- 移除显示 CHANGELOG.md 的步骤
  • Loading branch information
molanp committed Nov 1, 2024
1 parent 2f938b7 commit a941f76
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions .github/workflows/update-changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ on:
permissions:
contents: write # 允许写入内容


jobs:
generate-changelog:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Set up Git
run: |
Expand All @@ -24,27 +23,20 @@ jobs:
- name: Append to CHANGELOG.md
run: |
CURRENT_DATE=$(date +%Y-%m-%d)
NEW_ENTRIES="## [$CURRENT_DATE] - New Updates\n"
# 获取上次推送的提交哈希
LAST_COMMIT=$(git rev-parse $GITHUB_SHA^)
NEW_ENTRIES="- $(git log -1 --pretty=format:"%s (hash: %h)")
# 获取自上次推送以来的所有新提交的日志
COMMIT_LOG=$(git log --pretty=format:"- %s (hash: %h)" "$LAST_COMMIT"..HEAD)
if [ -z "$COMMIT_LOG" ]; then
echo "No new commits to add to CHANGELOG."
# 检查 CHANGELOG.md 中是否存在相同日期的条目
if grep -q "## \[$CURRENT_DATE\] - New Updates" CHANGELOG.md; then
# 存在相同日期的条目,追加新的提交信息
sed -i "/## \[$CURRENT_DATE\] - New Updates/a \n$NEW_ENTRIES" CHANGELOG.md
else
NEW_ENTRIES+="$COMMIT_LOG\n"
# 不存在相同日期的条目,创建新的条目
HEAD_CONTENT=$(head -n 1 CHANGELOG.md)
TAIL_CONTENT=$(tail -n +2 CHANGELOG.md)
NEW_SECTION="## [$CURRENT_DATE] - New Updates\n$NEW_ENTRIES\n"
echo -e "$HEAD_CONTENT\n$NEW_SECTION$TAIL_CONTENT" > CHANGELOG.md
fi
# 读取现有内容
HEAD_CONTENT=$(head -n 1 CHANGELOG.md)
TAIL_CONTENT=$(tail -n +2 CHANGELOG.md)
# 创建新的 CHANGELOG 内容
echo -e "$HEAD_CONTENT\n$NEW_ENTRIES$TAIL_CONTENT" > CHANGELOG.md
- name: Display CHANGELOG.md
run: |
cat CHANGELOG.md
Expand All @@ -55,4 +47,4 @@ jobs:
git commit -m "Update CHANGELOG.md with new entries" || echo "No changes to commit"
git push
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 comments on commit a941f76

Please sign in to comment.