-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
107 additions
and
90 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,13 @@ | ||
## 使用说明 | ||
|
||
本说明适用于部署在 Cloudflare 的场景。对于其他部署方案,默认导出的 URL 变量名可能不是 `url`,需结合文档对 `steps.deploy.outputs.url` 后的变量名稍作修改。 | ||
|
||
1. 在 `deploy` 层级下使用 `outputs` 导出生成的 URL | ||
2. 在 `jobs` 层级下引用工作流 | ||
|
||
## 代码样例 | ||
|
||
```yaml | ||
jobs: | ||
deploy: | ||
runs-on: ubuntu-latest | ||
outputs: | ||
preview_url: ${{ steps.deploy.outputs.url }} | ||
steps: | ||
# 下方部署过程略 | ||
``` | ||
### 使用说明 | ||
|
||
```yaml | ||
# 上方部署过程略 | ||
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 }} | ||
- name: bot | ||
uses: project-trans/Display-Preview-URL-in-PR@SHA | ||
with: | ||
previewUrl: ${{ steps.deploy.outputs.deployment-url }} | ||
BOT_APP_ID: ${{ vars.BOT_APP_ID }} | ||
BOT_APP_SECRET: ${{ secrets.BOT_APP_SECRET }} | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
REMOVE_PREFIX: docs | ||
REMOVE_SUFFIX: .md | ||
``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
name: 'pr preview action' | ||
description: 'Generate and post a preview URL as a PR comment.' | ||
branding: | ||
icon: 'award' | ||
color: 'green' | ||
|
||
inputs: | ||
previewUrl: | ||
required: true | ||
BOT_APP_ID: | ||
required: true | ||
BOT_APP_SECRET: | ||
required: true | ||
GITHUB_TOKEN: | ||
required: true | ||
REMOVE_PREFIX: | ||
required: false | ||
REMOVE_SUFFIX: | ||
required: false | ||
|
||
runs: | ||
using: "composite" | ||
steps: | ||
- name: 使用 GitHub App 进行身份验证 | ||
id: auth | ||
uses: actions/create-github-app-token@v1 | ||
with: | ||
app-id: ${{ inputs.BOT_APP_ID }} | ||
private-key: ${{ inputs.BOT_APP_SECRET }} | ||
owner: ${{ github.repository_owner }} | ||
|
||
- name: List PR files using GitHub CLI | ||
id: url | ||
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 | ||
if [[ $file == *.md ]]; then | ||
# 去掉路径前缀和后缀 | ||
modified_file="${file#${{ inputs.REMOVE_PREFIX }}}" # 删除路径前缀 | ||
modified_file="${modified_file%${{ inputs.REMOVE_SUFFIX }}}" # 删除路径后缀 | ||
# 拼接 URL | ||
file_url="${{ inputs.previewUrl }}${modified_file}" | ||
all_file_urls="${all_file_urls}\n${file_url}" | ||
fi | ||
done | ||
echo "all_file_urls=${all_file_urls}" >> $GITHUB_ENV | ||
echo -e "All File URLs: ${{ env.all_file_urls }}" | ||
shell: bash | ||
env: | ||
GH_TOKEN: ${{ inputs.GITHUB_TOKEN }} | ||
|
||
# 获取预览链接并发送到 PR | ||
- name: 发送整体 PR review | ||
uses: actions/github-script@v6 | ||
with: | ||
github-token: ${{ steps.auth.outputs.token }} | ||
script: | | ||
const prNumber = context.payload.pull_request.number; | ||
const reviewBody = `🚀 预览部署完成!访问链接: ${{ inputs.previewUrl }}\n\n✨ 本 PR 修改了以下页面: ✨${{ env.all_file_urls }}`; | ||
// 获取现有 review | ||
const { data: reviews } = await github.rest.pulls.listReviews({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
pull_number: prNumber, | ||
}); | ||
// 查找已有的评论 review | ||
const existingReview = reviews.find(review => | ||
review.body.includes('🚀 预览部署完成!')); | ||
if (existingReview) { | ||
// 如果已经有 review,更新它 | ||
await github.rest.pulls.updateReview({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
pull_number: prNumber, | ||
review_id: existingReview.id, | ||
body: reviewBody, | ||
}); | ||
} else { | ||
// 如果没有 review,创建新的 review | ||
await github.rest.pulls.createReview({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
pull_number: prNumber, | ||
body: reviewBody, | ||
event: "COMMENT", | ||
}); | ||
} |