一、整体思路

yuque_diagram.jpg

  • 【1】利用npm插件yuque-hexo-lyrics将语雀云端的文章同步到博客源码source/_posts文件夹下。
  • 【2】再利用GitHub Actions自动化部署将GitHub私有仓库(website)的博客源文件编译成静态博客文件并部署push到静态博客仓库(wztlink1013.github.io)下。

:::danger 但是以上【1】【2】两种方式并不能解决语雀一发布文章就触发GitHub源码仓库的GitHub Actions :::

  • 【3】所以需要中间TencentCloud云函数/Aliyun云函数,云函数的作用就是,语雀文章一经正式发布就触发云函数,从而云函数再触发GitHub私有的源码仓库下的GitHub Actions达到编译静态博客的效果。

    二、网站源文件配置

    插件npm地址【暂未GitHub开源】:https://www.npmjs.com/package/yuque-hexo-lyrics

yuque-hexo-lyrics插件使用

:::info 本人基于yuque-hexo项目修改的进阶插件 :::

【1】在开发环境当中下载插件(全局安装)

  1. npm i -g yuque-hexo-lyrics

【2】相关hexo命令

  1. yuque-hexo-lyrics clean # 清缓存删除yuque文件夹 删除yuque.json文件(更新之后的插件不删除yuque.json)
  2. yuque-hexo-lyrics sync # 从云端拉取到本地

【3】特别需要注意的是:开发环境语雀Token变量的传入

语雀为了防止用户恶意多次拉取数据,出于对知识库安全性的调整,使用第三方 API 访问知识库,需要传入环境变量 YUQUE_TOKEN,如果是本地使用建议使用环境变量,也可以是终端的方式输入。
image.png
如果是在GitHub Actions里面的环境执行yuque-hexo-lyrics sync命令的时候,通过如下命令传入语雀的token

  1. YUQUE_TOKEN=${{ secrets.YUQUE_TOKEN }} yuque-hexo sync

【4】最后的配置样式(本插件可以同时下载多个知识库)

  1. "yuqueConfig_blog": {
  2. "baseUrl": "https://www.yuque.com/api/v2",
  3. "login": "wztlink1013",
  4. "repo": "blog",
  5. "postPath": "source/_posts/blog",
  6. "cachePath": "yuque_blog.json",
  7. "mdNameFormat": "slug",
  8. "onlyPublished": false,
  9. "onlyPublic": false,
  10. "adapter": "hexo",
  11. "timeout": "150s"
  12. },
  13. "yuqueConfig_essay": {
  14. "baseUrl": "https://www.yuque.com/api/v2",
  15. "login": "wztlink1013",
  16. "repo": "essay",
  17. "postPath": "source/_posts/essay",
  18. "cachePath": "yuque_essay.json",
  19. "mdNameFormat": "slug",
  20. "onlyPublished": false,
  21. "onlyPublic": false,
  22. "adapter": "hexo",
  23. "timeout": "150s"
  24. },
  25. "yuqueConfig_dsal": {
  26. "baseUrl": "https://www.yuque.com/api/v2",
  27. "login": "wztlink1013",
  28. "repo": "dsal",
  29. "postPath": "source/_posts/blog",
  30. "cachePath": "yuque_dsal.json",
  31. "mdNameFormat": "slug",
  32. "onlyPublished": false,
  33. "onlyPublic": false,
  34. "adapter": "hexo",
  35. "timeout": "150s"
  36. },
  37. "yuqueConfig_javascript": {
  38. "baseUrl": "https://www.yuque.com/api/v2",
  39. "login": "wztlink1013",
  40. "repo": "javascript",
  41. "postPath": "source/_posts/blog",
  42. "cachePath": "yuqueConfig_javascript.json",
  43. "mdNameFormat": "slug",
  44. "onlyPublished": false,
  45. "onlyPublic": false,
  46. "adapter": "hexo",
  47. "timeout": "150s"
  48. },
  49. "scripts": {
  50. "build": "hexo generate",
  51. "clean": "hexo clean",
  52. "deploy": "hexo deploy",
  53. "server": "hexo server"
  54. }

更详细使用参考官方官方仓库

GitHub Actions文件的配置

在博客源文件夹下新建如下GitHub Actions文件
.github/workflows/main.yml

