1.安装Hugo

brew install hugo
  1. 新建博客管理文件夹

    hugo new site blog_name --format yaml
    
  2. 设置博客主题

    我用的是Hugo PaperMod

    $ git init
    $ git submodule add --depth=1 https://github.com/adityatelange/hugo-PaperMod.git themes/PaperMod
    

    修改博客配置应用主题,通过修改根目录下的hugo.yml文件

    baseURL: ""
    languageCode: zh-cn
    title: Howe's Blog
    theme: PaperMod
    

    这样博客的初步框架就打好了,可以先本地预览一下。命令行输入以下指令之后,就可以本地预览了,打开浏览器访问//localhost:1313/

    hugo server
    

2.配置PaperMod

主题配置可以查看github介绍,参考example设置。

3.新建文章

hugo new post/first.md

文章头需要设置为

date: '自动生成'
draft: false
title: 'first'
Tags: ['tags','example]

4.设置GitHub Actions

通过设置github workflow,这样就可以通过自动化发布自己的博文啦!因为我的仓库和github pages仓库不是同一个,所有需要将生成出来的静态文件,push到另一仓库,我的yml配置如下,在仓库根目录下创建 .github/workflow/hugo-deploy.yml

name: GitHub Pages

on:
  push:
    branches:
      - main  # Set a branch to deploy
  pull_request:

jobs:
  deploy:
    runs-on: ubuntu-22.04
    concurrency:
      group: ${{ github.workflow }}-${{ github.ref }}
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true  # Fetch Hugo themes (true OR recursive)
          fetch-depth: 0    # Fetch all history for .GitInfo and .Lastmod

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

      - name: Build
        run: hugo --minify

      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3
        with:
          PERSONAL_TOKEN: ${{ secrets.PERSONAL_TOKEN }}
          publish_dir: ./public
          EXTERNAL_REPOSITORY: your own github page repo
          PUBLISH_BRANCH: master
          commit_message: ${{ github.event.head_commit.message }}

设置完成之后,每次push就会出发github action发布博文了。

[!NOTE]

PERSONAL_TOKEN,在仓库 Settings->Secrets and variables->Actions下设置

5.配置自定义域名

配置自定义域名,可以先在阿里云上购买自己喜欢的域名,通过审核后,先在阿里云域名管理平台上设置域名解析 ,添加两个CNAME, @www其他参数默认即可。

回到Github Pages仓库,在Settings-> PagesCustom domain 填入自己的自定义域名。

由于hugo生成静态网页,是不带CNAME文件的,所以自动推送的时候会把Github pages仓库里面新生成的CNAME文件覆盖掉。 在Github workflows配置文件中添加如下内容即可自动创建CNAME文件:

# 创建 CNAME, 用于自定义域名
- uses: "finnp/create-file-action@master"
  env:
    FILE_NAME: "./public/CNAME"
    FILE_DATA: "howeblogs.top" #自定义域名