尼采般地抒情

公告栏

此网站主题为本人手写主题,主题还在开发中……


作者:尼采般地抒情
本站主页面和blog页面暂时一样,目的是为了百度收录,百度收录之后,会将主页换回引导页~

站点信息

文章数目:195
已运行时间:
目录
  1. 一、整体思路
  2. 二、网站源文件配置
    1. yuque-hexo 插件
    2. 配置 GitHub Actions 文件
  3. 三、Serverless 云函数配置
    1. 腾讯云 serverless

尼采般地抒情

尼采般地抒情

公告栏

此网站主题为本人手写主题,主题还在开发中……


作者:尼采般地抒情
本站主页面和blog页面暂时一样,目的是为了百度收录,百度收录之后,会将主页换回引导页~

站点信息

文章数目:195
已运行时间:

一、整体思路

yuque_diagram.jpg
【1】利用npm插件yuque-hexo将语雀云端的文章同步到博客源码source/_posts/blog文件夹下。

【2】再利用GitHub Actions自动化部署将 GitHub 私有仓库(website)的博客源文件编译成静态博客文件并部署 push 到静态博客仓库(wztlink1013.github.io)下。

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

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

二、网站源文件配置

官方仓库

yuque-hexo 插件

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

npm i -g yuque-hexo

【2】相关 hexo 命令

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

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

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

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

【4】最后的配置样式

  "yuqueConfig": {
    "baseUrl": "https://www.yuque.com/api/v2",
    "login": "nicaibandishuqing",
    "repo": "blog",
    "postPath": "src/_posts/blog",
    "cachePath": "yuque.json",
    "mdNameFormat": "slug",
    "onlyPublished": false,
    "onlyPublic": true,
    "adapter": "markdown"
  },
  "scripts": {
    "clean": "hexo clean",
    "build": "hexo generate",
    "server": "hexo server",
    "deploy": "hexo deploy"
  }

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

配置 GitHub Actions 文件

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

文件内容配置如下

# workflow name
name: website to wztlink1013.github.io CI/CD

on: [repository_dispatch, watch]