文件内容配置如下

  1. # workflow name
  2. name: website to wztlink1013.github.io CI/CD
  3. on: [repository_dispatch, watch]
  4. jobs:
  5. Deploy-Pages:
  6. name: website to wztlink1013.github.io
  7. runs-on: ubuntu-latest
  8. steps:
  9. # check it to your workflow can access it
  10. # from: https://github.com/actions/checkout
  11. - name: Checkout Repository master branch
  12. uses: actions/checkout@main
  13. # from: https://github.com/actions/setup-node
  14. - name: Setup Node.js 10.x
  15. uses: actions/setup-node@main
  16. with:
  17. node-version: "10.x"
  18. - name: add Git infomations
  19. run: |
  20. git config --global user.name '${{secrets.GIT_NAME}}'
  21. git config --global user.email '${{secrets.GIT_EMAIL}}'
  22. - name: submit commit infomations
  23. run: |
  24. git log --pretty=format:"%s from Github Actions at `date +"%Y-%m-%d %H:%M:%S"`" --date=short -n 1 > commit-message.log
  25. - name: npm istall hexo-cliyuque-hexo、*
  26. env:
  27. YUQUE_TOKEN: ${{ secrets.YUQUE_TOKEN }}
  28. run: |
  29. npm install hexo-cli -g
  30. npm install yuque-hexo-lyrics -g
  31. npm install
  32. - name: generate articles
  33. run: |
  34. hexo clean
  35. yuque-hexo-lyrics clean
  36. YUQUE_TOKEN=${{ secrets.YUQUE_TOKEN }} yuque-hexo-lyrics sync
  37. hexo generate
  38. - name: push wztlink1013.github.io repository
  39. env:
  40. Github_Pages: github.com/wztlink1013/wztlink1013.github.io
  41. Github_Token: ${{ secrets.token_GithubAPI }}
  42. run: |
  43. git clone https://${Github_Token}@${Github_Pages} .github_pages
  44. mv .github_pages/.git/ ./public/
  45. cd ./public/
  46. git add .
  47. git commit -F ../commit-message.log
  48. git push --force --quiet "https://${Github_Token}@${Github_Pages}" master:master

三、Serverless云函数配置

腾讯云serverless

:::info python2.7的配置 :::

  1. # -*- coding: utf8 -*-
  2. import requests
  3. def main_handler(event, context):
  4. r = requests.post("https://api.github.com/repos/wztlink1013/website/dispatches",
  5. json={'event_type': "run-it"},
  6. headers = {"User-Agent":'curl/7.52.1',
  7. 'Content-Type': 'application/json',
  8. 'Accept': 'application/vnd.github.everest-preview+json',
  9. 'Authorization': 'token ***********'})
  10. if r.status_code == 204:
  11. return "This's OK!"
  12. else:
  13. return r.status_code

:::info 触发器的设置 ::: image.png

阿里云serverless

记录一下Hexo自动化部署过程中阿里云平台的原函数配置的python代码 :::info python2.7 :::

  1. # -*- coding: utf-8 -*-
  2. import logging
  3. import requests
  4. # To enable the initializer feature (https://help.aliyun.com/document_detail/158208.html)
  5. # please implement the initializer function as below:
  6. # def initializer(context):
  7. # logger = logging.getLogger()
  8. # logger.info('initializing')
  9. def handler(event, context):
  10. # logger = logging.getLogger()
  11. # logger.info('hello world')
  12. # return 'hello world'
  13. r = requests.post("https://api.github.com/repos/wztlink1013/blog-source/dispatches",
  14. json={'event_type': "run-it"},
  15. headers = {"User-Agent":'curl/7.52.1',
  16. 'Content-Type': 'application/json',
  17. 'Accept': 'application/vnd.github.everest-preview+json',
  18. 'Authorization': 'token f43964836a33dce244385bc303c8c20adc1bd52194'})
  19. if r.status_code == 204:
  20. return "This's OK!"
  21. else:
  22. return r.status_code
  23. # # -*- coding: utf8 -*-
  24. # import requests
  25. # def main_handler(event, context):
  26. # r = requests.post("https://api.github.com/repos/wztlink1013/blog-source/dispatches",
  27. # json={'event_type': "run-it"},
  28. # headers = {"User-Agent":'curl/7.52.1',
  29. # 'Content-Type': 'application/json',
  30. # 'Accept': 'application/vnd.github.everest-preview+json',
  31. # 'Authorization': 'token 144a87bd45e62ff1cf30dc18880787917bc7865417'})
  32. # if r.status_code == 204:
  33. # return "This's OK!"
  34. # else:
  35. # return r.status_code

:::info python3版本 :::

  1. # -*- coding: utf-8 -*-
  2. import logging
  3. import requests
  4. OK = b'ok\n'
  5. def handler(environ, start_response):
  6. status = '200 OK'
  7. response_headers = [('Content-type', 'text/plain')]
  8. sync_yuque()
  9. start_response(status, response_headers)
  10. return [OK]
  11. def sync_yuque():
  12. requests.post("https://api.github.com/repos/wztlink1013/Blog3.0/dispatches",
  13. json={'event_type': "run-it"},
  14. headers={"User-Agent":'curl/7.52.1',
  15. 'Content-Type': 'application/json',
  16. 'Accept': 'application/vnd.github.everest-preview+json',
  17. 'Authorization': 'token f43964836a33dce415385bc303c8c20adc1bd52194'})

:::info