-
Notifications
You must be signed in to change notification settings - Fork 13
109 lines (104 loc) · 4.4 KB
/
pr.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
name: PR
# 监听 PR 相关事件
on:
pull_request:
# 监听 PR 被打开、重新打开和推送事件
types: [opened, reopened, synchronize]
# 赋予 release-please-action 权限
permissions:
contents: write
pull-requests: write
jobs:
# 设置 release-please 任务
release-please:
# 设置任务运行环境为 ubuntu-latest
runs-on: ubuntu-latest
steps:
- name: 检出代码
uses: actions/checkout@v4
# 设置 pnpm
- name: 设置 pnpm
uses: pnpm/action-setup@v2
with:
version: 8
# 设置 Node.js 环境
- name: 设置 Node.js 环境
uses: actions/setup-node@v4
with:
# 设置 Node.js 版本
node-version: 20
# 设置 npm 源
registry-url: "https://registry.npmjs.org"
# 添加 pnpm 缓存支持
cache: pnpm
# 使用 pnpm 安装依赖
- name: 安装依赖
run: pnpm install --no-frozen-lockfile
# 构建输出
- name: 构建输出
id: build
run: pnpm run build:prod > build.log 2>&1
continue-on-error: true # 捕获失败并允许后续步骤运行
# 获取当前 PR 编号并设置环境变量
- name: 获取 PR 编号
run: echo "PR_NUMBER=${{ github.event.pull_request.number }}" >> $GITHUB_ENV
# 自动修改 package.json 的版本号、删除开发、对等依赖
- name: 修订版本号
run: pnpm run pr all
if: success()
# 发布到 npm
- name: 发布到 npm
run: pnpm run pub-beta
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
if: success()
# 在 PR 上发布构建完成或失败信息
- name: 发布评论
run: |
if [ "${{ steps.build.outcome }}" = "failure" ]; then
# 捕获构建失败日志
ERROR_MESSAGE=$(cat build.log || echo "未找到详细的构建日志。")
# 发布构建失败评论
gh pr comment ${{ env.PR_NUMBER }} --body $'❌ 构建失败!以下是错误日志:\n\n<details>\n<summary>展开查看错误日志</summary>\n\n```\n'"${ERROR_MESSAGE}"$'\n```\n\n</details>'
else
# 构建成功评论
INSTALL_COMMAND_1="pnpm rm ${{ env.PKG_NAME }} && pnpm install ${{ env.PKG_NAME }}@${{ env.PKG_VERSION }} -D"
INSTALL_COMMAND_2="pnpm install ${{ env.PKG_NAME }}@${{ env.PKG_VERSION }} -w"
gh pr comment ${{ env.PR_NUMBER }} --body $'🎉 构建完成!请不要使用此版本,正在重构中:\n\n```\n'"${INSTALL_COMMAND_1}"$'\n```\n\n```\n'"${INSTALL_COMMAND_2}"$'\n```'
fi
env:
GITHUB_TOKEN: ${{ secrets.RELEASE }}
# 撤回历史评论 保留最新的3条评论
- name: 撤回历史评论
run: |
# 获取当前PR的所有评论
comments=$(gh pr view ${{ env.PR_NUMBER }} --json comments)
# 过滤目标评论并按时间排序
target_comments=$(echo "$comments" | jq -r '.comments[] | select(.body | test("🎉 构建完成!|❌ 构建失败!")) | {url: .url, created_at: .createdAt}' | jq -s '. | sort_by(.created_at) | reverse')
# 获取需要删除的评论ID (保留最新的3条)
comment_ids_to_delete=$(echo "$target_comments" | jq -r 'if length > 3 then .[3:][] | .url | capture("#issuecomment-(?<id>\\d+)$").id else empty end')
# 删除旧评论
if [ ! -z "$comment_ids_to_delete" ]; then
for id in $comment_ids_to_delete; do
# 调试信息
echo "删除请求URL: /repos/${{ github.repository }}/issues/comments/$id"
response=$(curl -L \
-X DELETE \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.RELEASE }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-w "%{http_code}" \
-o /dev/null \
"https://api.github.com/repos/${{ github.repository }}/issues/comments/$id")
if [ "$response" = "204" ]; then
echo "✓ 成功删除评论 $id"
else
echo "❌ 删除评论 $id 失败,HTTP状态码: $response"
exit 1
fi
done
else
echo "没有需要删除的历史评论"
fi
env:
GITHUB_TOKEN: ${{ secrets.RELEASE }}