首先,建立一个以 username.github.io 为名的 Github 仓库。或者,在此基础上新建一个普通的项目,然后可以挂到 username.github.io 域名的后面。

本地流程化

将此目录初始化成 git 仓库:

1
2
3
git init
git add .
git commit -m "first commit"

与新建的仓库建立连接:

1
2
git branch -M main
git remote add origin [email protected]:xxx/xxx.github.io.git

运行 hugoMarkdownHTML 内容转换为静态网站文件,将生成的文件放在 public 文件夹中,进入 public 目录,提交静态网站文件。

将所有步骤打包到 scripts\depoly_local.bat

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@echo off

:: 运行 Hugo 生成静态文件
echo Running Hugo...
hugo

:: 切换到 public 文件夹
cd public || (echo Error: 'public' directory not found! && exit /b 1)

:: 初始化 Git 仓库(如果尚未初始化)
if not exist .git (
    git init
    git remote add origin https://github.com/Sakuracedia/Sakuracedia.github.io.git
)

:: 提交静态文件
echo Committing generated static files...
git add .
git commit -m "Update generated static files"

:: 推送静态文件
git push -u origin main

echo Deployment completed!

稍等一两分钟,即可打开网址查看。

Github Action 自动发布

添加 .gitignore 文件。这里主要就是把 public 目录给排除掉,这个会在Action的时候自动生成。

 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
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so

# Folders
_obj
_test

# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out

*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*

_testmain.go

*.exe
*.test

/public
.DS_Store
.hugo_build.lock
resources/_gen/

Github Action 自动发布其实就是在网页端进行流程化。

  1. 添加路径 .github/workflows/deploy.yml
  2. 配置 deploy.yml 文件,注意修改hugo-version和分支名。
  3. 查看Action运行状态。

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Sample workflow for building and deploying a Hugo site to GitHub Pages
name: Deploy Hugo site to Pages

on:
  # Runs on pushes targeting the default branch
  push:
    branches:
      - main

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
  contents: read
  pages: write
  id-token: write

# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
concurrency:
  group: "pages"
  cancel-in-progress: false

# Default to bash
defaults:
  run:
    shell: bash

jobs:
  # Build job
  build:
    runs-on: ubuntu-latest
    env:
      HUGO_VERSION: 0.143.1
    steps:
      - name: Install Hugo CLI
        run: |
          wget -O ${{ runner.temp }}/hugo.deb https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_linux-amd64.deb \
          && sudo dpkg -i ${{ runner.temp }}/hugo.deb          
      - name: Install Dart Sass
        run: sudo snap install dart-sass
      - name: Checkout
        uses: actions/checkout@v4
        with:
          submodules: recursive
          fetch-depth: 0
      - name: Setup Pages
        id: pages
        uses: actions/configure-pages@v5
      - name: Install Node.js dependencies
        run: "[[ -f package-lock.json || -f npm-shrinkwrap.json ]] && npm ci || true"
      - name: Build with Hugo
        env:
          HUGO_CACHEDIR: ${{ runner.temp }}/hugo_cache
          HUGO_ENVIRONMENT: production
          TZ: America/Los_Angeles
        run: |
          hugo \
            --gc \
            --minify \
            --baseURL "${{ steps.pages.outputs.base_url }}/"          
      - name: Upload artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: ./public

  # Deployment job
  deploy:
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4

Github Action 直接推送源代码即可,集成到 scripts\depoly_action.bat

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
@echo off

:: 初始化 Git 仓库(如果尚未初始化)
if not exist .git (
    git init
    git remote add origin https://github.com/Sakuracedia/Blog.git
)

echo Committing source codes...
git add .
git commit -m "Update source codes"
git push -u origin main

echo Deployment completed!