fix(ci): 修复预览环境workflow YAML解析错误,评论逻辑抽为独立脚本
CI Build & Deploy Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI Build & Deploy Pipeline / Build Staging API Image (pull_request) Has been skipped
CI Build & Deploy Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI Build & Deploy Pipeline / Build Production Web Image (pull_request) Has been skipped
CI Build & Deploy Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI Build & Deploy Pipeline / Build Production API Image (pull_request) Has been skipped
CI Build & Deploy Pipeline / Build Production Worker Image (pull_request) Has been skipped
CI Build & Deploy Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI Build & Deploy Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI Build & Deploy Pipeline / Deploy Production (pull_request) Has been skipped
CI Build & Deploy Pipeline / Production Browser E2E (pull_request) Has been skipped
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 24s
Preview Deploy / Deploy Preview Environment (pull_request) Failing after 48s
CI/CD Pipeline / Frontend Lint (pull_request) Successful in 54s
CI/CD Pipeline / Validate Code Quality And Tests (pull_request) Failing after 43s
Auto Merge CI PRs / Auto Merge on CI Green + Approved (pull_request) Successful in 1m11s
AI Code Review / AI Code Review (pull_request) Successful in 2m7s
CI/CD Pipeline / Integration Tests (pull_request) Successful in 1m29s
CI/CD Pipeline / Unit Tests (pull_request) Has been cancelled
Auto Approve CI PRs / Auto Approve on CI Green (pull_request) Successful in 2m59s

- 修复 preview-deploy.yml 和 preview-cleanup.yml 中Python三重引号
  嵌Markdown表格导致YAML解析失败的问题
- 评论生成逻辑抽离为 scripts/ci/preview_comment.py
- workflow中直接调用脚本,避免多层嵌套字符串
This commit is contained in:
CI Bot
2026-07-18 12:01:59 +08:00
parent 854522986e
commit c4f2a2f817
3 changed files with 63 additions and 46 deletions
+3 -11
View File
@@ -95,18 +95,9 @@ jobs:
run: |
set -eu
PR_NUMBER=$(echo "$GITHUB_REF" | sed 's|refs/pull/||; s|/.*||')
export PR_NUMBER
COMMENT_BODY=$(python3 -c "
import json, os
pr_num = os.environ['PR_NUMBER']
body = f'''🗑️ **预览环境已清理**
PR #{pr_num} 已关闭或合并,对应的预览环境已被清理。
> 如有需要,可以重新打开 PR 来重新生成预览环境。
'''
print(json.dumps({'body': body}))
")
COMMENT_BODY=$(python3 scripts/ci/preview_comment.py cleanup)
API_URL="${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments"
curl -s -X POST \
@@ -116,3 +107,4 @@ PR #{pr_num} 已关闭或合并,对应的预览环境已被清理。
"$API_URL" \
> /dev/null
echo "Cleanup comment posted"
+7 -35
View File
@@ -202,50 +202,24 @@ jobs:
set -eu
PR_NUMBER=$(echo "$GITHUB_REF" | sed 's|refs/pull/||; s|/.*||')
PREVIEW_URL="https://pr-${PR_NUMBER}.preview.xiaoxiajianji.com"
export PR_NUMBER PREVIEW_URL
# 构建评论内容
COMMENT_BODY=$(python3 -c "
import json, os
pr_num = os.environ['PR_NUMBER']
url = os.environ['PREVIEW_URL']
body = f'''🚀 **预览环境已部署**
COMMENT_BODY=$(python3 scripts/ci/preview_comment.py deploy)
| 项目 | 详情 |
|------|------|
| PR号 | #{pr_num} |
| 预览链接 | [${url}](${url}) |
| API环境 | staging |
> 💡 预览环境使用 staging API 数据,请勿在预览环境中操作重要数据。
>
> 🔄 每次提交新代码后预览环境会自动更新。
>
> 🗑️ PR 关闭或合并后,预览环境会自动清理。
'''
print(json.dumps({'body': body}))
")
# 查找是否已有预览评论
API_URL="${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments"
EXISTING_COMMENT_ID=""
COMMENTS=$(curl -s -H "Authorization: token ${GITHUB_TOKEN}" "$API_URL")
EXISTING_COMMENT_ID=$(echo "$COMMENTS" | python3 -c "
EXISTING_COMMENT_ID=$(curl -s -H "Authorization: token ${GITHUB_TOKEN}" "$API_URL" | python3 -c "
import sys, json
try:
comments = json.load(sys.stdin)
for c in comments:
body = c.get('body', '')
if '预览环境已部署' in body and 'Preview URL' not in body:
for c in json.load(sys.stdin):
if '预览环境已部署' in c.get('body', ''):
print(c['id'])
break
except:
print('')
except Exception:
pass
")
if [ -n "$EXISTING_COMMENT_ID" ]; then
# 更新已有评论
echo "Updating existing comment: $EXISTING_COMMENT_ID"
curl -s -X PATCH \
-H "Authorization: token ${GITHUB_TOKEN}" \
-H "Content-Type: application/json" \
@@ -254,8 +228,6 @@ jobs:
> /dev/null
echo "Comment updated"
else
# 发布新评论
echo "Creating new comment"
curl -s -X POST \
-H "Authorization: token ${GITHUB_TOKEN}" \
-H "Content-Type: application/json" \
+53
View File
@@ -0,0 +1,53 @@
#!/usr/bin/env python3
"""生成预览环境PR评论内容"""
import json
import os
import sys
def generate_deploy_comment(pr_number, preview_url):
"""生成部署成功的评论内容"""
return f"""🚀 **预览环境已部署**
| 项目 | 详情 |
|------|------|
| PR号 | #{pr_number} |
| 预览链接 | [{preview_url}]({preview_url}) |
| API环境 | staging |
> 💡 预览环境使用 staging API 数据,请勿在预览环境中操作重要数据。
>
> 🔄 每次提交新代码后预览环境会自动更新。
>
> 🗑️ PR 关闭或合并后,预览环境会自动清理。
"""
def generate_cleanup_comment(pr_number):
"""生成清理完成的评论内容"""
return f"""🗑️ **预览环境已清理**
PR #{pr_number} 已关闭或合并,对应的预览环境已被清理。
> 如有需要,可以重新打开 PR 来重新生成预览环境。
"""
def main():
mode = sys.argv[1] if len(sys.argv) > 1 else "deploy"
pr_number = os.environ.get("PR_NUMBER", "")
preview_url = os.environ.get("PREVIEW_URL", "")
if mode == "deploy":
body = generate_deploy_comment(pr_number, preview_url)
elif mode == "cleanup":
body = generate_cleanup_comment(pr_number)
else:
print(f"Unknown mode: {mode}", file=sys.stderr)
sys.exit(1)
print(json.dumps({"body": body}))
if __name__ == "__main__":
main()