HUGO

  • 新闻
  • 文档
  • 主题
  • 社区
  • GitHub
gohugoio Star
  • 关于
    • 本节内容
    • 简介
    • 特性
    • 隐私
    • 安全
    • 许可
  • 安装
    • 本节内容
    • macOS
    • Linux
    • Windows
    • BSD
  • 开始
    • 本节内容
    • 快速入门
    • 基本用法
    • 目录结构
    • 配置
    • 配置标记
    • 配置构建
    • 术语表
    • 外部学习资源
  • 快速参考
    • 本节内容
    • 表情符号
    • 函数
    • 方法
    • 页面集合
  • 内容管理
    • 本节内容
    • 组织
    • 页面包
    • 内容格式
    • 前言
    • 构建选项
    • 页面资源
    • 图像处理
    • 简码
    • 相关内容
    • 章节
    • 内容类型
    • 原型
    • 分类
    • 摘要
    • 链接和交叉引用
    • URL 管理
    • 菜单
    • 评论
    • 多语言
    • Markdown 属性
    • 语法高亮
    • 图表
    • 数学
    • 数据源
    • 内容适配器
  • 模板
    • 本节内容
    • 简介
    • 模板类型
    • 查找顺序
    • 基础模板
    • 主页模板
    • 单页模板
    • 章节模板
    • 分类模板
    • 术语模板
    • 局部模板
    • 内容视图模板
    • 简码模板
    • 站点地图模板
    • RSS 模板
    • 404 模板
    • robots.txt 模板
    • 菜单
    • 分页
    • 嵌入式模板
    • 自定义输出格式
  • 函数
    • 本节内容
    • cast
    • collections
    • compare
    • crypto
    • css
    • data
    • debug
    • diagrams
    • encoding
    • fmt
    • global
    • go template
    • hash
    • hugo
    • images
    • inflect
    • js
    • lang
    • math
    • openapi3
    • os
    • partials
    • path
    • reflect
    • resources
    • safe
    • strings
    • templates
    • time
    • transform
    • urls
  • 方法
    • 本节内容
    • Duration
    • 菜单
    • 菜单项
    • 页面
    • 分页器
    • 页面集
    • 资源
    • 简码
    • 站点
    • 分类
    • 时间
  • 渲染钩子
    • 本节内容
    • 简介
    • 块引用
    • 代码块
    • 标题
    • 图像
    • 链接
    • 透传
    • 表格
  • 简码
    • 本节内容
    • 评论
    • 详情
    • 图示
    • Gist
    • 高亮
    • Instagram
    • 参数
    • QR
    • Ref
    • Relref
    • Vimeo
    • X
    • YouTube
  • Hugo 模块
    • 本节内容
    • 配置 Hugo 模块
    • 使用 Hugo 模块
    • 主题组件
  • Hugo 管道
    • 本节内容
    • 简介
    • 将 Sass 转译为 CSS
    • PostCSS
    • 后处理
    • JavaScript 构建
    • 资源压缩
    • 连接资源
    • 指纹识别和 SRI 哈希
    • 从字符串创建资源
    • 从模板创建资源
  • CLI
  • 问题排查
    • 本节内容
    • 审计
    • 日志
    • 检查
    • 弃用
    • 性能
    • 常见问题
  • 开发者工具
    • 本节内容
    • 编辑器插件
    • 前端
    • 搜索
    • 迁移
    • 其他项目
  • 托管和部署
    • 本节内容
    • Hugo 部署
    • 使用 Rclone 部署
    • 使用 Rsync 部署
    • 在 21YunBox 上托管
    • 在 AWS Amplify 上托管
    • 在 Azure Static Web Apps 上托管
    • 在 Cloudflare Pages 上托管
    • 在 Firebase 上托管
    • 在 GitHub Pages 上托管
    • 在 GitLab Pages 上托管
    • 在 KeyCDN 上托管
    • 在 Netlify 上托管
    • 在 Render 上托管
  • 贡献
    • 本节内容
    • 开发
    • 文档
    • 主题
  • 维护
托管和部署

Hugo 部署

将您的网站直接部署到 Google Cloud Storage 存储桶、AWS S3 存储桶或 Azure Storage 容器。

使用 hugo deploy 命令将您的网站直接部署到 Google Cloud Storage 存储桶、AWS S3 存储桶或 Azure Storage 容器

此功能需要 Hugo extended/deploy 版本。有关详细信息,请参阅安装部分。

