使用 Hugo 和 Github Pages 生成并部署静态站点
🛠
此页内容待完善
生成 Hugo 及 Hextra 配置文件
参考链接:
此处使用 Hugo 的 Hextra 主题来构建项目
hugo new site lowcost-ha-local-k8s --format=yaml- 使用 Hugo 模块初始化 Hextra 主题
# 初始化 Hugo 模块
cd lowcost-ha-local-k8s
# GITHUB_MIRROR="ghfast.top/https://github.com"
GITHUB_MIRROR="github.com"
hugo mod init ${GITHUB_MIRROR}/Tsanfer/lowcost-ha-local-k8s
# 整理并下载 Hugo 模块所需依赖
hugo mod tidy
hugo mod graph
# 添加 Hextra 主题
hugo mod get ${GITHUB_MIRROR}/imfing/hextra- 在配置文件中启用 Hextra 主题
文件位置:./hugo.yaml
./hugo.yaml
| |
- 创建站点页面
hugo new content/_index.md
hugo new content/docs/_index.md- 本地浏览站点
hugo server --buildDrafts --disableFastRender用浏览器访问运行服务的机器的 1313 端口即可,比如 http://localhost:1313/
- (可选)更新 Hugo 模块与主题
如果需要更新 Hugo 模块或者主题,则可执行:
# 更新所有 Hugo 模块
# hugo mod get -u
# 更新 Hextra 主题
# GITHUB_MIRROR="ghfast.top/https://github.com"
GITHUB_MIRROR="github.com"
hugo mod get -u ${GITHUB_MIRROR}/imfing/hextra自动构建部署到 Github Pages
参考链接:部署站点 – Hextra
更改图片缓存路径:
文件位置:./hugo.yaml
./hugo.yaml
| |
创建 Github Actions 文件:
mkdir -p .github/workflows
touch .github/workflows/hugo.yaml根据 Hextra 教程配置 CI/CD 流程:
文件位置:.github/workflows/pages.yaml
.github/workflows/pages.yaml
| |
提交 Git 更新并推送到 Github:
git add -A
git commit -m "Create hugo.yaml"
git push将 Github 仓库设置中的 Pages → Build and deployment → Source 设置为 GitHub Actions
- 添加自定义域名加速访问
如果需要自定义域名访问,则需要 Github Actions 配置文件:
文件位置:.github/workflows/pages.yaml
.github/workflows/pages.yaml
| |
将 --baseURL 参数改为自定义的域名
在 Github 仓库设置的 Pages → Custom domain 中填入自定义域名
登录 DNS 服务商后台,添加一条 CNAME 记录:将域名指向 GitHub Pages 的地址(如 username.github.io)