我的期望

  1. 专注于内容创作,不需要花里胡哨的东西影响阅读和网站加载速度
  2. 使用方便,在本地 source/_posts/ 中用 markdown 语法编写文章,使用 git 同步至远端私有仓库 then 自动触发用于自动编译部署站点的 githubActions

依赖

  • nvm (optional,but recommended)
  • nrm (optional, but recommended)
  • node.js

生成站点

1
2
3
4
5
6
7
8
9
10
11
12
13
# 初始化站点源码
npm i hexo-cli -g
hexo init blog

# 本地预览站点
cd blog
hexo s

# 给站点换个主题
https://github.com/chunqiuyiyu/hexo-theme-polk

# 再次预览
hexo s

部署站点

参考了这里,以我的 github 用户名 brannua 为例

  • 在 github 上建立两个存储库:blog (private),brannua.github.io (public)
  • 私有存储库 blog 用于存储站点源码,公开仓库 brannua.github.io 用于部署站点
1
2
cd blog
npm i hexo-deployer-git --save
  • 将如下配置写入 blog/_config.yml
1
2
3
4
deploy:
type: git
repo: https://github.com/Brannua/brannua.github.io
branch: master
1
2
3
# 部署
hexo clean
hexo deploy

好了,现在你可以去趟厕所,然后喝点水,伸个懒腰

网站的线上地址:https://brannua.github.io/

备份站点源码

1
2
3
4
5
6
7
8
9
cd blog/themes/polk
rm -rf .git

cd ../..
git init
git remote add origin https://github.com/Brannua/blog.git
git add .
git commit
git push --set-upstream origin master

配置站点信息

1
2
blog/_config.yml                # 站点的配置文件
blog/themes/polk/_config.yml # 主题的配置文件

githubActions

首先,借助 ssh,将私有仓库 blog 和公有仓库 brannua.github.io 打通

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 生成公私钥匙对儿
ssh-keygen -f gh-hexo-deploy

# 给私有仓库 blog 添加私钥
# blog -> Settings -> Secrets -> Actions -> New repository secret
Name: HEXO_DEPLOY_KEY
Value: ${cat ~/.ssh/gh-hexo-deploy}
# Add secret

# 给公有仓库 brannua.github.io 添加公钥
# brannua.github.io -> Settings -> Deploy keys -> Add deploy key
Title: HEXO_DEPLOY_PUB
Value: ${cat ~/.ssh/gh-hexo-deploy.pub}
# Allow write access
# Add key

修改 blog/_config.yml

1
2
3
4
deploy:
type: git
repo: git@github.com:Brannua/brannua.github.io.git
branch: master

然后配置 githubActions 实现 push 源码就触发站点的自动编译部署

1
2
mkdir -p blog/.github/workflows
vim blog/.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
name: deploy-hexo-blog

on:
push:
branches:
- master

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

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

- name: 2. Setup Hexo...
run: |
npm i hexo-cli -g

- name: 3. Setup Git...
run: |
git config --global user.name "brannua-github-action"
git config --global user.email "374453156@qq.com"
git config --global init.defaultBranch master

- name: 4. Setup ssh...
env:
ACTION_DEPLOY_KEY: ${{ secrets.HEXO_DEPLOY_KEY }}
run: |
mkdir ~/.ssh/
echo "$ACTION_DEPLOY_KEY" > ~/.ssh/id_rsa
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa
ssh-keyscan github.com >> ~/.ssh/known_hosts

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

- name: 6. Install dependences...
run: |
npm install

- name: 7. Build and Deploy...
run: |
hexo clean
hexo deploy

现在,我的所有期望已经实现

给网站配个域名

buy 域名 from aliyun.

实名认证 -> 创建相关信息模板 -> 设置邮箱 -> 解析域名

blog/source 中执行 echo ${域名} > CNAME

push 到 github,ok

域名解析生效需要一个午觉的时间

更具体的步骤谷歌一搜一大把,不再赘述

最终成果:https://liupj.top

(完)