基础

原理:用户提交仓库,触发main分支修改,触发Github Action运行,Public文件写入Github Pages仓库。触发Vercel部署,CDN缓存刷新。

创建源代码仓库

这个仓库就是存放我们Blog源代码文件的。当然,如果不介意的话,你也可以创建一个公开的仓库。

本教程以私密仓库为例讲解。 名称随意。

通过git clone将仓库克隆到此电脑中。

如果出现了权限问题……

在终端运行以下指令生成密钥。

1
ssh-keygen -t rsa

用记事本打开存储在C:\Users\xxx\.ssh\id_rsa.pub文件,并复制。

在Github设置,SSH and GPG keys,点击New SSH key

Title随意,Key type不动,将复制的内容粘贴到Key中。

编写Action脚本

  1. 将源代码复制到刚才克隆的文件夹中

    提醒

    请注意你所使用的主题文件夹内是否有.git这个文件夹,若有,请删除,否则会对后面的提交有影响。

  2. 在根目录下新建.github文件夹,在其中再次新建workflows文件夹。在文件内新建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
    name: 自动部署
    on:
    push:
    branches:
    - main
    release:
    types:
    - published

    jobs:
    deploy:
    runs-on: ubuntu-latest
    steps:
    - name: 检查分支
    uses: actions/checkout@v2
    with:
    ref: main

    - name: 安装 Node
    uses: actions/setup-node@v1
    with:
    node-version: "20.x"

    - name: 安装 Hexo
    run: |
    export TZ='Asia/Shanghai'
    npm install hexo-cli -g

    - name: 缓存 Hexo
    uses: actions/cache@v1
    id: cache
    with:
    path: node_modules
    key: ${{runner.OS}}-${{hashFiles('**/package-lock.json')}}

    - name: 安装依赖
    run: |
    npm install

    - name: 生成静态文件
    run: |
    hexo clean
    hexo generate

    - name: 部署
    run: |
    cd ./public
    git init
    git config --global user.name '${{ secrets.GITHUBUSERNAME }}'
    git config --global user.email '${{ secrets.GITHUBEMAIL }}'
    git add .
    git commit -m "${{ github.event.head_commit.message }} $(date +"%Z %Y-%m-%d %A %H:%M:%S") Updated By Github Actions"
    git push --force --quiet "https://${{ secrets.GITHUBUSERNAME }}:${{ secrets.GITHUBTOKEN }}@github.com/${{ secrets.GITHUBUSERNAME }}/${{ secrets.GITHUBUSERNAME }}.github.io.git" master:main

    提醒

    第22行的node-version建议与本地的版本一致,请在终端运行node -v查看版本号。

    如:终端输出v20.12.2,那么在文件里写20.x

  3. 在Github仓库设置中添加环境变量。

    变量名 内容
    GITHUBEMAIL 你的Github账号邮箱。
    GITHUBTOKEN Developer Settings,Personal access tokens (classic)中新建填写。
    GITHUBUSERNAME 你的Github用户名。

    提醒

    创建Token中务必勾选以下两个权限。

    且Token只显示一次。

提交

  1. 添加屏蔽项。

    像node_modules等文件夹在Action部署时会自己生成,我们也没必要在上传。

    在根目录新建.gitignore文件,写入以下内容。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    .DS_Store
    Thumbs.db
    db.json
    *.log
    node_modules/
    public/
    .deploy*/
    .deploy_git*/
    .idea
    themes/solitude/.git
  2. 在终端运行以下指令,提交。

    1
    2
    3
    4
    git checkout -b main
    git add .
    git commit -m "Action"
    git push origin main

    如果你使用Vscode,你也可以直接在Code里打开此文件夹,提交

CDN

查看

FAQ

Git提交时报HTTPS443等类似问题

玄学问题,建议使用🪜🧪。

更多问题待补充……