道破——GitHub Pages

1. 快速上手

  1. 登录GitHub创建一个新项目

项目名为[username].github.io,其中[username]换成自己的GitHub用户名。

权限设为Public

  1. 克隆到本地,并创建一个HTML
1
2
3
git clone https://github.com/username/username.github.io.git
cd username.github.io
echo "Hello World" > index.html
  1. 将新内容提交到远程仓库
1
2
3
git add .
git commit -m "Initial commit"
git push -u origin main
  1. 访问https://username.github.io

2. Hugo集成

使用Hugo和GitHub Actions持续集成部署到GitHub Pages。

  1. 创建.github/workflows/gh-pages.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
name: GitHub Pages

on:
  push:
    branches:
      - main  # Set a branch name to trigger deployment
  pull_request:

jobs:
  deploy:
    runs-on: ubuntu-20.04
    permissions:
      contents: write
    concurrency:
      group: ${{ github.workflow }}-${{ github.ref }}
    steps:
      - uses: actions/checkout@v3
        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@v2
        with:
          hugo-version: '0.101.0'
          extended: true

      - name: Build
        run: hugo --minify

      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3
        if: ${{ github.ref == 'refs/heads/main' }} 
        with:
          deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }} # secret 中设置好私钥
          external_repository: username/username.github.io  # Page 仓库
          publish_branch: main  # Page 仓库的分支
          publish_dir: ./public # 静态网页路径
  1. 生成秘钥对,用于设置ACTIONS_DEPLOY_KEY。
1
ssh-keygen -t rsa -b 4096 -C "$(git config user.email)" -f gh-pages
  1. 将公钥添加到username/username.github.io仓库中Settings->Deploy Keys->Add depoly key, 命名为ACTIONS_DEPLOY_KEY_PUB。

  2. 将私钥添加到Hugo仓库中Settings->Secrets->Actions->New repository secret, 命名为ACTIONS_DEPLOY_KEY。

  3. 查看Hugo仓库->Actions持续集成是否成功,查看username/username.github.io仓库是否部署成功, 访问https://username.github.io/查看是否正常显示,正常则部署完毕。

详细了解Hugo On GitHub Actions持续集成请参考:

Host on GitHub

actions-hugo