1. 准备工作

2. 创建库和环境

  • 在GitHub中Create a new repository

    • 名字:yourusername.github.com
  • 创建TOKEN

    • 账户-设置-Develop Settings-Personal access tokens
    • Expiration设置永不
    • 勾选repoworkflow

    image-20241013135433025

  • 链接Token到repository

    • 打开库-设置
    • 新建密钥。名称TOKEN

    image-20241013135847601

3. 本地创建workflow

  • 在your-blog-site目录里面依次创建.github/workflows/hugo_de.yaml
 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:
      - dev

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 }} #TOKEN是刚才在库里面创建的名字,按照你的填。
              EXTERNAL_REPOSITORY: your-username/your-repository-name
              PUBLISH_BRANCH: main
              PUBLISH_DIR: ./public
              commit_message: auto deploy

这个自动程序是将你本地未部署的文件发布到dev分支,然后Action自动部署到main分支

4. 部署到GitHub

  • 在主目录下输入:
1
2
3
4
5
6
git init
git add .
git commit -m "update"
git branch -M dev
git remote add origin https://github.com/your-username/your-repository-name.git
git push -u origin dev
  • 设置GitHub Page
    • repository-setting-general-Default branch:main
    • repository-setting-page-Deploy from a branch:main