三、语雀写作,hexo自动部署服务器

前文

  1. hexo安装部署到云服务器
  2. Travis-CI监听github仓库变动部署到云服务器

思路

  • 在语雀中新建一个知识库,知识库里配置Web Hook。通过配置 Web Hook,可以方便地让开发者订阅到此知识库下所有文档、评论等数据变更。
  • 在腾讯云上创建 **云函数 ,**通过语雀的Web Hook 去触发云函数
  • 云函数中配置了Travis-CI的token,repoid,强制让travis-ci重新restart一次

在本地git bash拉取hexoBlog-auto

git clone git@github.com:DallenCat/hexoBlog-auto.git

安装yuque-hexo,配置package.json

npm install yuque-hexo
  • 修改 package.json
"scripts": {
        "build": "hexo generate",
        "clean": "hexo clean",
        "server": "hexo server",
        "sync": "yuque-hexo sync",
        "clean:yuque": "yuque-hexo clean",
        "deploy": "npm run sync && hexo clean && hexo g -d"
},
"yuqueConfig": {
        "postPath": "source/_posts/yuque",
        "cachePath": "yuque.json",
        "mdNameFormat": "title",
        "adapter": "hexo",
        "concurrency": 5,
        "baseUrl": "https://www.yuque.com/api/v2",
        "login": "wozaimiaozheli",    //语雀账号
        "repo": "szls99",            //语雀知识库id
        "onlyPublished": false,
        "token": "xxxxxxxxxxxxxxxxxx"     //语雀token
}
  • yuqueConfiglogin在语雀右上角头像,账户设置,账户管理,个人路径

image.png
image.png


  • repo 在 知识库,点进你的知识库,上面url中账号后面的英文加数字就是你的repo

image.png
image.png
我的repo就是szls99

  • token 在右上角头像,账户设置中,Token,新建Token,创建,复制

image.png

image.png

image.png

  • 附上完整的package.json
{
    "name": "hexo-site",
    "version": "0.0.0",
    "private": true,
    "scripts": {
        "build": "hexo generate",
        "clean": "hexo clean",
        "server": "hexo server",
        "sync": "yuque-hexo sync",
        "clean:yuque": "yuque-hexo clean",
        "deploy": "npm run sync && hexo clean && hexo g -d"
    },
    "hexo": {
        "version": "4.2.0"
    },
    "dependencies": {
        "hexo": "^4.0.0",
        "hexo-deployer-git": "^2.1.0",
        "hexo-generator-archive": "^1.0.0",
        "hexo-generator-category": "^1.0.0",
        "hexo-generator-index": "^1.0.0",
        "hexo-generator-tag": "^1.0.0",
        "hexo-helper-live2d": "^3.1.1",
        "hexo-renderer-ejs": "^1.0.0",
        "hexo-renderer-marked": "^2.0.0",
        "hexo-renderer-stylus": "^1.1.0",
        "hexo-server": "^1.0.0",
        "live2d-widget-model-koharu": "^1.0.5"
    },
    "yuqueConfig": {
        "postPath": "source/_posts/yuque",
        "cachePath": "yuque.json",
        "mdNameFormat": "title",
        "adapter": "hexo",
        "concurrency": 5,
        "baseUrl": "https://www.yuque.com/api/v2",
        "login": "wozaimiaozheli",
        "repo": "szls99",
        "onlyPublished": false,
        "token": "xxxxxxxxxxxxxxxxxxxxx"
    }
}

创建腾讯云serverless云函数解析

  • 新建云函数,模板选择Php7

image.png


image.png

  • serverless 函数:
<?php
function main_handler($event, $context) {
    // 解析语雀post的数据
    $update_title = '';
    if($event->body){
        $yuque_data= json_decode($event->body);
        $update_title .= $yuque_data->data->title;
    }
    // default params
    $repos = 'xxxx';  // 你的仓库id 或 slug
    $token = 'xxxxxx'; // 你的登录token
    $message = date("Y/m/d").':yuque update:'.$update_title;
    $branch = 'master';
    // post params
    $queryString = $event->queryString;
    $q_token = $queryString->token ? $queryString->token : $token;
    $q_repos = $queryString->repos ? $queryString->repos : $repos;
    $q_message = $queryString->message ? $queryString->message : $message;
    $q_branch = $queryString->branch ? $queryString->branch : 'master';
    echo($q_token);
    echo('===');
    echo ($q_repos);
    echo ('===');
    echo ($q_message);
    echo ('===');
    echo ($q_branch);
    echo ('===');
    //request travis ci
    $res_info = triggerTravisCI($q_repos, $q_token, $q_message, $q_branch);

    $res_code = 0;
    $res_message = '未知';
    if($res_info['http_code']){
        $res_code = $res_info['http_code'];
        switch($res_info['http_code']){
            case 200:
            case 202:
                $res_message = 'success';
            break;
            default:
                $res_message = 'faild';
            break;
        }
    }
    $res = array(
        'status'=>$res_code,
        'message'=>$res_message
    );
    return $res;
}

/*
* @description  travis api , trigger a build
* @param $repos string 仓库ID、slug
* @param $token string 登录验证token
* @param $message string 触发信息
* @param $branch string 分支
* @return $info array 回包信息
*/
function triggerTravisCI ($repos, $token, $message='yuque update', $branch='master') {
    //初始化
    $curl = curl_init();
    //设置抓取的url
    curl_setopt($curl, CURLOPT_URL, 'https://api.travis-ci.org/repo/'.$repos.'/requests');
    //设置获取的信息以文件流的形式返回,而不是直接输出。
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    //设置post方式提交
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
    //设置post数据
    $post_data = json_encode(array(
        "request"=> array(
            "message"=>$message,
            "branch"=>$branch
        )
    ));
    $header = array(
      'Content-Type: application/json',
      'Travis-API-Version: 3',
      'Authorization:token '.$token,
      'Content-Length:' . strlen($post_data)
    );
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
    //执行命令
    $data = curl_exec($curl);
    $info = curl_getinfo($curl);
    //关闭URL请求
    curl_close($curl);
    return $info;
}
?>
  • 触发方式选择API网管触发器,得到访问路径,例如:

https://service-qomnxox2-1300907076.gz.apigw.tencentcs.com/release/yuque-to-hexo

  • $token 获取

在Travis-CI官网右上角头像点击Settings,再点击Settings,复制API authentication
image.png


image.png


  • $repos 获取,27946093就是我的$repos

使用postman 获取 api 接口 https://api.travis-ci.org/owner/你的github账号/repos
Headers中添加3个参数
Authorization : token 你的travis-ci token
Travis-API-Version : 3
User-Agent : API Explorer
如下图我传的参数
image.png

设置语雀知识库web hook

  • 打开语雀知识库列表,选择要设置的知识库,设置,开发者,URL填写云函数创建的API网关触发器的访问路径

image.png

image.png

发布或更新一篇文章查看测试

  • 云函数的运行日志

image.png

  • Travis-CI的运行日志

image.png


祝大家江山一片绿,build success

我的博客地址

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值