安装 Hugo 博客

安装 hugo:

Windows:

1
2
3
choco install hugo --confirm
或者
scoop install hugo

macOS:

1
brew install hugo

创建新站点:

1
hugo new site blog

切换到 blog

1
cd blog

在当前目录中初始化一个空的 Git 存储库

1
git init

安装博客主题:

1
2
3
4
git clone https://github.com/HEIGE-PCloud/DoIt.git themes/DoIt
或者
下面这种方法可以随时更新主题文件的修改,前提是先自己将主题文件fork到自己的库中
git submodule add https://github.com/tsaoshuang/DoIt themes/DoIt

创建第一篇博文:

1
hugo new posts/first_post.md

开启本地服务:

1
hugo server

访问 http://localhost:1313 本地查看

发布站点到 GitHub 上

  1. 生成 public 文件夹
1
hugo --theme=DoIt --baseURL="https://tsaoshuang.github.io/" --buildDrafts

或者不加参数只用 hugo 命令 这个命令将生成一个 public 文件夹,其中包含您网站的所有静态内容,可以将其部署在任何 Web 服务器上。

  1. git 操作(切换至 public 目录进行操作)
1
2
3
4
git init
git add .
git commit -m "建立hugo博客"
git remote add origin https://github.com/tsaoshuang/tsaoshuang.github.io.git

遇到的问题:

运行 git remote add origin 时出现错误: failed to push some refs to

如何修复此问题 运行以下命令:

1
git pull --rebase origin master

再运行:

1
git push -u origin master

更新博客内容的命令

新建博文

1
hugo new post/FileName.md

更新配置文件之后重新用 hugo 命令生成新文件

1
2
hugo --theme=DoIt --baseURL="https://tsaoshuang.github.io/
" --buildDrafts

或更新后只需要在 博客根目录运行

1
hogo

命令

然后切换到 public 文件夹

1
cd public

提交:

1
2
3
git add .
git commit -m "内容"
git push -u origin master

自动化部署

利用 Github Actions 实现将 hugo 站源文件自动化部署到 GitHub Pages 上

创建 Github Actions 文件

在站点源文件根目录,创建 .github/workflows/deploy.yml 文件:

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
31
32
33
# This is a basic workflow to help you get started with Actions

name: hugo-deploy-CI

# Controls when the action will run.
on: push

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# docs:https://github.com/peaceiris/actions-gh-pages
deploy:
runs-on: ubuntu-18.04
steps:
- name: Git checkout
uses: actions/checkout@v2

- name: Setup hugo
uses: peaceiris/actions-hugo@v2
with:
hugo-version: '0.81.0'
extended: true

- name: Build
run: hugo
- name: Deploy
uses: peaceiris/actions-gh-pages@v3
with:
personal_token: ${{ secrets.PERSONAL_TOKEN }}
external_repository: xcbeyond/xcbeyond.github.io # 修改为自己的地址
publish_dir: ./public
# keep_files: true
publish_branch: master
cname: blog.readfere.com # 如有独立域名,修改为自己的域名

另一个自动化脚本:

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
31
32
33
34
35
name: deploy

on:
push:
workflow_dispatch:
schedule:
# Runs everyday at 8:00 AM
- cron: "0 0 * * *"

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
submodules: true
fetch-depth: 0

- name: Setup Hugo
uses: peaceiris/actions-hugo@v2
with:
hugo-version: "latest"

- name: Build Web
run: hugo

- name: Deploy Web
uses: peaceiris/actions-gh-pages@v3
with:
PERSONAL_TOKEN: ${{ secrets.PERSONAL_TOKEN }}
EXTERNAL_REPOSITORY: pseudoyu/pseudoyu.github.io
PUBLISH_BRANCH: master
PUBLISH_DIR: ./public
commit_message: ${{ github.event.head_commit.message }}

其中,on: push 表示提交时触发 Action,自动编译并部署到 GitHub Pages。 以后提交个人站点内容时,会自动编译并部署。

绑定域名

除了在域名管理中绑定域名 cname 之外,还要在 GitHub setting 中的 pages 设置中绑定并验证域名 txt,这样域名才会生效 还有要在 Hugo 仓库的根目录新建一个 CNAME 文件里面写上你要绑定的域名或者子域名不带协议头的那种!