Skip to content

CI: Deploy Preview

CI: Deploy Preview #49

name: 'CI: Deploy Preview'
on:
workflow_run:
workflows: ["ci"]
types:
- completed
permissions:
pull-requests: write
actions: read
contents: read
jobs:
deploy:
# 只在 PR 触发且 CI 成功时运行
if: >
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success'
runs-on: ubuntu-latest
steps:
- name: Download build artifacts
uses: actions/download-artifact@v4
with:
name: build-${{ github.event.workflow_run.head_sha }}
path: artifacts
github-token: ${{ secrets.GITHUB_TOKEN }}
run-id: ${{ github.event.workflow_run.id }}
- name: Read PR Number
id: pr
run: |
if [ -f artifacts/pr_number.txt ]; then
echo "number=$(cat artifacts/pr_number.txt)" >> $GITHUB_OUTPUT
else
echo "PR number file not found"
exit 1
fi
- name: Download pkg.pr.new output
uses: actions/download-artifact@v4
with:
name: pkg-pr-new-output-${{ github.event.workflow_run.head_sha }}
path: pkg-artifacts
github-token: ${{ secrets.GITHUB_TOKEN }}
run-id: ${{ github.event.workflow_run.id }}
continue-on-error: true
- name: Comment build generating
uses: actions-cool/[email protected]
with:
token: ${{ secrets.GITHUB_TOKEN }}
body: |
[<img width="400" src="https://github.com/user-attachments/assets/c25bbbe4-0cf5-4aca-b4b3-db49e7a264d9">](#)
🔨 Deploying preview...
<!-- TINY_ROBOT_BUILD -->
body-include: '<!-- TINY_ROBOT_BUILD -->'
number: ${{ steps.pr.outputs.number }}
- name: Post or update pkg.pr.new comment
if: hashFiles('pkg-artifacts/pkg-pr-new-output.json') != ''
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
const output = JSON.parse(fs.readFileSync('pkg-artifacts/pkg-pr-new-output.json', 'utf8'));
const sha = '${{ github.event.workflow_run.head_sha }}'.substring(0, 7);
const packages = output.packages.map((p) => {
const shortUrl = p.url.replace(/@[a-f0-9]{40}/, '@' + sha);
return 'pnpm add ' + shortUrl;
}).join('\n\n');
const commitUrl = 'https://github.com/${{ github.repository }}/commit/' + sha;
const body = '## 📦 Package Preview\n\n' + packages + '\n\n**commit:** [' + sha + '](' + commitUrl + ')\n\n<!-- PKG_PR_NEW_COMMENT -->';
const botCommentIdentifier = '<!-- PKG_PR_NEW_COMMENT -->';
const comments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: ${{ steps.pr.outputs.number }},
});
const existingComment = comments.data.find((comment) =>
comment.body.includes(botCommentIdentifier)
);
if (existingComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingComment.id,
body: body,
});
} else {
await github.rest.issues.createComment({
issue_number: ${{ steps.pr.outputs.number }},
owner: context.repo.owner,
repo: context.repo.repo,
body: body,
});
}
- name: Deploy Site to Surge
id: deploy
run: |
DEPLOY_DOMAIN=preview-${{ steps.pr.outputs.number }}-tiny-robot.surge.sh
echo "Deploying to: https://$DEPLOY_DOMAIN"
npx surge --project ./artifacts/docs/dist --domain $DEPLOY_DOMAIN --token $SURGE_TOKEN
echo "url=https://$DEPLOY_DOMAIN" >> $GITHUB_OUTPUT
env:
SURGE_TOKEN: ${{ secrets.SURGE_TOKEN }}
- name: Comment build success
if: success()
uses: actions-cool/[email protected]
with:
token: ${{ secrets.GITHUB_TOKEN }}
body: |
[<img width="400" src="https://github.com/user-attachments/assets/2a0bb639-dfaf-4384-8d73-cec616ea4b97">](https://preview-${{ steps.pr.outputs.number }}-tiny-robot.surge.sh)
✅ Preview build completed successfully!
> Click the image above to preview.
> Preview will be automatically removed when this PR is closed.
<!-- TINY_ROBOT_BUILD -->
body-include: '<!-- TINY_ROBOT_BUILD -->'
number: ${{ steps.pr.outputs.number }}
- name: Comment build failed
if: failure()
uses: actions-cool/[email protected]
with:
token: ${{ secrets.GITHUB_TOKEN }}
body: |
[<img width="400" src="https://github.com/user-attachments/assets/acfe799d-40be-4ffb-8190-da33f84056dc">](#)
❌ Deploy failed. Please check the logs for details.
<!-- TINY_ROBOT_BUILD -->
body-include: '<!-- TINY_ROBOT_BUILD -->'
number: ${{ steps.pr.outputs.number }}