博客小更新记录

之前blog一直是通过hexo+GitHub page部署的,那是还没有GitHub actions,每次操作都无比复杂,最近有时间就做个小更新,记录下更改点。

Github Actions

这个肯定是这次的重中之重!其实流程也是很简单。

Repos

推荐使用两个repo的模式,一个blog的repo,设置成private,原因是仓库里可能会包含一些系统的secretKey等信息,不适合对外暴露,而另一个就是GitHub Page的repo。

Keys

简要流程如下,我们假设博客原始仓库为A,Page仓库为B

  • 生成ssh key
1
$ ssh-keygen -f github-deploy-key

然后一路按几次回车就完成生成工作了,最后在目录下会生成 github-deploy-key 私钥 和 github-deploy-key.pub 公钥两个文件。

  • 配置公钥

    复制 github-deploy-key.pub 文件里的内容,在B仓库 Settings -> Deploy keys -> Add deploy key 页面上添加 SSH 公钥:

    • Title 填写 HEXO_DEPLOY_PUB
    • Key 填写 github-deploy-key.pub 文件内容
    • 勾选 Allow write access 选项

    image-20220514094036069

  • 配置私钥

复制 github-deploy-key 文件里的内容,在A仓库 Settings -> Secrets ->Actions -> Add a new secret 页面上添加 SSH 私钥:

  • Name 填写 HEXO_DEPLOY_PRI。
  • Value 填写 github-deploy-key 文件内容。

image-20220514094119400

Actions

A仓库根目录下创建 .github/workflows/deploy.yml 文件,最后的目录结构应该是这样:

1
2
3
4
A (repository)
└── .github
└── workflows
└── deploy.yml

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
name: Blog deploy

on:
push:
paths-ignore:
- 'source/_drafts/**'
- '.github/**'
jobs:
build:
runs-on: ubuntu-latest
name: A job to deploy blog.
steps:
- name: Checkout
uses: actions/checkout@v1
with:
submodules: true

- name: Cache node modules
uses: actions/cache@v1
id: cache
with:
path: node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: Install Dependencies
if: steps.cache.outputs.cache-hit != 'true'
run: npm ci

# Deploy hexo blog website.
- name: Deploy
id: deploy
uses: sma11black/[email protected]
with:
deploy_key: ${{ secrets.HEXO_DEPLOY_PRI }}
commit_msg: ${{ github.event.head_commit.message }}ion)
- name: Get the output
run: |
echo "${{ steps.deploy.outputs.notify }}"

Hexo deploy

在A仓库的源码中,对hexo进行配置,添加deploy支持,修改_config.yml

1
2
3
4
5
6
# Deployment
## Docs: https://hexo.io/docs/one-command-deployment
deploy:
type: git
repo: your B repo ssh git address
branch: master

Hexo theme 更新

这里仍然使用[NEXT](NexT - Theme for Hexo (theme-next.js.org))。但是修改了引入方式,从原来的clone改为yarn add hexo-theme-next,相关主题配置修改通过Hexo提供的Alternate Theme Config方式。整个repo的目录结构如下:

1
2
3
A (repository)
└── _config.yml -- hexo 主配置文件
└── _config.next.yml -- next 主题配置文件

分析统计配置

阅读量统计原来是通过leancloud统计的,但是由于现在需要实名认证以及备案,就切换到google firebase了,这样统计和分析都使用了Google的服务,只是对于国内的访问无法统计到,这个后续再看怎么解决吧。

图床优化

之前图床一直使用的是AWS S3,然后由于其只提供HTTP方式,和主站HTTPS的方式可能会存在访问异常问题,遂通过AWS Cloudfront进行一次中转,这样就可以通过HTTPS的方式访问S3服务了。