Featured image of post Hugo|Github Pages|Stack主题|搭建记录

Hugo|Github Pages|Stack主题|搭建记录

基于 macOS Monterey,Hugo 框架和 GitHub Pages 网页寄存服务,博客主题为 Stack,搭建过程尽量简单,同时减少后续隐患

预警:非常啰嗦

前期准备

安装 Hugo

1
2
3
4
5
6
7
# 安装
brew install hugo

# 检查安装是否成功
hugo version
# 返回⬇️
hugo v0.104.1+extended darwin/arm64 BuildDate=unknown

安装 Git

Git 下载、安装程序,按默认选项安装即可

设置你的用户名和邮件地址

1
2
3
# 填你的 GitHub ID 和注册邮箱
git config --global user.name "<username>"
git config --global user.email [email protected]

GitHub

https://raw.githubusercontent.com/Falasool/blog-pic-bed/main/blog/new-public-repo-1.png

https://raw.githubusercontent.com/Falasool/blog-pic-bed/main/blog/image-20221115113949496.png

创建公开仓库

存放编译好的静态页面

创建私密仓库

存放源码

给两个仓库绑定 SSH key

  • 生成 SSH key
1
ssh-keygen -t rsa -b 4096 -C "[email protected]"

这行命令 means:生成 SSH 密钥文件 - 密钥类型(rsa)- 密钥长度(4096) - 注释([email protected]

.ssh 文件夹里找到生成的 SSH

  • 添加公钥:把 public key id_rsa_hugo_deploy.pub 添加到 .github.io 仓库:
1
2
cd .ssh
cat id_rsa_hugo_deploy.pub # 显示文件内容
  • 添加私钥:Private Key 添加到 private仓库: 这里 Secrets 变量名要一定是: ACTIONS_DEPLOY_KEY

绑定 Token

  • 在 Account- Settings- Developer Settings- Personal access tokens-Tokens(classic) 里创建 Token,记得勾选 repo 和 workflow,并复制保存。我的 Token: (隐藏)

博客发布-设置token2

  • 在私有仓库 hugo-scources-settings-secrets-Actions 创建名为PERSONAL_TOKEN,值为刚才的 Token 的Secret,这样就完成了 Token 的设置

搭建步骤

克隆 private 仓库到本地

新建 Hugo 站点

在本地 hugo-sources 文件夹中执行 ⬇️,在当前目录生成 Hugo 源码

1
hugo new site . --force

找到 blog 文件夹,打开会发现结构如下

1
2
3
4
5
6
7
8
9
blog
├── archetypes # 配置文件,决定创建新博文时的自带格式
│   └── default.md
├── config.toml # 网站配置文件
├── content # 网站内容,全部是 markdown 格式
├── data
├── layouts # 网站模板,优先于 themes/<THEME>/layouts
├── static # 静态资源(css/js/fonts/media等)
└── themes

Hugo 主题

有了主题,内容才会渲染出来

在 github.com 搜索 hugo theme ,或在 Hugo Themes 选择你喜欢的主题。我选择 stack ,功能丰富,支持多种评论系统,使用人数多bug好解决

  • fork 主题仓库,并使用 git submodule 方式添加到 themes 里,这样方便后续对主题的修改进行单独维护
1
2
3
# 在hugo-sources目录下
git init 
git submodule add https://github.com/Falasool/hugo-theme-stack themes/hugo-theme-stack
  • 更新主题:
1
2
3
4
# 初始化
git submodule update --init --recursive
# 同步主题仓库的最新修改
git submodule update --remote
  • 删除根目录下的config.toml 文件,把 ./themes/hugo-theme-stack/exampleSite/ 里的 config.yaml 和 content 转移到根目录下,在 config.yaml 中进行站点细节配置
  • 删掉 examplesite/content/post/rich-content/index.md,因为Twitter 等等被墙应用的API 中国大陆无法访问 Issus #[email protected]/hugo-theme-stack
  • 本地预览
1
2
hugo server -D
# 在浏览器打开localhost:1313
  • 预览满意了 Control+C 终止预览

推送到 GitHub

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

GitHub Actions 配置

  • 在 GitHub 的 Private 仓库页面,切换到 Actions tab,点击set up a workflow yourself,粘贴以下文件,并 commit

配置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
34
35
36
37
38
39
40
name: Deploy Hugo Site to Github Pages on Main Branch

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

jobs:
  build-deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/[email protected]
        with:
          submodules: true
          fetch-depth: 0

      - name: Setup Hugo
        uses: peaceiris/[email protected]
        with:
          hugo-version: '0.104.1' # 查看你的版本把它替换掉,防止兼容问题
          extended: true

      - name: Build
        run: hugo --minify

      - name: Deploy
        uses: peaceiris/[email protected]
        with:
          PERSONAL_TOKEN: ${{ secrets.PERSONAL_TOKEN }} # 这里的 ACTIONS_DEPLOY_KEY 则是上面设置 Private Key的变量名
          external_repository: Falasool/Falasool.github.io # Pages 远程仓库 
          publish_dir: "./public"
          keep_files: false # remove existing files
          publish_branch: main  # deploying branch
          commit_message: ${{ github.event.head_commit.message }}
          cname: anodicseal.xyz # 这里要改成你的个人域名,没有就把这行注释掉
  • 在本地 hugo-sources 目录下执行 git pull origin main,查看本地文件夹,出现了 .github/workflows/main.yml 文件
  • 检查流水线是否被正常触发(查看流水线日志)

测试自动发布系统

博客搭建完成

使用步骤

1
2
3
4
5
6
7
8
9
# 新建文章
hugo new post/<title>.md
# 打开<title.md>,编辑并保存
# 本地预览
hugo server -D
# 没问题了就push到远程仓库,Actions会自动部署
git add .
git commit -m "test"
git push origin main

拓展

Github Actions 实现的三种方案:

  • github_token
  • deploy_key
  • personal_token

适用情况和差异请参考 官方文档

相关参考

Hugo 搭建博客实践

Git Submodule 命令与理解

Github Pages + Hugo 搭建个人博客

自动发布系统

GitHub Actions 入门教程

Workflow syntax for GitHub Actions

CI/CD是什麼?一篇認識CI/CD工具及優勢,將日常瑣事自動化

Hugo + GitHub Action,搭建你的博客自动发布系统

Hugo 搭配 Github Actions 實現 Github Pages 的自動部署

利用GitHub Action实现Hugo博客在GitHub Pages自动部署

GitHubAction + Hugo 自动构建发布个人博客

使用 Github Actions 自动发布 hugo 站点

***

折腾Hugo | GitHub Pages | Github Actions自动构建发布免费个人网站

将Hugo静态网站部署到Github Pages

用 Hugo 配合 GitHub Actions 和 GitHub Pages 搭建博客

comments powered by Disqus
未经允许不得随意转载
Built with Hugo
Theme Stack designed by Jimmy