详细步骤请参考这篇blog
以下内容为纯执行步骤:
第1步:在Github创建一个仓库
仓库名任意,只要不是.github.io即可(例如就叫做blog)。这个仓库可以是私有的。
第2步:关联本地Hugo项目
cd <YOUR HUGO PROJECT>
git remote add origin git@github.com:USERNAME/blog.git
第3步:在Github创建一个.github.io仓库
就是你的Github用户名。这个仓库用来存放最终发布的blog内容。
第4步:关联已安装的theme
如果Hugo项目安装了一些theme,需要git submodule add这些theme。以ananke为例,命令如下:
git submodule add https://github.com/theNewDynamic/gohugo-theme-ananke.git themes/ananke
第5步:关联Hugo的public文件夹
如果Hugo项目路径下已存在public文件夹,请直接删除。不用担心。
git submodule add -b master git@github.com:<USERNAME>/<USERNAME>.github.io.git public
第6步:生成并发布内容
hugo
cd public
git status
git add .
git commit -m "first commit"
git push -u orgin master
注意:本地网站是关联的blog仓库,本地网站下的public文件夹是以子模块的形式关联的github.io仓库,他们是相对独立的。
自动化部署脚本
将以上步骤整理成自动执行脚本deploy.sh。脚本commit信息会使用当前时间。将deploy.sh脚本放到Hugo项目路径下,写完blog后直接双击运行即可自动部署发布。
#!/bin/bash
echo -e "\033[0;32mDeploying updates to GitHub...\033[0m"
# Build the project.
hugo # if using a theme, replace with `hugo -t <YOURTHEME>`
# Go To Public folder
cd public
# Add changes to git.
git add .
# Commit changes.
msg="rebuilding site `date`"
if [ $# -eq 1 ]
then msg="$1"
fi
git commit -m "$msg"
# Push source and build repos.
git push origin master
# Come Back up to the Project Root
cd ..
给网站添加评论(未验证)
Hugo支持Disqus评论,只需要在配置文件config.toml中设置disqusShortname = “disqusShortname"即可。如果没有disqusShortname,需要到Disqus官网注册一个账号。注册后添加一个网站,添加时会让填一个shortname,Disqus会生成一个shortname.disqus.com的网址。另外还需要把config.toml中的baseURL地址,也就是公网访问地址添加到Diqus的信任网址中,类似于互相添加信任。