Skip to content

Commit dce2aeb

Browse files
committed
ci: 🎡 新增预览部署
1 parent 749cf26 commit dce2aeb

1 file changed

Lines changed: 55 additions & 3 deletions

File tree

.github/workflows/preview.yml

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,65 @@ jobs:
4747
if: steps.build.outcome == 'success'
4848
run: npm install -g netlify-cli
4949

50-
- name: Deploy to Netlify
50+
- name: Deploy to Netlify
5151
if: steps.build.outcome == 'success'
52+
id: netlify-deploy
5253
env:
5354
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
5455
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
5556
run: |
56-
netlify deploy \
57+
# 部署到Netlify并获取预览URL
58+
DEPLOY_OUTPUT=$(netlify deploy \
5759
--dir=.next \
5860
--message='Deploy Preview for PR #${{ github.event.number }}' \
59-
--alias=pr-${{ github.event.number }}
61+
--alias=pr-${{ github.event.number }} \
62+
--json)
63+
64+
DEPLOY_URL=$(echo "$DEPLOY_OUTPUT" | jq -r '.deploy_url')
65+
echo "Preview URL: $DEPLOY_URL"
66+
echo "deploy_url=$DEPLOY_URL" >> $GITHUB_OUTPUT
67+
68+
- name: Update Preview Comment
69+
if: steps.build.outcome == 'success' && steps.netlify-deploy.outcome == 'success'
70+
uses: actions/github-script@v6
71+
with:
72+
script: |
73+
const deployUrl = '${{ steps.netlify-deploy.outputs.deploy_url }}';
74+
const prNumber = context.issue.number;
75+
76+
// 查找现有的预览评论
77+
const comments = await github.rest.issues.listComments({
78+
owner: context.repo.owner,
79+
repo: context.repo.repo,
80+
issue_number: prNumber
81+
});
82+
83+
const botComment = comments.data.find(comment =>
84+
comment.user.type === 'Bot' &&
85+
(comment.body.includes('Deploy Preview') || comment.body.includes('Preview Deployment'))
86+
);
87+
88+
const successComment = `✅ **Deploy Preview for PR #${prNumber} - SUCCESS!**
89+
90+
🚀 **Preview URL:** ${deployUrl}
91+
📅 **Updated:** ${new Date().toISOString()}
92+
93+
_This preview will be updated automatically when you push new commits._`;
94+
95+
if (botComment) {
96+
// 更新现有评论
97+
await github.rest.issues.updateComment({
98+
owner: context.repo.owner,
99+
repo: context.repo.repo,
100+
comment_id: botComment.id,
101+
body: successComment
102+
});
103+
} else {
104+
// 创建新评论
105+
await github.rest.issues.createComment({
106+
owner: context.repo.owner,
107+
repo: context.repo.repo,
108+
issue_number: prNumber,
109+
body: successComment
110+
});
111+
}

0 commit comments

Comments
 (0)