title: 语雀文档自动同步 Hexo 博客
date: 2022-02-18
comments: true
tags: [语雀, 博客, Serverless, 云函数 ]
categories:
- [杂七杂八 ]
本文的目的是实现
**语雀文章**编辑发布后,调用**Serverless**触发**GitHub Actions**自动同步到**github pages**前提条件:已经利用 github pages 成功搭建博客(没有的可查看我的这篇文章:《github + hexo 搭建个人博客》 ))
部署流程
配置语雀 Token
- 账户设置 => Token => 新建 Token 并配置好权限 Access Token 即为 YUQUE_TOKEN

- 创建以后, 复制下来, 配置 hexo 的时候要用到
安装 yuque-hexo 插件
官方文档:yuque-hexo
安装
npm install yuque-hexo --save
配置 hexo
{..."scripts": {"start": "yuque-hexo clean && yuque-hexo sync && hexo clean && hexo generate","build": "hexo generate","clean": "hexo clean","deploy": "hexo deploy","server": "hexo server"},..."yuqueConfig": {"postPath": "source/_posts/","cachePath": "yuque.json","mdNameFormat": "slug","adapter": "hexo","concurrency": 5,"baseUrl": "https://www.yuque.com/api/v2","login": "语雀个人路径","repo": "知识库路径","token": "语雀创建的 token","onlyPublished": true,"onlyPublic": true}}
配置 Github
在 Github 创建仓库, 并将博客的源码上传到该仓库
Github 生成 token
点击头像 > setting > Developer settings > Personal access tokens > Generate new token
只勾选
repo
- 生成了 token 之后一定要记下来,再回来就没法看了
Github Actions 配置
进入刚才 博客源码的仓库, 点击 settings

- 添加两个 Secret
GH_REF 是你博客的仓库地址: github.com/alwayscn/alwaysblog (注意去掉前面 https://)
GE_TOKEN 是刚才生成的 token - Actions 配置
1) 点击 Actions > new workflow > set up a workflow yourself
2) 编辑内容如下:
name: Blog CI/CDon: [push, repository_dispatch]jobs:blog-cicd:name: Hexo blog build & deployruns-on: ubuntu-latestenv:TZ: Asia/Shanghaisteps:- name: Checkout codesuses: actions/checkout@v2- name: Setup nodeuses: actions/setup-node@v1with:node-version: '12.x'- name: Cache node modulesuses: actions/cache@v1with:path: ~/.npmkey: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}- name: Install dependenciesrun: |npm install hexo-cli -gnpm install yuque-hexo -gnpm installyuque-hexo sync- name: Generate filesrun: hexo generate- name: Deploy blogrun: |git clone "https://${{ secrets.GH_REF }}" deploy_gitmv ./deploy_git/.git ./public/cd ./publicgit config user.name "alwayscn"git config user.email "123456@outlook.com"git add .git commit -m "GitHub Actions Auto Builder at $(date +'%Y-%m-%d %H:%M:%S')"git push --force --quiet "https://${{ secrets.GH_TOKEN }}@${{ secrets.GH_REF }}" main:main
user.name 和 user.email 根据自己的情况改一下,注意对齐
弄完之后每当 push 或 repository_dispatch 的时候都会自动的进行更新
配置 Serverless 云函数
- 登录腾讯云,搜索云函数,创建函数

“””
Author: 张小剩
Desc:
“””
import requests
def main_handler(event, context):
r = requests.post(“https://api.github.com/repos/alwayscn/blog-source/dispatches“,
json = {“event_type”: “run-it”},
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
```
post 请求里只需要改用户名和仓库名(alwayscn/blog-source)后面是固定的
那个 token 是带着的,完整的就是 ‘Authorization’: ‘token xxxxxxxxxxxxxx’(上面生成的 Github token)
- 测试执行
1)点下面那个测试,返回 This’s OK!
2)同时 github actions 也会收到指令,去执行之前在 main.yml 设定好的指令

- 触发器配置
触发管理 > 创建一个触发器 > 配置如下图所示
提交后 他会给你一个访问路径,记下来
配置语雀 webhook
- 选择 yuque-hexo 绑定的知识库 > 设置 > 消息推送(URL 填写上面 Serverless 生成的访问路径)

- 测试触发
点击测试 进入博客源码的仓库查看 Github Actions , 一段时间后就成下面那个绿色的对号了,然后去访问一下博客,看看是否正常。可以的话就证明云函数可以了
- 之后知识库进行发布、更新、删除文章等操作,只要选择
文档有较大更新,推送给关注者即可自动触发 webhook, 文章也就可以同步到自己的博客了
