自动部署HEXO


利用语雀实现 HEXO 在线编辑并自动部署

部署 hexo

暂缺

在 github 上

创建一个私密仓库

编辑 GitHub Actions 脚本

添加.github/workflows/main.yml

内容如下

# workflow name
name: Deploy To Github Pages
# 当有 push 到仓库和外部触发的时候就运行
on: [push, repository_dispatch]
# YUQUE_TOKEN
# Github_SSH_PRIVATE_KEY
jobs:
  deploy:
    name: Deploy Hexo Public To Pages
    runs-on: ubuntu-latest
    env:

      TZ: Asia/Shanghai
    steps:
    # check it to your workflow can access it
    # from: https://github.com/actions/checkout
    - name: Checkout Repository master branch
      uses: actions/checkout@master
    # from: https://github.com/actions/setup-node
    - name: Setup Node.js 14.x
      uses: actions/setup-node@master
      with:
        node-version: "14.x"
    # from https://github.com/x-cold/yuque-hexo
    - name: Setup Hexo Dependencies and Generate Public Files
      env:
        YUQUE_TOKEN: ${{ secrets.YUQUE_TOKEN }}
      run: |
        npm install
        npm install hexo-cli -g
        npm install yuque-hexo -g
        yuque-hexo clean
        yuque-hexo sync
        hexo generate

    # from https://github.com/peaceiris/actions-gh-pages
    - name: Deploy
      uses: peaceiris/actions-gh-pages@v3
      with:
          deploy_key: ${{ secrets.ACTION_DEPLOY_KEY }}
          external_repository: 用户名/博客仓库名
          publish_branch: master  #博客部署分支
          publish_dir: ./public
          commit_message: auto deploy

现在还不能运行需要配置隐藏内容

YUQUE_TOKEN 从语雀获取的 TOKEN,语雀上点击个人头像 –> 设置 –> Token 即可获取,要在 Hexo 源仓库的 Settings–>Secrets 中进行添加,对重要信息进行保密
ACTION_DEPLOY_KEY 是 SSH-Key 密钥中的私钥,需要在 Secrets 中进行添加,公钥(.pub)已经存储在 Github 中
IMG_20210603_144736.jpg

在腾讯云函数

新建函数选择自定义 Python

函数代码

# -*- coding: utf8 -*-
import requests

def main_handler(event, context):
    r = requests.post("https://api.github.com/repos/github用户名/仓库名称/dispatches",
    json = {"event_type": "start"},
    headers = {"User-Agent":'curl/7.52.1',
              'Content-Type': 'application/json',
              'Accept': 'application/vnd.github.everest-preview+json',
              'Authorization': 'token 修改githubtoken'})

    if r.status_code == 204:
        return "This's OK!"
    else:
        return r.status_code

HEXO 源码

先安装 yuque-hexo

npm install yuque-hexo –save

修改 package.json 文件
可以参考

{
  "name": "hexo-site",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "build": "hexo generate",
    "server": "hexo server",
    "clean": "hexo clean",
    "clean:yuque": "yuque-hexo clean",
    "deploy": "yuque-hexo sync && hexo deploy",
    "publish": "npm run clean && npm run deploy",
    "dev": "hexo s",
    "sync": "yuque-hexo sync",
    "reset": "npm run clean:yuque && npm run sync"
  },
  "yuqueConfig": {
    "baseUrl": "https://www.yuque.com/api/v2",
    "login": "语雀个人路径",
    "repo": "知识库路径",
    "mdNameFormat": "title",
    "postPath": "source/_posts/yuque",
    "cachePath": "yuque.json",
    "adapter": "hexo",
    "token": "语雀token",
    "onlyPublished": true,
    "onlyPublic": true
  },
  "hexo": {
    "version": "5.4.0"
  },
  "dependencies": {
    "hexo": "^5.0.0",
    "hexo-deployer-git": "^3.0.0",
    "hexo-filter-github-emojis": "^3.0.4",
    "hexo-generator-archive": "^1.0.0",
    "hexo-generator-category": "^1.0.0",
    "hexo-generator-feed": "^3.0.0",
    "hexo-generator-index": "^2.0.0",
    "hexo-generator-search": "^2.4.2",
    "hexo-generator-tag": "^1.0.0",
    "hexo-permalink-pinyin": "^1.1.0",
    "hexo-renderer-ejs": "^1.0.0",
    "hexo-renderer-marked": "^4.0.0",
    "hexo-renderer-stylus": "^2.0.0",
    "hexo-server": "^2.0.0",
    "hexo-theme-landscape": "^0.0.3",
    "hexo-wordcount": "^6.0.1"
  },
  "devDependencies": {
    "yuque-hexo": "^1.7.0"
  }
}

文章作者: 紫鼠
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 紫鼠 !
  目录