Skip to content

支持为 PR 生成预览构建#308

Merged
burningtnt merged 19 commits intoHMCL-dev:mainfrom
neveler:dev-pr-preview
Oct 31, 2025
Merged

支持为 PR 生成预览构建#308
burningtnt merged 19 commits intoHMCL-dev:mainfrom
neveler:dev-pr-preview

Conversation

@neveler
Copy link
Copy Markdown
Contributor

@neveler neveler commented Oct 20, 2025

支持为 PR 生成预览构建

Note

欢迎大家测试并提供反馈!

关联 ISSUE

功能

创建预览

仓库所属组织的成员和仓库所有者在需要创建预览的 PR 添加 /preview create 评论或者添加 preview/create 标签触发 Github Action 工作流创建 PR 预览。

移除预览

Note

移除预览仅删除缓存中当前 PR 产生的构建文件,因此在 Github Pages 下次构建成功前页面仍可访问。

仓库所属组织的成员和仓库所有者在需要移除预览的 PR 添加 /preview remove 评论或添加 preview/remove 标签也可以关闭 PR 触发 Github Action 工作流移除 PR 预览。

存在的问题

  • jekyll 支持插件机制,插件可能在构建过程中造成破坏性操作,故可能存在安全问题
    • 为提升安全性,目前构建步骤在最低权限环境下运行,并且构建必须手动触发
  • 产物中部分文章存在路径错误(需要对文章路径进行特殊处理)
    • 需要对文章中的相对链接做特殊处理,例如使用 {{ /path | relative_url }} 或者 {% link _docs/xxxx.md %}

@burningtnt burningtnt requested review from Glavo and zkitefly October 20, 2025 14:46
@zkitefly
Copy link
Copy Markdown
Member

缺少删除历史构建的操作(例如自动删除已关闭的 PR 或者过于久远的 PR )

这个其实我不是很能接受

@neveler
Copy link
Copy Markdown
Contributor Author

neveler commented Oct 21, 2025

这个其实我不是很能接受

怎么说

@zkitefly
Copy link
Copy Markdown
Member

zkitefly commented Oct 21, 2025

这个其实我不是很能接受

怎么说