假设

  • 您已完成快速入门或拥有一个准备好部署并与世界分享的 Hugo 网站。
  • 您在要部署到的服务提供商(Google Cloud、AWS 或 Azure)处拥有一个帐户。
  • 您已通过身份验证。
    • Google Cloud:安装 CLI 并运行 gcloud auth login。
    • AWS:安装 CLI 并运行 aws configure。
    • Azure:安装 CLI 并运行 az login。
    • 注意:每个服务都支持其他身份验证方式,包括使用环境变量。有关更多详细信息,请参阅此处。
  • 您已创建要部署到的存储桶。如果您希望您的网站公开,请务必将存储桶配置为可公开读取的静态网站。
    • Google Cloud:创建一个存储桶 并托管一个静态网站
    • Amazon S3:创建一个存储桶 并托管一个静态网站
    • Microsoft Azure:创建一个存储容器 并托管一个静态网站

配置您的首次部署

在您站点的配置文件中,添加一个 [deployment] 部分和一个 [[deployment.targets]] 子部分。唯一必需的参数是名称和 URL

[deployment]

[[deployment.targets]]
# An arbitrary name for this target.
name = "production"

# URL specifies the Go Cloud Development Kit URL to deploy to. Examples:
URL = "<FILL ME IN>"

# Google Cloud Storage -- see https://gocloud.dev/howto/blob/#gcs
#URL = "gs://<Bucket Name>"

# Amazon Web Services S3; see https://gocloud.dev/howto/blob/#s3
#URL = "s3://<Bucket Name>?region=<AWS region>"

# For S3-compatible endpoints, see https://gocloud.dev/howto/blob/#s3-compatible
#URL = "s3://<Bucket Name>?endpoint=https://my.minio.instance&awssdk=v2&use_path_style=true&disable_https=false

# Microsoft Azure Blob Storage; see https://gocloud.dev/howto/blob/#azure
#URL = "azblob://$web"

部署

要部署到目标

hugo deploy [--target=<target name>]

部署过程会递归遍历您的本地发布目录(默认为 public),并将其同步到目标存储桶,以确保本地内容和远程内容匹配。

如果您未指定目标,Hugo 将部署到您的配置中的第一个目标。

有关更多命令行选项,请参阅 hugo help deploy 或部署命令行文档。

文件列表的工作方式

hugo deploy 首先要做的就是通过遍历本地发布目录和远程存储桶来创建本地和远程的文件列表。

对于本地和远程,文件列表都包括和排除文件,具体取决于部署目标的配置——

  • 如果配置指定了 include 模式,则默认情况下跳过所有文件,除非与该模式匹配的文件。
  • 如果配置指定了 exclude 模式,则跳过与该模式匹配的文件。

创建本地文件列表时,还会应用一些其他跳过:首先,Hugo 始终跳过名为 .DS_Store 的文件。

其次,Hugo 始终跳过本地隐藏目录(名称以句点开头的目录,例如 .git),并且不会遍历到其中,但特殊的 名为 .well-known 的隐藏目录除外,如果它存在,则会遍历它。

本地和远程文件列表如何进行比较

在第二步中,Hugo 会比较两个文件列表,以确定实际上需要在远程进行的更改。首先比较文件名;如果本地和远程文件都存在,则比较大小和 md5 校验和。任何差异都意味着该文件将被(重新)上传。

指定 --force 标志将确保所有文件都被重新上传,即使 Hugo 检测不到本地和远程之间的任何差异。

如果远程存储桶中的文件不存在于本地文件列表中,则会被删除。

如果使用排除/包含配置从文件列表生成中排除了远程文件,则比较步骤将不知道删除该文件 – 因此即使它不存在于本地,它仍将保留在远程。

如果给定了 --confirm 或 --dryRun 标志,Hugo 将显示它发现的差异,并在此处暂停或停止。

同步如何工作

Hugo 将更改列表应用于远程存储桶。缺少和/或更改的文件将被上传,而本地缺少但远程存在的文件将被删除。上传文件时,也会根据匹配器配置在远程配置其标头。

作为一项安全措施,为了防止意外,如果要删除的文件超过 256 个,Hugo 将不会从远程删除任何文件。使用 --maxDeletes 命令行标志可以覆盖此设置。

高级配置

这是一个完整的部署配置示例

[deployment]

# By default, files are uploaded in an arbitrary order.
# If you specify an `order` list, files that match regular expressions 
# in this list will be uploaded first, in the specified order.
order = [".jpg$", ".gif$"]

[[deployment.targets]]
# Define one or more targets, e.g., staging and production.
# Each target gets its own [[deployment.targets]] section.

# An arbitrary name for this target.
name = "mydeployment"
# The Go Cloud Development Kit URL to deploy to. Examples:
URL = "<FILL ME IN>"

# GCS; see https://gocloud.dev/howto/blob/#gcs
#URL = "gs://<Bucket Name>"

