-
Notifications
You must be signed in to change notification settings - Fork 907
ci: 添加PR部署工作流 #829
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
lingbopro
wants to merge
5
commits into
win12-online:main
Choose a base branch
from
lingbopro:branch_deploy_action
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
ci: 添加PR部署工作流 #829
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,80 @@ | ||
| name: Pull Request Deploy | ||
|
|
||
| on: | ||
| pull_request: | ||
| types: [opened, synchronize, reopened] | ||
| permissions: | ||
| contents: read | ||
| pull-requests: write | ||
| jobs: | ||
| deploy: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout repo | ||
| uses: actions/checkout@v6 | ||
| - name: Deploy | ||
| id: deploy | ||
| uses: cloudflare/wrangler-action@v3 | ||
| with: | ||
| apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} | ||
| accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} | ||
| command: | | ||
| pages deploy . --project-name=win12-online --branch=pr-preview/${{ github.event.pull_request.number }} --commit-hash=${{ github.event.pull_request.head.sha }} | ||
| - name: Post comment | ||
| if: ${{ steps.deploy.outputs.deployment-url != '' }} | ||
| uses: actions/github-script@v9 | ||
| with: | ||
| script: | | ||
| // Check if there is already a comment | ||
| let commentId = null; | ||
| await github.paginate( | ||
| 'GET /repos/{owner}/{repo}/issues/{issue_number}/comments', | ||
| { | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: ${{ github.event.pull_request.number }}, | ||
| }, | ||
| (response) => { | ||
| for (const comment of response) { | ||
| if ( | ||
| comment.user.login === 'github-actions[bot]' && | ||
| comment.body.includes('<!-- Deploy Bot Comment -->')) { | ||
| commentId = comment.id; | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| ); | ||
|
|
||
| console.log('commentId: ', commentId); | ||
| const comment = ` | ||
| <!-- Deploy Bot Comment --> | ||
| ## ⚡ 已自动部署到 Cloudflare Pages | ||
|
|
||
| 预览地址: <${{ steps.deploy.outputs.deployment-url }}> | ||
| 提交hash: ${{ github.event.pull_request.head.sha }} | ||
|
|
||
| PR 更新时会自动进行部署并更新此评论 | ||
|
|
||
| ###### 本评论由 GitHub Actions 工作流自动生成~ | ||
| `; | ||
| console.log('comment: ', comment); | ||
|
|
||
| if (!commentId) { | ||
| await github.rest.issues.createComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: ${{ github.event.pull_request.number }}, | ||
| body: comment, | ||
| }); | ||
| } | ||
| else { | ||
| await github.rest.issues.updateComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| comment_id: commentId, | ||
| body: comment, | ||
| }); | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR似乎无法访问secrets捏,最好改用pull_request_target
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好像确实)
不过,
pull_request_target在源仓库的主分支上运行,那还能实时预览执行效果嘛?而且
pull_request_target工作流不能直接checkout PR里的代码There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
问了 GitHub Docs 内置的Copilot,它是这么说的:
https://docs.github.com/?search-overlay-open=true&search-overlay-ask-ai=true&search-overlay-input=can%20workflows%20(triggered%20by%20pull_request%20trigger)%20read%20repository%20secrets%3F
https://docs.github.com/?search-overlay-open=true&search-overlay-ask-ai=true&search-overlay-input=How%20do%20I%20checkout%20the%20PR's%20code%20from%20a%20pull_request_target%20workflow%3F
图片