feat: 适配新版Karin #86
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
# 设置 Node.js 环境 | |
- name: 设置 Node.js 环境 | |
uses: actions/setup-node@v4 | |
with: | |
# 设置 Node.js 版本 | |
node-version: 20 | |
# 设置 npm 源 | |
registry-url: "https://registry.npmjs.org" | |
# 安装依赖 不安装对等依赖 | |
- name: 安装依赖 | |
run: npm install --config.auto-install-peers=false --ignore-scripts --omit=peer | |
# 构建输出 | |
- name: 构建输出 | |
id: build | |
run: npm run build > 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: npm run pr all | |
if: success() | |
# 发布到 npm | |
- name: 发布到 npm | |
run: npm 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 install ${{ env.PKG_NAME }}@${{ env.PKG_VERSION }} -w" | |
gh pr comment ${{ env.PR_NUMBER }} --body $'🎉 构建完成!您可以通过以下命令安装此版本:\n\n```\n'"${INSTALL_COMMAND_1}"$'\n```\n' | |
fi | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
# 撤回历史评论 保留最新的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.GITHUB_TOKEN }}" \ | |
-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.GITHUB_TOKEN }} |