Deploy Hexo with Github Actions

0x01 Create Github repos

Create a repo for hosting static files

Just create a public repo named yourname.github.io.

Create a repo for building static files

Create a PRIVATE repo (contains secret information) named blog-deploy.

0x02 Create a SSH key and add to Github

Create a SSH key

1
$ ssh-keygen

Save it to another place and don’t override ~/.ssh/id_rsa !!

Add your key to Github

First, copy the text in id_rsa.pub and add it to your Github.

Go to blog-deploy repo and go to Settings -> Secrets, add a new secret named ACTIONS_DEPLOY_KEY and fill it with id_rsa which you just created.

0x03 Get and modify Hexo code

Get Hexo code and push

1
2
3
4
5
$ git clone https://github.com/hexojs/hexo-starter blog-deploy
$ cd blog-deploy
$ git remote remove origin
$ git remote add origin https://github.com/yourname/blog-deploy.git
$ git push -u origin master

Modify Hexo deploy script

Scroll to the end of blog-deploy/_config.yml and edit deploy section:

1
2
3
4
deploy:
type: git
repository: git@github.com:yourname/yourname.github.io.git
branch: master

Add Github workflow

Create blog-deploy/.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
name: Deploy

on:
push:
branches:
- master

jobs:
build:
runs-on: ubuntu-18.04
if: github.event.repository.owner.id == github.event.sender.id

steps:
- name: Checkout source
uses: actions/checkout@v2
with:
ref: master

- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: '12'

- name: Setup Hexo
env:
ACTION_DEPLOY_KEY: ${{ secrets.ACTIONS_DEPLOY_KEY }}
run: |
mkdir -p ~/.ssh/
echo "$ACTION_DEPLOY_KEY" > ~/.ssh/id_rsa
cat ~/.ssh/id_rsa
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa
ssh-keyscan github.com >> ~/.ssh/known_hosts
git config --global user.email "yourname@example.com"
git config --global user.name "yourname"
npm install hexo-cli -g
npm install hexo-deployer-git --save
npm install

- name: Deploy
run: |
hexo clean
hexo deploy

0x04 Push your blog to Github

1
2
3
$ git add .
$ git commit -m "init"
$ git push

0x05 Turn on Github Pages

Go to yourname.github.io Settings -> Options -> Github Pages and turn it on.