本文最后更新于:2025年4月24日 下午
                  
                
              
            
            
              
              创建两个Github仓库
在 Github 网站建立两个仓库,名称任意。其中一个仓库用于存放 Blog
文件的源码(以下称
仓库A),另一个用于存放生成以后的静态内容,即
public 目录下的内容(以下称 仓库B)。
使用 Github
Action + Vercel 为 Hexo 的 Pandoc 渲染器提供支持 | Hui-Shao's
Blog
创建个人访问令牌(Personal
Access Token)
因为我们需要在 Hexo 项目仓库A 执行 Github
Actions 向 username.github.io
仓库B推送代码,由于 Github 权限限制,我们需要在 GitHub
账户中创建一个具有足够权限的个人访问令牌(Personal Access
Token,简称 PAT)。这个令牌需要有足够的权限来修改仓库。然后在
secrects 里面添加 PERSONAL_TOKEN
利用
GitHub Actions 实现自动化部署 Hexo 到 Github Pages
(hackergavin.com)
创建 Github Actions 脚本
- 在你的 Hexo 项目仓库A根目录下创建一个
.github/workflows 文件夹(如果尚未存在)。
- 在该文件夹内创建一个新的 YAML 文件(例如
hexo-deploy.yml),用于定义 GitHub Actions
工作流。
- 复制如下配置到 YAML 文件
| 12
 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: Deploy Hexo to GitHub Pages
 on:
 push:
 branches:
 - master
 
 jobs:
 deploy:
 runs-on: ubuntu-latest
 
 steps:
 - name: Checkout blog source
 uses: actions/checkout@v2
 with:
 path: blog
 
 - name: Set up Node.js
 uses: actions/setup-node@v1
 with:
 node-version: "16"
 
 - name: Cache dependencies
 uses: actions/cache@v3
 with:
 path: node_modules
 key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
 restore-keys: |
 ${{ runner.os }}-node-
 
 - name: Install dependencies
 run: npm install
 working-directory: ./blog
 
 - name: Install Hexo CLI
 run: npm install -g hexo-cli
 working-directory: ./blog
 
 - name: Setup pandoc
 uses: nikeee/setup-pandoc@v1
 
 - name: Generate static pages
 run: hexo generate
 working-directory: ./blog
 
 - name: Deploy to GitHub Pages
 uses: peaceiris/actions-gh-pages@v3
 with:
 personal_token: ${{ secrets.PERSONAL_TOKEN }}
 publish_dir: ./blog/public
 external_repository: andyppang/andyppang.github.io
 publish_branch: master
 
 
 | 
大功告成
- 提交本地的 Hexo 项目代码到对应 Github 仓库即可触发 Github
Actions 工作流实现自动部署,然后访问你的
username.github.io ! 
- 或者直接在github仓库A的source/_posts文件夹下添加文件,开始写博客,写完就会自动部署到仓库B