更新 张瑞国 #42
Workflow file for this run
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: Preview PR Build | |
on: | |
pull_request_target: | |
types: [opened, synchronize, reopened, ready_for_review] | |
paths: | |
- 'content/**' | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: 检出主仓库代码 | |
uses: actions/checkout@v4 | |
with: | |
ref: ${{ github.event.pull_request.base.ref }} | |
fetch-depth: 0 | |
submodules: true | |
- name: 检出 PR 修改的部分 | |
run: | | |
git fetch origin +refs/pull/${{ github.event.pull_request.number }}/merge | |
git checkout -qf FETCH_HEAD | |
# - name: 配置 baseURL | |
# run: | | |
# sed -i 's|baseURL = "https://mtf.wiki/"|baseURL = "/"|' config/_default/config.toml | |
- name: Install Node.js | |
uses: actions/setup-node@v4 | |
with: | |
node-version: '22' | |
cache: 'npm' | |
- name: Install npm dependencies | |
run: npm install | |
- name: Set up Hugo | |
uses: peaceiris/actions-hugo@v2 | |
with: | |
hugo-version: latest | |
extended: true | |
- name: Build site | |
run: hugo --minify --enableGitInfo --baseURL "/" | |
- name: Deploy to Cloudflare Pages | |
uses: cloudflare/pages-action@v1 | |
id: deploy | |
with: | |
apiToken: ${{ secrets.CLOUDFLARE_PAGES_TOKEN }} | |
accountId: ${{ secrets.CLOUDFLARE_PAGES_ACCOUNT }} | |
projectName: mtfwiki-preview | |
directory: public | |
- name: 评论 PR 部署链接 | |
uses: actions/github-script@v6 | |
with: | |
script: | | |
const prNumber = context.payload.pull_request.number; | |
const previewUrl = `${{ steps.deploy.outputs.url }}`; | |
const commentBody = `🚀 预览部署完成! 访问链接: ${previewUrl}`; | |
// 获取现有评论 | |
const { data: comments } = await github.rest.issues.listComments({ | |
issue_number: prNumber, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
}); | |
// 查找评论的关键词 | |
const existingComment = comments.find(comment => | |
comment.body.includes('🚀 预览部署完成!')); | |
if (existingComment) { | |
// 如果已经有评论,更新评论 | |
await github.rest.issues.updateComment({ | |
comment_id: existingComment.id, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: commentBody, | |
}); | |
} else { | |
// 如果没有评论,创建新的评论 | |
await github.rest.issues.createComment({ | |
issue_number: prNumber, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: commentBody, | |
}); | |
} |