Hugo 部署
使用 hugo deploy
命令将您的网站直接部署到 Google Cloud Storage 存储桶、AWS S3 存储桶或 Azure Storage 容器
假设
- 您已完成快速入门或拥有一个准备好部署并与世界分享的 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 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 会比较两个文件列表,以确定实际上需要在远程进行的更改。首先比较文件名;如果本地和远程文件都存在,则比较大小和 md5 校验和。任何差异都意味着该文件将被(重新)上传。
指定 --force
标志将确保所有文件都被重新上传,即使 Hugo 检测不到本地和远程之间的任何差异。
如果远程存储桶中的文件不存在于本地文件列表中,则会被删除。
如果给定了 --confirm
或 --dryRun
标志,Hugo 将显示它发现的差异,并在此处暂停或停止。
同步如何工作
Hugo 将更改列表应用于远程存储桶。缺少和/或更改的文件将被上传,而本地缺少但远程存在的文件将被删除。上传文件时,也会根据匹配器配置在远程配置其标头。
高级配置
这是一个完整的部署配置示例
[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