博客搭建Hexo+GithubPage+GithubAction
1. 在 Github 创建两个仓库
- Github Page 仓库 仓库的命名方式采用 用户名.github.io 的方式进行命名
- 创建一个 Blog 名字的仓库(可随意命名成其他名字)
2. 在 Blog 仓库初始化 Hexo 项目
1 2 3 4
| # 初始化项目 hexo init # 本地运行hexo项目并演示 hexo s
|
1 2
| # 添加语雀同步插件 npm i --save-dev yuque-hexo
|
- 修改 package.json 语雀插件所需要的配置参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| { "name": "your hexo project", "yuqueConfig": { "postPath": "source/_posts/yuque", "cachePath": "yuque.json", "mdNameFormat": "title", "adapter": "hexo", "concurrency": 5, "baseUrl": "https://www.yuque.com/api/v2", "login": "yinzhi", "repo": "blog", "onlyPublished": false, "onlyPublic": false } }
|
参数名 |
含义 |
默认值 |
postPath |
文档同步后生成的路径 |
source/_posts/yuque |
cachePath |
文档下载缓存文件 |
yuque.json |
mdNameFormat |
文件名命名方式 (title / slug) |
title |
adapter |
文档生成格式 (hexo/markdown) |
hexo |
concurrency |
下载文章并发数 |
5 |
baseUrl |
语雀 API 地址 |
- |
login |
语雀 login (group), 也称为个人路径 |
- |
repo |
语雀仓库短名称,也称为语雀知识库路径 |
- |
onlyPublished |
只展示已经发布的文章 |
false |
onlyPublic |
只展示公开文章 |
false |
1 2 3 4 5 6 7 8 9 10 11
| { "scripts": { "sync": "yuque-hexo sync", "sync:mac": "YUQUE_TOKEN=XXXX yuque-hexo sync", "sync:win": "set YUQUE_TOKEN=XXXX && yuque-hexo sync", "clean:yuque": "yuque-hexo clean", "deploy": "npm run sync && hexo clean && hexo g -d", "deploy:mac": "npm run sync:mac && hexo clean && hexo g -d", "publish": "hexo clean && hexo g -d" } }
|
YUQUE_TOKEN 的申请地址: https://www.yuque.com/settings/tokens 创建之后填写即可
- 配置云函数用作触发 Github Action(以腾讯云为例子)
在腾讯云云函数地址中: https://console.cloud.tencent.com/scf/list 创建云函数选择 python 2.7 模版,并将下述代码添加至模版中,Github 访问的 Token,在 https://github.com/settings/apps 中选择 “Personal Access Tokens” 页面并点击”Generate new token” 生成一个新的 Token
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| import requests def main_handler(event, context): r = requests.post("https://api.github.com/repos/用户名/仓库名/dispatches", json = {"event_type": "start sync yuque"}, headers = { 'User-Agent':'curl/7.52.1', 'Content-Type': 'application/json', 'Accept': 'application/vnd.github.everest-preview+json', 'Authorization': 'token Github访问Token'}) if r.status_code == 204: return "This's OK!" else: return r.status_code
|
- 配置云函数的触发器在触发管理中点击”创建触发器” 触发方式选择 “Api 网关触发器”创建成功后会得到一个访问路径点击复制
- 配置语雀的 Webhook 通知云函数由云函数触发 Github Actions,在单个文档集合配置中点击”设置”,点击”开发者”,添加 Web Hook 将得到的访问路径填入 URL 中 ,并点亮 “发布文档”、”更新文档”、”删除文档”
3. 创建 GithubAction 自动编译 Blog 项目并自动上传更新 Github Page 项目仓库
-
4.