2024-04-21 18:46:21 +08:00
|
|
|
|
---
|
2024-04-23 22:20:01 +02:00
|
|
|
|
title: 部署站点
|
2024-04-21 18:46:21 +08:00
|
|
|
|
prev: /docs/guide/shortcodes
|
|
|
|
|
next: /docs/advanced
|
|
|
|
|
---
|
2024-04-23 22:08:06 +02:00
|
|
|
|
|
2025-08-14 23:49:06 +08:00
|
|
|
|
Hugo 生成静态网站,支持灵活的托管方案。
|
|
|
|
|
本页提供在各类平台上部署 Hextra 站点的指南。
|
2024-04-21 18:46:21 +08:00
|
|
|
|
|
|
|
|
|
<!--more-->
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## GitHub Pages
|
|
|
|
|
|
2025-08-14 23:49:06 +08:00
|
|
|
|
[GitHub Pages](https://docs.github.com/pages) 是推荐的免费部署托管方案。
|
2024-04-21 18:46:21 +08:00
|
|
|
|
|
2025-08-14 23:49:06 +08:00
|
|
|
|
若使用 [hextra-starter-template](https://github.com/imfing/hextra-starter-template) 初始化项目,已内置 GitHub Actions 工作流,可自动部署至 GitHub Pages。
|
2024-04-21 18:46:21 +08:00
|
|
|
|
|
2024-12-31 00:34:20 +00:00
|
|
|
|
{{% details title="GitHub Actions 配置" closed="true" %}}
|
2024-04-21 18:46:21 +08:00
|
|
|
|
|
2024-12-31 00:34:20 +00:00
|
|
|
|
以下是 [hextra-starter-template](https://github.com/imfing/hextra-starter-template) 的示例配置:
|
2024-04-21 18:46:21 +08:00
|
|
|
|
|
|
|
|
|
```yaml {filename=".github/workflows/pages.yaml"}
|
2025-08-14 23:49:06 +08:00
|
|
|
|
# 构建并部署 Hugo 站点到 GitHub Pages 的示例工作流
|
2024-12-31 00:34:20 +00:00
|
|
|
|
name: 部署 Hugo 站点到 Pages
|
2024-04-21 18:46:21 +08:00
|
|
|
|
|
|
|
|
|
on:
|
2025-08-14 23:49:06 +08:00
|
|
|
|
# 针对默认分支的推送触发
|
2024-04-21 18:46:21 +08:00
|
|
|
|
push:
|
|
|
|
|
branches: ["main"]
|
|
|
|
|
|
2025-08-14 23:49:06 +08:00
|
|
|
|
# 允许从 Actions 标签手动运行
|
2024-04-21 18:46:21 +08:00
|
|
|
|
workflow_dispatch:
|
|
|
|
|
|
2025-08-14 23:49:06 +08:00
|
|
|
|
# 设置 GITHUB_TOKEN 权限以允许部署到 GitHub Pages
|
2024-04-21 18:46:21 +08:00
|
|
|
|
permissions:
|
|
|
|
|
contents: read
|
|
|
|
|
pages: write
|
|
|
|
|
id-token: write
|
|
|
|
|
|
2025-08-14 23:49:06 +08:00
|
|
|
|
# 仅允许一个并发部署,跳过正在运行与最新排队之间的运行
|
|
|
|
|
# 但不会取消进行中的运行,以确保生产部署完成
|
2024-04-21 18:46:21 +08:00
|
|
|
|
concurrency:
|
|
|
|
|
group: "pages"
|
|
|
|
|
cancel-in-progress: false
|
|
|
|
|
|
2024-12-31 00:34:20 +00:00
|
|
|
|
# 默认使用 bash
|
2024-04-21 18:46:21 +08:00
|
|
|
|
defaults:
|
|
|
|
|
run:
|
|
|
|
|
shell: bash
|
|
|
|
|
|
|
|
|
|
jobs:
|
2024-12-31 00:34:20 +00:00
|
|
|
|
# 构建任务
|
2024-04-21 18:46:21 +08:00
|
|
|
|
build:
|
|
|
|
|
runs-on: ubuntu-latest
|
|
|
|
|
env:
|
2025-06-04 00:38:38 +01:00
|
|
|
|
HUGO_VERSION: 0.147.7
|
2024-04-21 18:46:21 +08:00
|
|
|
|
steps:
|
2025-08-14 23:49:06 +08:00
|
|
|
|
- name: 检出代码
|
2024-04-21 18:46:21 +08:00
|
|
|
|
uses: actions/checkout@v4
|
|
|
|
|
with:
|
2025-08-14 23:49:06 +08:00
|
|
|
|
fetch-depth: 0 # 获取完整历史记录以支持 .GitInfo 和 .Lastmod
|
2024-04-21 18:46:21 +08:00
|
|
|
|
submodules: recursive
|
2025-08-14 23:49:06 +08:00
|
|
|
|
- name: 设置 Go 环境
|
2024-04-21 18:46:21 +08:00
|
|
|
|
uses: actions/setup-go@v5
|
|
|
|
|
with:
|
2024-12-31 00:34:20 +00:00
|
|
|
|
go-version: '1.22'
|
2025-08-14 23:49:06 +08:00
|
|
|
|
- name: 配置 Pages
|
2024-04-21 18:46:21 +08:00
|
|
|
|
id: pages
|
|
|
|
|
uses: actions/configure-pages@v4
|
2025-08-14 23:49:06 +08:00
|
|
|
|
- name: 安装 Hugo
|
2024-04-21 18:46:21 +08:00
|
|
|
|
run: |
|
|
|
|
|
wget -O ${{ runner.temp }}/hugo.deb https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_linux-amd64.deb \
|
|
|
|
|
&& sudo dpkg -i ${{ runner.temp }}/hugo.deb
|
2024-12-31 00:34:20 +00:00
|
|
|
|
- name: 使用 Hugo 构建
|
2024-04-21 18:46:21 +08:00
|
|
|
|
env:
|
2025-08-14 23:49:06 +08:00
|
|
|
|
# 为 Hugo 模块提供最大兼容性
|
2024-04-21 18:46:21 +08:00
|
|
|
|
HUGO_ENVIRONMENT: production
|
|
|
|
|
HUGO_ENV: production
|
|
|
|
|
run: |
|
|
|
|
|
hugo \
|
|
|
|
|
--gc --minify \
|
|
|
|
|
--baseURL "${{ steps.pages.outputs.base_url }}/"
|
2025-08-14 23:49:06 +08:00
|
|
|
|
- name: 上传产物
|
2024-04-21 18:46:21 +08:00
|
|
|
|
uses: actions/upload-pages-artifact@v3
|
|
|
|
|
with:
|
|
|
|
|
path: ./public
|
|
|
|
|
|
2024-12-31 00:34:20 +00:00
|
|
|
|
# 部署任务
|
2024-04-21 18:46:21 +08:00
|
|
|
|
deploy:
|
|
|
|
|
environment:
|
|
|
|
|
name: github-pages
|
|
|
|
|
url: ${{ steps.deployment.outputs.page_url }}
|
|
|
|
|
runs-on: ubuntu-latest
|
|
|
|
|
needs: build
|
|
|
|
|
steps:
|
2024-12-31 00:34:20 +00:00
|
|
|
|
- name: 部署到 GitHub Pages
|
2024-04-21 18:46:21 +08:00
|
|
|
|
id: deployment
|
|
|
|
|
uses: actions/deploy-pages@v4
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
{{% /details %}}
|
|
|
|
|
|
|
|
|
|
|
2025-08-22 00:31:16 +02:00
|
|
|
|
{{< callout type="warning" >}}
|
2025-08-14 23:49:06 +08:00
|
|
|
|
在仓库设置中,将 **Pages** > **构建与部署** > **源** 设为 **GitHub Actions**:
|
2024-04-21 18:46:21 +08:00
|
|
|
|

|
|
|
|
|
{{< /callout >}}
|
|
|
|
|
|
2025-08-14 23:49:06 +08:00
|
|
|
|
默认配置中,GitHub Actions 工作流 `.github/workflows/pages.yaml` 假设站点部署在 `https://<用户名>.github.io/<仓库名>/`。
|
2024-04-21 18:46:21 +08:00
|
|
|
|
|
2025-08-14 23:49:06 +08:00
|
|
|
|
若需部署到 `https://<用户名>.github.io/`,请修改 `--baseURL`:
|
2024-04-21 18:46:21 +08:00
|
|
|
|
|
|
|
|
|
```yaml {filename=".github/workflows/pages.yaml",linenos=table,linenostart=54,hl_lines=[4]}
|
|
|
|
|
run: |
|
|
|
|
|
hugo \
|
|
|
|
|
--gc --minify \
|
|
|
|
|
--baseURL "https://${{ github.repository_owner }}.github.io/"
|
|
|
|
|
```
|
|
|
|
|
|
2025-08-14 23:49:06 +08:00
|
|
|
|
若使用自定义域名,请相应调整 `--baseURL` 值。
|
2024-04-21 18:46:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Cloudflare Pages
|
|
|
|
|
|
2025-08-14 23:49:06 +08:00
|
|
|
|
1. 将站点源码存入 Git 仓库(如 GitHub)
|
|
|
|
|
2. 登录 [Cloudflare 控制台](https://dash.cloudflare.com/) 并选择账户
|
|
|
|
|
3. 在账户首页选择 **Workers & Pages** > **创建应用** > **Pages** > **连接 Git**
|
|
|
|
|
4. 选择仓库后,在 **设置构建与部署** 部分填写:
|
2024-04-21 18:46:21 +08:00
|
|
|
|
|
2025-08-14 23:49:06 +08:00
|
|
|
|
| 配置项 | 值 |
|
|
|
|
|
| ---------------- | -------------------- |
|
|
|
|
|
| 生产分支 | `main` |
|
|
|
|
|
| 构建命令 | `hugo --gc --minify` |
|
|
|
|
|
| 构建输出目录 | `public` |
|
2024-04-21 18:46:21 +08:00
|
|
|
|
|
2025-08-14 23:49:06 +08:00
|
|
|
|
更多细节请参阅:
|
|
|
|
|
- [部署 Hugo 站点](https://developers.cloudflare.com/pages/framework-guides/deploy-a-hugo-site/#deploy-with-cloudflare-pages)
|
|
|
|
|
- [语言支持与工具](https://developers.cloudflare.com/pages/platform/language-support-and-tools/)
|
2024-04-21 18:46:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Netlify
|
|
|
|
|
|
2025-08-14 23:49:06 +08:00
|
|
|
|
1. 将代码推送到 Git 仓库(GitHub/GitLab 等)
|
|
|
|
|
2. 在 Netlify 中[导入项目](https://app.netlify.com/start)
|
|
|
|
|
3. 若未使用 [hextra-starter-template][hextra-starter-template],需手动配置:
|
|
|
|
|
- 构建命令设为 `hugo --gc --minify`
|
|
|
|
|
- 发布目录设为 `public`
|
|
|
|
|
- 添加环境变量 `HUGO_VERSION` 并设为 `0.147.7`,或在 `netlify.toml` 中配置
|
|
|
|
|
4. 开始部署!
|
2024-04-21 18:46:21 +08:00
|
|
|
|
|
2025-08-14 23:49:06 +08:00
|
|
|
|
详见 [Netlify 上的 Hugo](https://docs.netlify.com/integrations/frameworks/hugo/)
|
2024-04-21 18:46:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Vercel
|
|
|
|
|
|
2025-08-14 23:49:06 +08:00
|
|
|
|
1. 将代码推送到 Git 仓库(GitHub/GitLab 等)
|
|
|
|
|
2. 进入 [Vercel 控制台](https://vercel.com/dashboard) 导入 Hugo 项目
|
|
|
|
|
3. 配置项目时选择 Hugo 作为框架预设
|
|
|
|
|
4. 覆盖构建命令与安装命令:
|
|
|
|
|
1. 构建命令设为 `hugo --gc --minify`
|
|
|
|
|
2. 安装命令设为 `yum install golang`
|
2024-04-21 18:46:21 +08:00
|
|
|
|
|
2024-12-31 00:34:20 +00:00
|
|
|
|

|