所需前提条件,你需要先创建两个github仓库:

本文章使用的仓库名为构建该博客的仓库

  • 一个私有仓库: 存放hugo配置相关文件
  • 一个共有仓库: 存放hugo生成的静态页面文件

Step 0: 拉取私有仓库、公有仓库

拉取自己的私有仓库(空的,名称不限制)

1
git clone sadanspace-page-config

拉取自己的公有仓库(名称必须符合github page的标准,YOURNAME.github.io)

1
2
cd sadanspace-page-config
git clone sadanspace.github.io

当前的目录结构如下:

  • sadanspace-page-config
    • .git
    • sadanspace.github.io
      • .git

Step 1: 安装Hugo

macOS下进行安装

1
brew install hugo

检查是否安装成功(返回正常值即可)

1
hugo version

Step 2: 添加主题

下载你喜欢的主题到sadanspace-page-config目录

1
2
cd sadanspace-page-config
git submodule add https://github.com/olOwOlo/hugo-theme-even themes/even

将下载主题中的测试案例复制到当前目录

1
cp -R themes/even/exampleSite/ .

Step 3: 启动Hugo服务

运行该命令,启动Hugo服务

1
hugo server 

浏览器中访问该地址(http://localhost:1313/),查看效果

至此,本地已经可以访问该个人博客了。

Step 4: 配置静态页面生成目录

增加配置文件参数,将hugo生成的静态文件目录名设置为公有仓库名(sadanspace.github.io)

1
echo "publishDir = 'sadanspace.github.io'" > config.toml

在当前目录添加.gitignore配置(私有仓库不存放静态页面文件)

1
echo sadanspace.github.io > .gitignore"

Step 5: 提交生成静态页面,使GitHub Pages域名生效

在当前目录生成静态页面

1
hugo -D

进入公有仓库,提交生成的静态页面文件

1
2
3
4
cd sadanspace.github.io
git add .
git commit -m "first commit"
git push 

此时等几分钟,即可访问sadanspace.github.io

Step 6: 添加自动化提交脚本、以及提交私有仓库信息

退回到sadanspace-page-config目录下

1
cd ..

添加自动化更新、提交公有仓库数据脚本deploy.sh

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#! /bin/sh
# usage  `deploy.sh "msg"`

# clone publishDir repo
git clone https://github.com/sadanspace/sadanspace.github.io.git

# Build the project
hugo

# Go To publishDir
cd sadanspace.github.io

# Add changes to git.
git add .

# Commit changes.
msg="rebuilding site $(date)"
if [ -n "$*" ]; then
	msg="$*"
fi
git commit -m "$msg"

# Push source and build repos.
git push origin master

给脚本设置执行权限

1
chmod +x deploy.sh

将当前私有仓库的内容push到github中

1
2
3
git add .
git commit -m "first commit"
git push

至此完成了整个流程(不包括创建博文)

Step 7: 整个工作流

在私有仓库目录下(sadanspace-page-config)

  • 1 创建博客(使用hugo new posts/first-blog.md命令完成)
  • 2 编写博客
  • 3 本地重新生成静态页面文件(使用hugo -D命令完成)
  • 4 更新公有仓库的静态页面文件(使用./deploy.sh "second commit"命令完成)
  • 5 更新私有仓库的配置文件(此处也可写入脚本)
    1
    2
    3
    
    git add .
    git commit -m "second commit"
    git push
    
  • 6 访问博客(https://sadanspace.github.io)

参考链接