# S3; see https://gocloud.dev/howto/blob/#s3
# For S3-compatible endpoints, see https://gocloud.dev/howto/blob/#s3-compatible
#URL = "s3://<Bucket Name>?region=<AWS region>"

# Azure Blob Storage; see https://gocloud.dev/howto/blob/#azure
#URL = "azblob://$web"

# You can use a "prefix=" query parameter to target a subdirectory of the bucket:
#URL = "gs://<Bucket Name>?prefix=a/subdirectory/"

# If you are using a CloudFront CDN, deploy will invalidate the cache as needed.
#cloudFrontDistributionID = "<FILL ME IN>"

# Include or exclude specific files when deploying to this target:
# If exclude is non-empty, and a local or remote file's path matches it, that file is not synced.
# If include is non-empty, and a local or remote file's path does not match it, that file is not synced.
# Note: local files that don't pass the include/exclude filters are not uploaded to remote,
# and remote files that don't pass the include/exclude filters are not deleted.
# 
# The pattern syntax is documented here: https://godoc.org/github.com/gobwas/glob#Glob
# Patterns should be written with forward slashes as separator.
# 
#include = "**.html" # would only include files with ".html" suffix
#exclude = "**.{jpg, png}" # would exclude files with ".jpg" or ".png" suffix

# Map any file named "<dir>/index.html" to the remote file "<dir>/". This does
# not affect the root "index.html" file, and it does not affect matchers below.
# This works when deploying to key-value cloud storage systems, such as Amazon
# S3 (general purpose buckets, not directory buckets), Google Cloud Storage, and
# Azure Blob Storage. This makes it so the canonical URL will match the object
# key in cloud storage, except for the root index.html file.
#
#stripIndexHTML = true


#######################
[[deployment.matchers]] 
# Matchers enable special caching, content type and compression behavior for
# specified file types. You can include any number of matcher blocks; the first one
# matching a given file pattern will be used.

# See https://golang.ac.cn/pkg/regexp/syntax/ for pattern syntax.
# Pattern searching is stopped on first match.
# This is not affected by stripIndexHTML, above.
pattern = "<FILL ME IN>"

# If true, Hugo will gzip the file before uploading it to the bucket.
# With many storage services, this will save on storage and bandwidth costs
# for uncompressed file types.
#gzip = false 

# If true, Hugo always re-uploads this file even if size and md5 match.
# This is useful if Hugo isn't reliably able to determine whether to re-upload
# the file on its own.
#force = false

# Content-type header to configure for this file when served.
# By default this can be determined from the file extension.
#contentType = ""

# Cache-control header to configure for this file when served.
# The default is the empty string.
#cacheControl = ""

# Content-encoding header to configure for this file when served.
# By default, if gzip is True, this will be filled with "gzip".
#contentEncoding = ""


# Samples:

[[deployment.matchers]]
# Cache static assets for 1 year.
pattern = "^.+\\.(js|css|svg|ttf)$"
cacheControl = "max-age=31536000, no-transform, public"
gzip = true

[[deployment.matchers]]
pattern = "^.+\\.(png|jpg)$"
cacheControl = "max-age=31536000, no-transform, public"
gzip = false

[[deployment.matchers]]
# Set custom content type for /sitemap.xml
pattern = "^sitemap\\.xml$"
contentType = "application/xml"
gzip = true

[[deployment.matchers]]
pattern = "^.+\\.(html|xml|json)$"
gzip = true

另请参阅

  • 使用 Rclone 部署
  • 使用 Rsync 部署
  • 在 Azure Static Web Apps 上托管
  • 在 Firebase 上托管
  • 在 GitLab Pages 上托管

本页内容

  • 假设
  • 配置您的首次部署
  • 部署
  • 高级配置
上次更新时间:2025年1月16日: 将目录名称、文件名和文件路径格式化为代码 (8051ff818)
改进此页面
由 Hugo 作者编写
Hugo Logo
  • 提交问题
  • 获取帮助
  • @GoHugoIO
  • @spf13
  • @bepsays

Netlify badge

 

Hugo 赞助商

您的公司?
 

Hugo 徽标的版权归 Steve Francia 所有,2013–2025 年。

Hugo Gopher 基于 Renée French 的原创作品。

  • 新闻
  • 文档
  • 主题
  • 社区
  • GitHub
  • 关于
  • 安装
  • 开始
  • 快速参考
  • 内容管理
  • 模板
  • 函数
  • 方法
  • 渲染钩子
  • 简码
  • Hugo 模块
  • Hugo 管道
  • CLI
  • 问题排查
  • 开发者工具
  • 托管和部署
  • 贡献
  • 维护