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: 部署 PR 到 Cloudflare Pages | |
on: | |
pull_request_target: | |
types: [opened, synchronize, reopened, ready_for_review] | |
paths: | |
- 'docs/**' | |
- 'package.json' | |
- 'packages/**' | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
outputs: | |
preview_url: ${{ steps.deploy.outputs.deployment-url }} | |
steps: | |
# 第一步:检出主仓库代码 | |
- name: 检出主仓库代码 | |
uses: actions/checkout@v4 | |
with: | |
ref: ${{ github.event.pull_request.base.ref }} | |
fetch-depth: 0 | |
# 第二步:检出 PR 修改的部分 | |
- name: 检出 PR 修改的部分 | |
run: | | |
git fetch origin +refs/pull/${{ github.event.pull_request.number }}/merge | |
git checkout -qf FETCH_HEAD | |
# 第三步:安装 pnpm | |
- name: 安装 pnpm | |
uses: pnpm/action-setup@v4 | |
# 第四步:安装依赖 | |
- name: 安装依赖 | |
run: pnpm install | |
- name: 更新主题包 | |
run: pnpm update @project-trans/vitepress-theme-project-trans@prerelease | |
# 第五步:构建项目 | |
- name: 构建项目 | |
run: pnpm build # 构建 VitePress 项目 | |
# 第六步:安装 Wrangler | |
- name: 安装 Wrangler | |
run: pnpm add -g wrangler@3 # 安装 Wrangler v3 | |
- name: Deploy to Cloudflare Pages | |
id: deploy | |
uses: cloudflare/wrangler-action@v3 | |
with: | |
apiToken: ${{ secrets.CLOUDFLARE_PAGES_TOKEN }} | |
accountId: ${{ secrets.CLOUDFLARE_PAGES_ACCOUNT }} | |
command: pages deploy docs/.vitepress/dist --project-name=blog | |
- name: List PR files using GitHub CLI | |
run: | | |
# 使用 GitHub CLI 获取 PR 文件列表并提取文件路径 | |
files=$(gh api \ | |
-H "Accept: application/vnd.github+json" \ | |
/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/files \ | |
| jq -r '.[].filename') # 使用 -r 获取原始文本输出 | |
# 处理文件路径,拼接成完整的 URL | |
for file in $files; do | |
# 去掉路径前的 "docs/" 和后缀 ".md" | |
modified_file="${file#docs/}" # 删除路径前的 "docs/" | |
modified_file="${modified_file%.md}" # 删除路径后面的 ".md" | |
# 拼接 URL | |
file_url="${{ steps.deploy.outputs.deployment-url }}${modified_file}" | |
# 输出每个文件的完整 URL | |
echo "File URL: ${file_url}" | |
done | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
# comment_on_pr: | |
# needs: deploy | |
# uses: project-trans/actions/.github/workflows/comment-pr-preview-link.yml@main | |
# secrets: inherit | |
# with: | |
# previewUrl: ${{ needs.deploy.outputs.preview_url }} |