於 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 以發佈站點。
razonyang
2024年7月26日 星期五 2023年4月15日 星期六