使用netlify部署hugo注意事项

nextlify部署hugo blog

参考:

hugo官方: https://gohugo.io/hosting-and-deployment/hosting-on-netlify

nextlify官方: https://docs.netlify.com/integrations/frameworks/hugo/

步骤

  1. 上传项目到github仓库, 该仓库可以是私人仓库

  2. hugo的主题必须是git submodule形式, 如果自定义主题, 上传到gihub仓库上, 以子模块方式引入

  3. .gitignore里排除 public文件夹

  4. 在项目根目录下新建文件netlify.toml

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    
    [build]
    publish = "public"
    command = "hugo --gc --minify"
    
    [context.production.environment]
    HUGO_VERSION = "0.65.3"
    HUGO_ENV = "production"
    HUGO_ENABLEGITINFO = "true"
    
    [context.split1]
    command = "hugo --gc --minify --enableGitInfo"
    
    [context.split1.environment]
    HUGO_VERSION = "0.65.3"
    HUGO_ENV = "production"
    
    [context.deploy-preview]
    command = "hugo --gc --minify --buildFuture -b $DEPLOY_PRIME_URL"
    
    [context.deploy-preview.environment]
    HUGO_VERSION = "0.65.3"
    
    [context.branch-deploy]
    command = "hugo --gc --minify -b $DEPLOY_PRIME_URL"
    
    [context.branch-deploy.environment]
    HUGO_VERSION = "0.65.3"
    
    [context.next.environment]
    HUGO_ENABLEGITINFO = "true"
    
  5. 注册Nextlify,可以用github账号授权

  6. 选择 “New site from Git”——“Github”——选择自己hugo博客源码对应的仓库

  7. Deploy之前可以先设置下, "site overview"--"site setting"--"change site name", 不改的话会默认分配https://xxx-xxx-xxx.netlify.app一堆符号的二级域名

  8. Deploy status badge徽章可以放到项目的readme上, 可以看到最新部署状态

  9. Deploy

  10. 每次仓库更新, netlify就会自动部署

可选: 绑定域名

  1. Site settings -- Domain management--Custom domains--Add domain alias--填写域名--verify--Yes,add domain
  2. 填写后到DNS 解析提供商里面,将 CNAME 记录值更改为 Netlify 给你的二级域名
  3. https: 隔一段时间后点Verify DNS configuration

可能的问题

  1. 报错:
1
2
3
4
5
Production: master@HEAD Failed
failed during stage 'building site': Build script returned non-zero exit code: 255
# 解决方法: stackoverflow上找到类似答案,需要修改Netlify控制台的环境变量与实际Hugo版本一致。
# 技术问题千万别用百度,垃圾csdn
# hugo -v

“setting”——“Build&deploy”——“Environment”——"Environment variables"——“Edit variables”,左边填“HUGO_VERSION”,右边填“v?.?.?”

注:版本号以实际为准

  1. 报错

    1
    2
    
    The build image for this site uses Ubuntu 16.04 Xenial Xerus, which is no longer supported.
    # ubuntu 版本过低
    

    "Deploys"--"Delploy settings"--"Build image selection"--"Edit settings"--选最新的

    重新部署

build with Hugo, theme Stack, visits 0