准备
- 需要两个 github 仓库:
- 一个用于发布页面: XXXXXX.github.io
- 一个用于放源码: hexo-source (可设为隐私仓库)
1. 创建 hexo-source 仓库
- 在hexo的根目录下( ~/hexo)运行:
cd hexo
git init
git add .
git commit -m "first commit"
git remote add origin https://github.com/XXXXXX/hexo-source.git
git push -u origin master
2. Github 仓库设置
- 首先在命令行运行 ssh-keygen 生成一对密钥。
$ ssh-keygen
- 打开 XXXXXX.github.io 仓库设置,在
Deploye keys
选项中,添加公钥~/.ssh/id_rsa.pub
的内容。 - 打开 hexo-source 仓库设置,在
Deploye keys
选项中,添加私钥~/.ssh/id_rsa
的内容,名称设为GITHUB_ACTION
。
3. 添加 Github Actions 配置文件
内容如下:
name: Hexo Auto-Deploy
on: [push]
jobs:
build:
name: Hexo Auto-Deploy by GitHub Actions
runs-on: ubuntu-latest
steps:
- name: 1. git checkout...
uses: actions/checkout@v1
- name: 2. setup nodejs...
uses: actions/setup-node@v1
- name: 3. install hexo...
run: |
npm install hexo-cli -g
npm install
- name: 4. hexo generate public files...
run: |
hexo clean
hexo g
- name: 5. hexo deploy ...
run: |
mkdir -p ~/.ssh/
echo "${{ secrets.GITHUB_ACTION }}" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan github.com >> ~/.ssh/known_hosts
git config --global user.name "XXXXXX"
git config --global user.email "XXXXXX@XXXXXX.com"
git config --global core.quotepath false
hexo d
以上。