个人博客搭建与部署:Hugo + Stack + Github

本地搭建

github下载hugo_extended和stack,添加hugo.exe所在目录至环境变量(或者把hugo.exe复制到hugo-dev目录下)

将stack放在theme目录下,用其exampleSite中的content和hugo.yaml替换hugo主目录中的content和hugo.toml

删掉content/post中rich-content所有内容,修改配置中的baseURL为https://xujiaz2000.github.io/,并修改其他配置,修改content/page下的各个目录中index.md里的title为中文

注:hugo主目录配置优先级高于theme目录下的配置(可参考theme下的文件放置位置在主目录配置)

1
2
3
4
# hugo基本命令
hugo new site ./hugo-dev
hugo server -D
hugo new content post/HelloWorld/index.md

Github部署

新建github仓库(Public,不要README)

1
xujiaz2000.github.io

删除hugo-dev中的public,重新生成静态页面;进入public,推送远端

1
2
3
4
5
6
7
8
hugo
cd public
git init
git add .
git commit -am "first commit to build the blog"
git branch -M main # 强制更名当前分支为main
git remote add origin https://github.com/xujiaz2000/xujiaz2000.github.io.git
git push -u origin main # 推送并建立关联,之后只需git push

xujiaz2000.github.io仓库 - Settings - Pages - Branch选择main后save

Github自动部署

新建github仓库(Private,不要README)

在hugo-dev主目录新建.gitignore并添加

1
2
3
4
public
resources
.hugo build.lock
hugo.exe

推送远端

1
2
3
4
5
6
7
echo "# hugo-dev" >> README.md
git init
git add .
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/xujiaz2000/hugo-dev.git
git push -u origin main

personal profile -> Setttings -> Developer Settings -> Personal access tokens -> Tokens (classic)

Note: TOKEN, Expiration: No expiration, Select scopes: repo & workflow

记下token,进入hugo-dev -> Settings -> Secrets and variables -> Actions -> New repository secret -> TOKEN

回到本地hugo-dev目录,新建.github/workflows/hugo_deploy.yaml,参考5配置,修改TOKEN(#{{secrets.TOKEN}})和REPOSITORY(xujiaz2000/xujiaz2000.github.io)

 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
name: deploy

# 代码提交到main分支时触发github action
on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
        - name: Checkout
          uses: actions/checkout@v4
          with:
              fetch-depth: 0

        - name: Setup Hugo
          uses: peaceiris/actions-hugo@v3
          with:
              hugo-version: "latest"
              extended: true

        - name: Build Web
          run: hugo -D

        - name: Deploy Web
          uses: peaceiris/actions-gh-pages@v4
          with:
              PERSONAL_TOKEN: ${{ secrets.你的token变量名 }}
              EXTERNAL_REPOSITORY: 你的github名/你的仓库名
              PUBLISH_BRANCH: main
              PUBLISH_DIR: ./public
              commit_message: auto deploy
1
2
PERSONAL_TOKEN: ${{ secrets.TOKEN }}
EXTERNAL_REPOSITORY: xujiaz2000/xujiaz2000.github.io

之后只需在hugo_dev中push即可

1
2
3
git add .
git commit -am "update"
git push

绑定域名

华为云 -> 域名注册 -> 使用指南

搜索域名注册 -> 网站解析 -> 管理解析 -> 添加记录集

1
2
3
记录类型:CNAME
主机记录:www / 空
记录值:xujiaz2000.github.io

github.io仓库 -> Setting -> Pages -> Custom domain

1
shccc.top

Ref

  1. Hugo中文文档

  2. Github Page部署指南

  3. Stack配置文档

  4. 【雷】Hugo + Github免费搭建博客,并实现自动化部署

  5. letere的教程

comments powered by Disqus