Hugo 部署
使用 hugo deploy
命令可将您的站点直接部署到 Google Cloud Storage 存储桶、AWS S3 存储桶或 Azure 存储容器。
假设
- 您已完成 快速入门 ,或者已经拥有一个您准备部署并与世界分享的 Hugo 网站。
- 您拥有您要部署到的服务提供商( Google Cloud 、 AWS 或 Azure )的帐户。
- 您已完成身份验证。
- Google Cloud: 安装 CLI 并运行
gcloud auth login
。 - AWS: 安装 CLI 并运行
aws configure
。 - Azure: 安装 CLI 并运行
az login
。 - 注意:每项服务都支持身份验证的替代方法,包括使用环境变量。更多详情请参阅 此处 。
- Google Cloud: 安装 CLI 并运行
- 您已创建要部署到的存储桶。如果您希望您的网站公开访问,请确保将存储桶配置为可公开读取的静态网站。
配置您的第一次部署
在站点的配置文件中,添加一个 [deployment]
部分和一个 [[deployment.targets]]
子部分。唯一必需的参数是名称和 URL:
[deployment]
[[deployment.targets]]
# 此目标的任意名称。 {#an-arbitrary-name-for-this-target}
name = "production"
# URL 指定要部署到的 Go Cloud Development Kit URL。示例: {#url-specifies-the-go-cloud-development-kit-url-to-deploy-to-examples}
URL = "<FILL ME IN>"
# Google Cloud Storage -- 请参阅 https://gocloud.dev/howto/blob/#gcs {#google-cloud-storage-see-https-gocloud-dev-howto-blob-gcs}
# URL = "gs://<Bucket Name>" {#url-gs-bucket-name}
# Amazon Web Services S3;请参阅 https://gocloud.dev/howto/blob/#s3 {#amazon-web-services-s3-see-https-gocloud-dev-howto-blob-s3}
# URL = "s3://<Bucket Name>?region=<AWS region>" {#url-s3-bucket-name-region-aws-region}
# 对于与 S3 兼容的端点,请参阅 https://gocloud.dev/howto/blob/#s3-compatible {#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 {#url-s3-bucket-name-endpoint-https-my-minio-instance-awssdk-v2-use-path-style-true-disable-https-false}
# Microsoft Azure Blob Storage;请参阅 https://gocloud.dev/howto/blob/#azure {#microsoft-azure-blob-storage-see-https-gocloud-dev-howto-blob-azure}
# URL = "azblob://$web" {#url-azblob-web}
部署
要部署到目标:
hugo deploy [--target=<target name>]
部署过程会递归遍历您的本地发布目录(默认为 public
)并将其同步到目标存储桶,以确保本地和远程内容匹配。
如果您没有指定目标,Hugo 将部署到您配置中的第一个目标。
请参阅 hugo help deploy
或 部署命令行文档 以了解更多命令行选项。
文件列表的工作原理
hugo deploy
首先要做的是通过遍历本地发布目录和远程存储桶来创建本地和远程的文件列表。
对于本地和远程,文件列表都会根据 部署目标的配置 包含和排除文件——
- 如果配置指定了
include
模式,则默认情况下会跳过所有文件,除了与该模式匹配的文件。 - 如果配置指定了
exclude
模式,则会跳过与该模式匹配的文件。
如何比较本地和远程文件列表
在第二步中,Hugo 会比较这两个文件列表以确定远程实际上需要进行哪些更改。首先比较文件名;如果本地和远程文件都存在,则比较大小和 md5 校验和。任何差异都意味着该文件将被(重新)上传。
指定 --force
标志将确保即使 Hugo 无法检测到本地和远程之间的任何差异,所有文件也会重新上传。
如果本地文件列表中不存在远程文件,则会从远程存储桶中删除这些文件。
如果提供了 --confirm
或 --dryRun
标志 ,Hugo 将显示它找到的差异,然后在此处暂停或停止。
同步的工作原理
Hugo 将更改列表应用于远程存储桶。将上传丢失和/或更改的文件,并将本地不存在但远程存在的文件删除。上传文件时,还会根据匹配器配置在远程配置其标头。
高级配置
这是一个完整的示例部署配置:
[deployment]
# 默认情况下,文件以任意顺序上传。{#by-default-files-are-uploaded-in-an-arbitrary-order}
# 如果你指定了一个 `order` 列表,则列表中与正则表达式匹配的文件将首先按指定的顺序上传。{#if-you-specify-an-order-list-files-that-match-regular-expressions}
order = [".jpg$", ".gif$"]
[[deployment.targets]]
# 定义一个或多个目标,例如,预发布和生产环境。{#define-one-or-more-targets-e-g-staging-and-production}
# 每个目标都有其自己的 [[deployment.targets]] 部分。{#each-target-gets-its-own-deployment-targets-section}
# 此目标的任意名称。{#an-arbitrary-name-for-this-target}
name = "mydeployment"
# 要部署到的 Go Cloud Development Kit URL。示例:{#the-go-cloud-development-kit-url-to-deploy-to-examples}
URL = "<FILL ME IN>"
# GCS;请参阅 https://gocloud.dev/howto/blob/#gcs {#gcs-see-https-gocloud-dev-howto-blob-gcs}
# URL = "gs://<Bucket Name>" {#url-gs-bucket-name}
# S3;请参阅 https://gocloud.dev/howto/blob/#s3 {#s3-see-https-gocloud-dev-howto-blob-s3}
# 对于与 S3 兼容的端点,请参阅 https://gocloud.dev/howto/blob/#s3-compatible {#for-s3-compatible-endpoints-see-https-gocloud-dev-howto-blob-s3-compatible}
# URL = "s3://<Bucket Name>?region=<AWS region>" {#url-s3-bucket-name-region-aws-region}
# Azure Blob Storage;请参阅 https://gocloud.dev/howto/blob/#azure {#azure-blob-storage-see-https-gocloud-dev-howto-blob-azure}
# URL = "azblob://$web" {#url-azblob-web}
# 你可以使用 "prefix=" 查询参数来定位存储桶的子文件夹:{#you-can-use-a-prefix-query-parameter-to-target-a-subfolder-of-the-bucket}
# URL = "gs://<Bucket Name>?prefix=a/subfolder/" {#url-gs-bucket-name-prefix-a-subfolder}
# 如果你正在使用 CloudFront CDN,部署将根据需要使缓存失效。{#if-you-are-using-a-cloudfront-cdn-deploy-will-invalidate-the-cache-as-needed}
# cloudFrontDistributionID = "<FILL ME IN>" {#cloudfrontdistributionid-fill-me-in}
# 部署到此目标时包含或排除特定文件:{#include-or-exclude-specific-files-when-deploying-to-this-target}
# 如果 exclude 非空,并且本地或远程文件的路径与之匹配,则该文件不会同步。{#if-exclude-is-non-empty-and-a-local-or-remote-file-s-path-matches-it-that-file-is-not-synced}
# 如果 include 非空,并且本地或远程文件的路径与之不匹配,则该文件不会同步。{#if-include-is-non-empty-and-a-local-or-remote-file-s-path-does-not-match-it-that-file-is-not-synced}
# 注意:不通过 include/exclude 过滤器过滤的本地文件不会上传到远程,{#note-local-files-that-don-t-pass-the-include-exclude-filters-are-not-uploaded-to-remote}
# 并且不通过 include/exclude 过滤器过滤的远程文件不会被删除。{#and-remote-files-that-don-t-pass-the-include-exclude-filters-are-not-deleted}
# {#}
# 此处记录了模式语法:https://godoc.org/github.com/gobwas/glob#Glob {#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" # 只包含后缀为 ".html" 的文件 {#include-html-would-only-include-files-with-html-suffix}
# exclude = "**.{jpg, png}" # 排除后缀为 ".jpg" 或 ".png" 的文件 {#exclude-jpg-png-would-exclude-files-with-jpg-or-png-suffix}
# 将任何名为 "<dir>/index.html" 的文件映射到远程文件 "<dir>/”。这不会{#map-any-file-named-dir-index-html-to-the-remote-file-dir-this-does}
# 影响根 "index.html" 文件,也不会影响下面的匹配器。{#not-affect-the-root-index-html-file-and-it-does-not-affect-matchers-below}
# 这在部署到键值云存储系统时有效,例如 Amazon {#this-works-when-deploying-to-key-value-cloud-storage-systems-such-as-amazon}
# S3(通用存储桶,而不是目录存储桶)、Google Cloud Storage 和 {#s3-general-purpose-buckets-not-directory-buckets-google-cloud-storage-and}
# Azure Blob Storage。这使得规范 URL 将与云存储中的对象键匹配,根 index.html 文件除外。{#azure-blob-storage-this-makes-it-so-the-canonical-url-will-match-the-object}
# {#}
# stripIndexHTML = true {#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}
# 请参阅 https://golang.org/pkg/regexp/syntax/ 以了解模式语法。{#see-https-golang-org-pkg-regexp-syntax-for-pattern-syntax}
# 模式搜索在第一次匹配时停止。{#pattern-searching-is-stopped-on-first-match}
# 这不受上面 stripIndexHTML 的影响。{#this-is-not-affected-by-stripindexhtml-above}
pattern = "<FILL ME IN>"
# 如果为 true,Hugo 将在将文件上传到存储桶之前对其进行 gzip 压缩。{#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}
# gzip = false {#gzip-false}
# 如果为 true,即使大小和 md5 匹配,Hugo 也会始终重新上传此文件。{#if-true-hugo-always-re-uploads-this-file-even-if-size-and-md5-match}
# 如果 Hugo 本身无法可靠地确定是否需要重新上传文件,这将很有用。{#this-is-useful-if-hugo-isn-t-reliably-able-to-determine-whether-to-re-upload}
# force = false {#force-false}
# 为此文件在提供服务时配置的内容类型标头。{#content-type-header-to-configure-for-this-file-when-served}
# 默认情况下,这可以从文件扩展名确定。{#by-default-this-can-be-determined-from-the-file-extension}
# contentType = "" {#contenttype}
# 为此文件在提供服务时配置的缓存控制标头。{#cache-control-header-to-configure-for-this-file-when-served}
# 默认值为空字符串。{#the-default-is-the-empty-string}
# cacheControl = "" {#cachecontrol}
# 为此文件在提供服务时配置的内容编码标头。{#content-encoding-header-to-configure-for-this-file-when-served}
# 默认情况下,如果 gzip 为 True,则将填充为“gzip”。{#by-default-if-gzip-is-true-this-will-be-filled-with-gzip}
# contentEncoding = "" {#contentencoding}
# 示例:{#samples}
[[deployment.matchers]]
# 将静态资源缓存 1 年。{#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]]
# 设置 /sitemap.xml 的自定义内容类型{#set-custom-content-type-for-sitemap-xml}
pattern = "^sitemap\\.xml$"
contentType = "application/xml"
gzip = true
[[deployment.matchers]]
pattern = "^.+\\.(html|xml|json)$"
gzip = true