只是觉得有些浪费资源(我看到你的更新了,感觉还可以

我觉得可以让有权限的人使用评论 /remove-preview/preview 来触发更好

@burningtnt
Copy link
Copy Markdown
Member

burningtnt commented Oct 21, 2025

可以使用 $.jobs[].if 来按需触发构建,实现权限控制没有技术困难。

@neveler neveler changed the title 尝试提供 PR 预览部署 支持为 PR 生成预览构建 Oct 22, 2025
@neveler
Copy link
Copy Markdown
Contributor Author

neveler commented Oct 22, 2025

我觉得可以让有权限的人使用评论 /remove-preview/preview 来触发更好

现在可以用 /preview create/preview remove 创建移除预览了

创建预览时会创建一个 check 用于检查预览部署是否成功

使用 action cache 替代分支 gh pages 持久化构建产物,相比于分支 cache 会在一段时间后自动清除,工作流创建新 cache 时会删除旧的 cache 避免过量堆积

Copy link
Copy Markdown
Member

@burningtnt burningtnt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

大体上没什么问题,感谢您的贡献。

我计划创建一个新的临时仓库来作为测试环境,避免错误的更改进入主线。

Comment thread .github/workflows/pr-preview.yml
Comment thread .github/workflows/pr-preview.yml Outdated
contents: write
pull-requests: write
steps:
- name: Restore Pages
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果完全不使用缓存,构建时间会被拉长到多少?

Copy link
Copy Markdown
Contributor Author

@neveler neveler Oct 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

使用缓存的目的并不仅仅是加快构建速度而是为了保留历史构建,不能因为构建当前 PR 就覆盖掉了其它 PR 的构建。

Comment thread .github/workflows/pr-preview.yml Outdated
JEKYLL_ENV: production
jobs:
remove:
if: ${{ (github.event_name == 'pull_request_target' && (github.event.action == 'closed' || (github.event.action == 'labeled' && github.event.label.name == 'preview/remove'))) || (github.event_name == 'issue_comment' && github.event.issue.pull_request && (github.event.comment.author_association == 'OWNER' || github.event.comment.author_association == 'MEMBER') && github.event.comment.body == '/preview remove') }}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我感觉不必支持通过标签来管理预览构建。 @zkitefly 你怎么看?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我感觉不必支持通过标签来管理预览构建。 @zkitefly 你怎么看?

我是先基于添加标签实现的工作流,写都写了感觉也没必要删,不用就好了。

@burningtnt
Copy link
Copy Markdown
Member

此外,我注意到目前已使用 GitHub Check 显示预览构建生成状态。如果可能,我认为可以移除基于评论的方式。

@neveler

This comment was marked as outdated.

@burningtnt
Copy link
Copy Markdown
Member

此外,我注意到目前已使用 GitHub Check 显示预览构建生成状态。如果可能,我认为可以移除基于评论的方式。

https://github.com/hmcl-docs/HMCL-docs/pull/3/checks?check_run_id=53383468933

这样吗?感觉不是很直观。

emmm…… 我本来以为这玩意儿会显示在这个区域里的。这还是太不明显了,那保留基于评论来显示状态的方式吧。

@neveler
Copy link
Copy Markdown
Contributor Author

neveler commented Oct 23, 2025

现在工作流会判断当前仓库是否为 fork 仓库,因此如需测试工作流可以在仓库设置中 Leave fork network 或自行修改工作流判断条件

  • 恢复了基于评论的消息通知
  • 页面构建完成后会自动删除 artifact
  • 仅在非 fork 仓库下运行工作流
  • 自动获取当前仓库的 page domain

@neveler
Copy link
Copy Markdown
Contributor Author

neveler commented Oct 23, 2025

支持使用指令 /preview watch 或添加 preview/watch 标签对指定 PR 启用监听,启用监听后将在 PR 有新提交时自动构建预览。由于该构建是自动触发的,所以请确保应用在可靠受信任的 PR 上。

watch 操作会自动触发 create 操作,因此 watch 前无需额外的 create 操作。

可通过 /preview remove 或者手动移除 preview/watch 标签的方式停止监听。

Copy link
Copy Markdown
Member

@burningtnt burningtnt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

大体上没有什么问题。PR 当前的状态是?

@neveler
Copy link
Copy Markdown
Contributor Author

neveler commented Oct 23, 2025

大体上没有什么问题。PR 当前的状态是?

我这边目前没有其它问题

Copy link
Copy Markdown
Member

@burningtnt burningtnt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我注意到当前 PR 对 github.event.comment.body 有些地方使用了 == 判断,而又有些地方使用了 contains 来同时判断是否满足任一条件。这可能导致用户在发送 123 /preview create 123 时表现异常。

Comment thread .github/workflows/pr-preview.yml Outdated
Comment thread .github/workflows/pr-preview.yml Outdated
Comment thread .github/workflows/pr-preview.yml Outdated
neveler and others added 4 commits October 23, 2025 20:17
Co-authored-by: Burning_TNT <pangyl08@163.com>
Co-authored-by: Burning_TNT <pangyl08@163.com>
Co-authored-by: Burning_TNT <pangyl08@163.com>
@neveler
Copy link
Copy Markdown
Contributor Author

neveler commented Oct 23, 2025

我注意到当前 PR 对 github.event.comment.body 有些地方使用了 == 判断,而又有些地方使用了 contains 来同时判断是否满足任一条件。这可能导致用户在发送 123 /preview create 123 时表现异常。

contains(xxxx, yyyy) 表示 xxxx 中是否包含 yyyy 因此,原本使用字符串包含判断的逻辑确实存在一些问题,现在已改用数组判断。

@neveler neveler requested a review from burningtnt October 23, 2025 12:24
Copy link
Copy Markdown
Member

@burningtnt burningtnt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Glavo @zkitefly Request Reviews

@neveler
Copy link
Copy Markdown
Contributor Author

neveler commented Oct 24, 2025

支持在合并缓存时进行文件去重,将所有文件统一存储到 ./data/files/{hash}-{size},并通过符号链接替代原有文件路径。同时在移除文件后也会删除无引用的原始文件。

未去重的缓存中,每新增一次构建缓存大约会增加 20MB 左右;去重后在静态资源没有显著变动的情况下,缓存大小基本保持不变。

@burningtnt
Copy link
Copy Markdown
Member

我希望能暂时成为 https://github.com/hmcl-docs/HMCL-docs 组织成员,以测试最新的更改。

@neveler
Copy link
Copy Markdown
Contributor Author

neveler commented Oct 25, 2025

我希望能暂时成为 https://github.com/hmcl-docs/HMCL-docs 组织成员,以测试最新的更改。

应该可以了

@burningtnt
Copy link
Copy Markdown
Member

我计划在明后两天接受该 PR

@burningtnt burningtnt merged commit 2282931 into HMCL-dev:main Oct 31, 2025
@github-actions
Copy link
Copy Markdown

[Preview] Preview has been removed.

@burningtnt
Copy link
Copy Markdown
Member

…… 原来 HMCL-docs 一直没开 GitHub Page 吗……

@neveler neveler deleted the dev-pr-preview branch November 1, 2025 07:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants