0%

使用VScode编写MarkDown,通过Github Actions自动部署hexo博客到GithubPages

这个博客兜兜转转多少次了。

说明

在Github上创建两个仓库,一个私库存放博客源码,一个用户名.github.io存放生成的public文件。

创建ssh密钥对

1
$ ssh-keygen -f github-deploy-key
  • 打开 用户名.github.io 的仓库,点击 setting / deploy key ,然后点击 add new key,名称填写 HEXO_DEPLOY_PUB ,值填写上一步生成的公钥(.pub文件)里面的内容,然后勾选 allow write,提交。
  • 打开私库,添加 setting / secrets / add new key,名称为 HEXO_DEPLOY_PRI,值为生成ssh密钥对之私钥内容,提交。

创建 github action workflow

在私库中点击 Actions,新建一个workflow。

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
name: HEXO CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [10.x]
steps:
- uses: actions/checkout@v1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Configuration environment
env:
HEXO_DEPLOY_PRI: ${{secrets.HEXO_DEPLOY_PRI}}
run: |
mkdir -p ~/.ssh/
echo "$HEXO_DEPLOY_PRI" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan github.com >> ~/.ssh/known_hosts
git config --global user.name "设置用户名"
git config --global user.email "设置邮箱"
- name: Install dependencies
run: |
npm i -g hexo-cli
npm install hexo-deployer-git --save
npm i
- name: Generate static blog file
run: |
hexo generate
- name: Deploy to github page
run: |
hexo deploy

修改私库中部署地址

1
2
3
4
deploy:
type: git
repo: [email protected]:username/username.github.io.git
branch: master

绑定域名

在私库博客源文件source文件夹中新建一个CNAME的文件,内容为你要绑定的域名,然后在域名服务商处解析域名cname到username.github.io。

部署

在私库源文件source/_posts中添加md格式的博文,然后push,即可自动部署成功。