jobs:
  Deploy-Pages:
    name: website to wztlink1013.github.io
    runs-on: ubuntu-latest

    steps:
      # check it to your workflow can access it
      # from: https://github.com/actions/checkout
      - name: Checkout Repository master branch
        uses: actions/checkout@main
      # from: https://github.com/actions/setup-node
      - name: Setup Node.js 10.x
        uses: actions/setup-node@main
        with:
          node-version: "10.x"

      - name: add Git infomations
        run: |
          git config --global user.name '${{secrets.GIT_NAME}}' 
          git config --global user.email '${{secrets.GIT_EMAIL}}'

      - name: submit commit infomations
        run: |
          git log --pretty=format:"%s from Github Actions at `date +"%Y-%m-%d %H:%M:%S"`" --date=short -n 1  > commit-message.log

      - name: npm istall hexo-cli、yuque-hexo、*
        env:
          YUQUE_TOKEN: ${{ secrets.YUQUE_TOKEN }}
        run: |
          npm install hexo-cli -g
          npm i -g yuque-hexo-haisawa
          npm i -g yuque-hexo-w-dsal
          npm i -g npm-wzutao
          npm install

      - name: generate articles
        run: |
          hexo clean
          yuque-hexo-haisawa clean
          yuque-hexo-w-dsal clean
          npm-wzutao clean
          YUQUE_TOKEN=${{ secrets.YUQUE_TOKEN }} yuque-hexo-haisawa sync
          YUQUE_TOKEN=${{ secrets.YUQUE_TOKEN }} npm-wzutao sync
          YUQUE_TOKEN=${{ secrets.YUQUE_TOKEN }} yuque-hexo-w-dsal sync
          cd ./src/_posts/
          mv ./dsal/* ./blog/
          cd ../../
          hexo generate

      - name: push wztlink1013.github.io repository
        env:
          Github_Pages: github.com/wztlink1013/wztlink1013.github.io
          Github_Token: ${{ secrets.token_GithubAPI }}
        run: |
          git clone https://${Github_Token}@${Github_Pages} .github_pages
          mv .github_pages/.git/ ./build/
          cd ./build/
          git add .
          git commit -F ../commit-message.log
          git push --force --quiet "https://${Github_Token}@${Github_Pages}" master:master

有个尚未清楚并解决的问题:语雀 Token 的值,通过 GitHub 仓库密匙的方式传不进去,只能以“裸露的方式传进去”
已解决:通过在仓库设置 secret,再用${{ secrets.YUQUE_TOKEN }}方式引入即可。

三、Serverless 云函数配置

腾讯云 serverless

1. python2.7的配置
```python # -*- coding: utf8 -*- import requests

def main_handler(event, context):
r = requests.post(“https://api.github.com/repos/wztlink1013/website/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 ***‘})

if r.status_code == 204:
    return "This's OK!"
else:
    return r.status_code
<div style="background: #E8F7FF;padding:10px;border: 1px solid #ABD2DA;border-radius:5px;margin-bottom:5px;">
2. 触发器的设置</div>
![image.png](https://cdn.nlark.com/yuque/0/2020/png/1484158/1594789179624-938bb8c2-ac15-4ece-a5f1-001563e4f08b.png#align=left&display=inline&height=282&margin=%5Bobject%20Object%5D&name=image.png&originHeight=583&originWidth=1250&size=50598&status=done&style=none&width=605)

### 阿里云serverless
记录一下Hexo自动化部署过程中阿里云平台的原函数配置的python代码
<div style="background: #E8F7FF;padding:10px;border: 1px solid #ABD2DA;border-radius:5px;margin-bottom:5px;">
- python2.7</div>
```python
# -*- coding: utf-8 -*-
import logging
import requests

# To enable the initializer feature (https://help.aliyun.com/document_detail/158208.html)
# please implement the initializer function as below:
# def initializer(context):
#   logger = logging.getLogger()
#   logger.info('initializing')

def handler(event, context):
#   logger = logging.getLogger()
#   logger.info('hello world')
#   return 'hello world'
    r = requests.post("https://api.github.com/repos/wztlink1013/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 f43964836a33dce4385bc303c8c20adc1bd52194'})

    if r.status_code == 204:
        return "This's OK!"
    else:
        return r.status_code
# # -*- coding: utf8 -*-
# import requests

# def main_handler(event, context):
#     r = requests.post("https://api.github.com/repos/wztlink1013/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 144a87bde62ff1cf30dc18880787917bc7865417'})

#     if r.status_code == 204:
#         return "This's OK!"
#     else:
#         return r.status_code
- python3
```python # -*- coding: utf-8 -*-

import logging
import requests
OK = b’ok\n’

def handler(environ, start_response):
status = ‘200 OK’
response_headers = [(‘Content-type’, ‘text/plain’)]
sync_yuque()
start_response(status, response_headers)
return [OK]

def sync_yuque():
requests.post(“https://api.github.com/repos/wztlink1013/Blog3.0/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 f43964836a33dce4385bc303c8c20adc1bd52194’})

<div style="background: #E8F7FF;padding:10px;border: 1px solid #ABD2DA;border-radius:5px;margin-bottom:5px;">
- 触发器配置</div>
![image.png](https://cdn.nlark.com/yuque/0/2020/png/1484158/1608448464729-f5e542f2-0a6e-4817-bd93-b8cf6f67b8fa.png#align=left&display=inline&height=95&margin=%5Bobject%20Object%5D&name=image.png&originHeight=190&originWidth=1547&size=28534&status=done&style=stroke&width=773.5)
![image.png](https://cdn.nlark.com/yuque/0/2020/png/1484158/1608448488925-30baf354-0b23-490c-8b05-4b054ca1b868.png#align=left&display=inline&height=399&margin=%5Bobject%20Object%5D&name=image.png&originHeight=798&originWidth=1045&size=52872&status=done&style=stroke&width=522.5)

### Vercel的serverless

- [https://github.com/wztlink1013/yuque-github-api](https://github.com/wztlink1013/yuque-github-api)

## 四、语雀云端的配置
在所需要拉取的仓库中webhook中填入云函数所留下的那串网址,勾选所需要触发的选项即可。
![image.png](https://cdn.nlark.com/yuque/0/2021/png/1484158/1610002554569-c1487bca-1b0f-4334-86a9-7ac062cf5a44.png#align=left&display=inline&height=291&margin=%5Bobject%20Object%5D&name=image.png&originHeight=581&originWidth=1084&size=52514&status=done&style=stroke&width=542)

## 参考

- [yuque-hexo开发仓库](https://github.com/x-cold/yuque-hexo)
- [Hexo:语雀云端写作,Github Actions持续集成](https://www.zhwei.cn/hexo-github-actions-yuque/)
- [Hexo 博客终极玩法:云端写作,自动部署](https://segmentfault.com/a/1190000017797561)
- [阿里云云函数使用用户](https://www.xiayinchang.top/post/b2362878.html)
- [[原创]自动同步语雀文档到你的hexo博客](https://bbs.pediy.com/thread-260866.htm)
- [https://github.com/Zfour/yuque_vercel_webhook_api](https://github.com/Zfour/yuque_vercel_webhook_api)

博客内容遵循: 署名-非商业性使用-禁止演绎 4.0 国际(CC BY-NC-ND 4.0)

本文永久链接: https://www.wztlink1013.com/blog/vyeuk3/

编辑: 部署: 订阅:

评论区

Twikoo 转换 utterances

最新评论

Loading...