第7章 部署——用 GitHub Pages 公开
必读 想跟着这个阶段学下去,一定要读这一章。一句话总结:GitHub Pages 是一项免费服务,能把仓库里的网页文件原样放到互联网上,变成一个网站。免费账号必须是公开仓库才能用。 部署是会影响外部世界的行为,所以用 Claude Code 的回退也无法撤销。 能不能公开,要由人来确认再点按钮。
本章依据 2026年11月1日确认的 GitHub、Claude Code 官方文档写成。GitHub 界面以英文版为准来写。
GitHub Pages 是什么
GitHub Pages 是一项服务:它接收仓库里的网页文件(HTML、CSS、JavaScript,组成网页的文件),再发布成网站。[1] 必要时,发布之前会先经过构建(把文件变成能放上网站的样子的过程)。待办看板只要把文件原样传上去就行,所以不用操心构建。
Pages 是放静态网站(Static site)的地方。静态网站不需要根据每位访客变换内容的服务器,只是把固定的文件原样展示出来。像待办看板这样展示表格的页面,很适合用它。
就像第2章看到的,免费账号只能在公开仓库里使用 Pages。[2] 而且 Pages 网站会向互联网公开。 即使套餐允许、仓库设成了私有,网站依然是公开的,所以敏感数据要在发布前删掉。[3]
理解检查
关于免费账号的 GitHub Pages,哪种说法是对的?
发布的两种方法
| 方法 | 什么时候用 |
|---|---|
| 从分支发布 | 不需要另外调整构建过程时(推荐) |
| 用 GitHub Actions 发布 | 想自己决定构建过程时 |
出处:GitHub 官方文档(只摘录了一部分)[4]
第二种方法里的 GitHub Actions,是你在第5章见过的功能。每次上传时,GitHub 会自动执行事先定好的工作。这个方法就是让这个功能连发布也一起负责。
像待办看板这样直接展示文件的页面,从分支发布就够了。官方文档也建议,不需要调整构建过程时用这个方法。[4:1] 选好分支和文件夹(仓库最外层 / 或 /docs)之后,每次往那个分支上传,那个文件夹里的内容就会被发布。[5]
发布后的网站网址,样子是 https://我的ID.github.io/仓库名/。
详细说明:用 GitHub Actions 发布的方法(点击展开)
本指南的「亲自动手试试」里不会用到它。因为 GitHub 的入门教程用的是这个方法,为了让你看教程时不糊涂,在这里写一下。
在设置的 Pages 里把 Source 改成 GitHub Actions,再放进部署设置文件,之后每次上传到 main,网站就会重新部署。[6]
理解检查
像待办看板这种不需要处理构建过程的页面,官方文档建议用哪种发布方式?
限额与禁止事项
虽然免费,但有不能用的场合。先记住这两点就行。
练习用的待办看板,两条都不沾边。
访客的 IP 地址会被记录。 IP 地址是连上互联网的每台设备都有的编号。只要有人访问 Pages 网站,无论是否登录,出于安全目的,访客的 IP 地址都会被记录并保存。[9] 如果你想在自己的页面上写"我们什么都不收集",请把这一点也一并写上。
详细说明:大小、时间限额和练习网站的条件(点击展开)
| 类别 | 限额 |
|---|---|
| 大小 | 已发布的网站不超过 1GB |
| 时间 | 部署超过10分钟就失败 |
| 带宽 | 每月 100GB(宽松的限额) |
出处:GitHub 官方文档(只摘录了一部分)[10][11][12]
带宽是发送给访客的数据量。
模仿真实服务做的练习网站有条件。为了学习而照着做本身不被禁止,但代码必须自己写,不能收集用户数据,并且要挂上醒目的声明,说明这是与原网站无关的教学用途。[13]
理解检查
哪种用途不能用 GitHub Pages?
部署无法用回退撤销
你在第3阶段学到的 Claude Code 回退(检查点),是会话内部的快速恢复工具。永久的记录由 Git 负责。[14] 而像数据库、外部服务、部署这样会影响外部系统的行为,检查点是撤销不了的。[15]
所以部署之前,要由人来确认。官方文档也写明,检查建议的命令是否安全、批准之前要先审阅,这是用户的责任。[16]
部署前,请问问自己这些问题。
- 第5章的检查环节通过了吗?
- 第6章的审阅做完、PR 合并了吗?
- 公开仓库和页面里,没有真实姓名、联系方式、密码吧?(第9章)
如果传上去的是敏感内容,光是撤回还不够。因为它会留在公开仓库的 Git 记录里。请按第9章的顺序(先作废、更换)来处理。
理解检查
为什么不能用 Claude Code 的回退(检查点)来撤销部署?
亲自动手试试
- 先用第9章的公开前检查表确认密钥和个人信息。
- 在 GitHub 上打开
cafe-todo-board仓库的 Settings → Pages。 - 在「Build and deployment」的 Source 里选 Deploy from a branch,在 Branch 里选
main分支和/(最外层)文件夹。[17] - 确认仓库最外层有
index.html。GitHub Pages 会在发布文件夹的最外层寻找这个文件。[18] 然后打开https://我的ID.github.io/cafe-todo-board/。上传之后,最长可能要10分钟左右才会生效。[17:1] 看到待办看板就成功了。 - 也用手机打开看看。
- 想一想,要不要在页面下方加上这样的声明:"这是为练习做的页面,不收集访问信息(GitHub Pages 出于安全目的会记录访客 IP)"。
连续任务:第8章要把"过期的待办标成红色"这样的改进写成议题,再多走一圈。
小结
- GitHub Pages 把仓库里的网页文件发布成网站。免费账号只能用公开仓库,网站总是公开的。
- 不需要调整构建时,就从分支发布。
- 不要用于经营商业、网店、输入密码。有大小、时间上的限额,访客 IP 会被记录。
- 部署无法用回退撤销。确认、审阅、公开前检查都做完,再由人来点。
术语解释
| 术语 | 中文名称 | 一句话解释 |
|---|---|---|
| GitHub Pages | GitHub Pages | 把仓库文件发布成免费网站的服务 |
| Static site | 静态网站 | 把固定的文件原样展示出来的网站 |
| Hosting | 托管 | 把网站放在互联网上 |
| Bandwidth | 带宽 | 发送给访客的数据量 |
这一章读完了吗?
学习记录只保存在这个浏览器中。整体进度可以在 全部目录中查看。
下一章:第8章. 运营与改进
参考资料(18 条)
GitHub 官方文档,「What is GitHub Pages?」。"GitHub Pages is a static site hosting service that takes HTML, CSS, and JavaScript files straight from a repository on GitHub, optionally runs the files through a build process, and publishes a website." docs.github.com (确认日期 2026-11-01) ↩︎
GitHub 官方文档,「What is GitHub Pages?」。"GitHub Pages is available in public repositories with GitHub Free and GitHub Free for organizations, and in public and private repositories with GitHub Pro, GitHub Team, GitHub Enterprise Cloud, and GitHub Enterprise Server." docs.github.com (确认日期 2026-11-01) ↩︎
GitHub 官方文档,「Configuring a publishing source for your GitHub Pages site」。"GitHub Pages sites are publicly available on the internet, even if the repository for the site is private (if your plan or organization allows it)." docs.github.com (确认日期 2026-11-01) ↩︎
GitHub 官方文档,「Configuring a publishing source for your GitHub Pages site」。"You can publish your site when changes are pushed to a specific branch, or you can write a GitHub Actions workflow to publish your site." / "If you do not need any control over the build process for your site, we recommend that you publish your site when changes are pushed to a specific branch." docs.github.com (确认日期 2026-11-01) ↩︎
GitHub 官方文档,「Configuring a publishing source for your GitHub Pages site」。"Whenever changes are pushed to the source branch, the changes in the source folder will be published to your GitHub Pages site." docs.github.com (确认日期 2026-11-01) ↩︎
GitHub 官方文档,「Deploying your website automatically」。"Under "Build and deployment", from the Source dropdown, select GitHub Actions." / "From now on, every push to
mainredeploys your site with your latest changes." docs.github.com (确认日期 2026-11-01) ↩︎GitHub 官方文档,「GitHub Pages limits」。"GitHub Pages is not intended for or allowed to be used as a free web-hosting service to run your online business, e-commerce site, or any other website that is primarily directed at either facilitating commercial transactions or providing commercial software as a service (SaaS)." docs.github.com (确认日期 2026-11-01) ↩︎
GitHub 官方文档,「GitHub Pages limits」。"GitHub Pages sites shouldn't be used for sensitive transactions like sending passwords or credit card numbers." docs.github.com (确认日期 2026-11-01) ↩︎
GitHub 官方文档,「What is GitHub Pages?」。"When a GitHub Pages site is visited, the visitor's IP address is logged and stored for security purposes, regardless of whether the visitor has signed into GitHub or not." docs.github.com (确认日期 2026-11-01) ↩︎
GitHub 官方文档,「GitHub Pages limits」。"Published GitHub Pages sites may be no larger than 1 GB." docs.github.com (确认日期 2026-11-01) ↩︎
GitHub 官方文档,「GitHub Pages limits」。"GitHub Pages deployments will timeout if they take longer than 10 minutes." docs.github.com (确认日期 2026-11-01) ↩︎
GitHub 官方文档,「GitHub Pages limits」。"GitHub Pages sites have a soft bandwidth limit of 100 GB per month." docs.github.com (确认日期 2026-11-01) ↩︎
GitHub 官方文档,「GitHub Pages limits」。"Using GitHub Pages to create a copy of an existing website as a learning exercise is not prohibited." / "you must write the code yourself, the site must not collect any user data, and you must include a prominent disclaimer on the site indicating that the project is not associated with the original and was only created for educational purposes." docs.github.com (确认日期 2026-11-01) ↩︎
Claude Code 官方文档,「Checkpointing」。"Checkpoints are designed for quick, session-level recovery. For permanent version history and collaboration, continue using version control, such as Git, for commits, branches, and long-term history." code.claude.com (确认日期 2026-11-01) ↩︎
Claude Code 官方文档,「How Claude Code works」。"Actions that affect remote systems (databases, APIs, deployments) can't be checkpointed. You control those with your permission mode and permission rules." code.claude.com (确认日期 2026-11-01) ↩︎
Claude Code 官方文档,「Security」。"You're responsible for reviewing proposed code and commands for safety before approval." code.claude.com (确认日期 2026-11-01) ↩︎
GitHub 官方文档,「Quickstart for GitHub Pages」。"Under "Build and deployment", under "Source", select Deploy from a branch." / "Note that it can take up to 10 minutes for changes to your site to publish after you push the changes to GitHub." docs.github.com (确认日期 2026-11-01) ↩︎
GitHub 官方文档,「Creating a GitHub Pages site」。"GitHub Pages will look for an
index.html,index.md, orREADME.mdfile as the entry file for your site." docs.github.com (确认日期 2026-11-01) ↩︎