于 Netlify 部署站点

这篇文章包含了如何在 Netlify 上部署网站的分步说明。

可以通过多种方式于 Netlify 部署站点,本文只对基于文件配置的部署方式,其比控制台设置更灵活。

通过基于文件配置部署站点

创建 Netlify 配置文件

首先创建以下配置文件,然后提交到你的仓库。

netlify.toml
 1[build]
 2command = "npm ci && hugo --minify --gc -b $URL"
 3publish = "public"
 4
 5[build.environment]
 6NODE_VERSION = "19"
 7HUGO_VERSION = "0.121.2"
 8HUGO_ENABLEGITINFO = "true"
 9HUGO_ENV = "production"
10GO_VERSION = "1.20"
11
12[[headers]]
13  for = "/*"
14  [headers.values]
15    Referrer-Policy = "strict-origin-when-cross-origin"
16    X-Content-Type-Options = "nosniff"
17    X-Frame-Options = "deny"
18    X-XSS-Protection = "1; mode=block"
19
20# multilingual 404 pages, remove it on monolingual sites.
21[[redirects]]
22  from = "/en/*"
23  to = "/en/404.html"
24  status = 404
25
26[[redirects]]
27  from = "/zh-hans/*"
28  to = "/zh-hans/404.html"
29  status = 404
30
31# fallback 404 page.
32[[redirects]]
33  from = "/*"
34  to = "/404.html"
35  # to = "/en/404.html" # use this instead if defaultContentLanguageInSubdir is enabled.
36  status = 404

新增站点

  1. 登录到 Netlify 控制台。
  2. 点击 Add new site,然后选择 Import an existing project
  3. 选择仓库。
  4. 保持表单原样,因为我们已于 netlify.toml 中配置。
  5. 点击 Deploy site 以发布站点。