经过gitee管理hexo管理发布内容

想法

  • 最原始的办法是经过ftp或者winscp这样的工具能实现内容上传,可是不够简化,首先你要登陆服务器,而后找到相应文件夹进行更新。
  • 第二种方法是先更新gitee仓库,而后经过ssh登陆服务器,而后从仓库pull下全部代码
  • 第三种方法是不须要gitee仓库,直接在服务器建一个空仓库,而后把本地发布文件push到服务器仓库上

上面3种方法,第1种最原始,第2种比较繁琐,第3种简单可操做,可是第3种有一个问题,就是代码管理所有在服务器上,可视性比较差,能不能经过gitee管理代码,包括原始文件,而后一旦用户push,经过钩子函数,触发服务器主动拉取,这样,全部操做都简化为了本地的一个git push指令。说干就干,问了一圈度娘,果真高手在民间,几个关键步骤都有相应的帖子介绍。php

第一步 创建hexo空仓库

本地把项目文件通通push上去,除了node_module文件夹。这样,项目文件也不会丢失,便于之后复用。html

第二步 创建服务器到hexo仓库私钥通讯

1. 建立ssh链接密钥

参看 https://gitee.com/help/articles/4181#article-header0node

 
none
ssh-keygen -t rsa -C "xxxxx@xxxxx.com"  
# Generating public/private rsa key pair...

产生两个文件:id_rsa id_rsa.pub
切换到 ~/.ssh目录,cat id_rsa.pub 文件,复制,这个就是gitee须要的公钥,注意最后的用户信息不要复制
gitee仓库上操做后,服务器就能免登陆操做仓库了nginx

gitee仓库管理上添加公钥
gitee仓库管理上添加公钥
 
gitee仓库管理上添加公钥
gitee仓库管理上添加公钥
 
gitee仓库管理上添加公钥
gitee仓库管理上添加公钥

 

2. 建立webhooks钩子函数并在服务器上处理

分两步,一是gitee上进行添加webhooks钩子,不懂得能够参照官方文档 https://gitee.com/help/categories/40
第二步,在服务器上添加地址监听,对上一步填写的post地址进行维护,大意就是一旦收到post请求,就执行一个sh命令,这个命令能够pull下仓库代码。git

gitee上添加wenhooks
gitee上添加wenhooks

我这里用的是nodejs的express模块监听某个端口,文件名为wenhooks.js,具体代码以下

 

 
none
var http = require('http')
var createHandler = require('gitee-webhook-handler')
var handler = createHandler({ path: '/webhooks_push', secret: 'cqmygysdssjtwmydtsgx'});
function run_cmd(cmd, args, callback) {
  var spawn = require('child_process').spawn;
  var child = spawn(cmd, args);
  var resp = "";
  child.stdout.on('data', function(buffer) { resp += buffer.toString(); });
  child.stdout.on('end', function() { callback (resp) });
}
handler.on('error', function (err) {
  console.error('Error:', err.message)
})
handler.on('Push Hook', function (event) { // # 这个地方就是GitHub 和 Gitee 不同的地方,须要注意
  console.log('Received a push event for %s to %s',
    event.payload.repository.name,
    event.payload.ref);
    run_cmd('sh', ['/*/*/deploy.sh'], function(text){ console.log("sh deploy success");console.log(text) });//# 须要执行的脚本位置,须要自行修改为本身的,这行代码比较重要,是要让系统去执行一个脚本命令
})
try {
  http.createServer(function (req, res) {
    handler(req, res, function (err) {
      res.statusCode = 404
      res.end('no such location')
    })
  }).listen(1999) // # 服务监听的端口,自行修改
}catch(err){
  console.error('Error:', err.message)
}

而后编辑一下脚本文件deploy.sh,大概内容以下:web

 
none
cd /hexo项目路径 #须要改为本身的项目地址,这行比较重要,当时由于这个问题折腾了很久,全部的都对,就是pull失败
git pull git@gitee.com:zdong22/hexo.git

node下的pm2工具启动进程,很是方便
pm2 start webhooks.js --name wenhook算法

pm2
pm2

 

3.起一个Nginx服务

须要使用ssl,这样传输更安全
首先监听80端口和443端口,而后80端口重定向到443端口
修改nginx.conf文件内容以下:express

 
none
//80端口配置
server
{
        listen 80;
        server_name zhangdong.site;
        index index.html index.htm index.php;
        root  /hexo项目路径;

        error_page   404   /404.html;
        include enable-php.conf;
        
        #将 http 重定向 https
        return 301 https://$server_name$request_uri;
}
//443端口配置
#https zhagndong.site
server {
        listen 443 ssl;
        server_name zhangdong.site;
        root /项目路径;
        index index.php index.html index.htm;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ \.php$ {
                try_files $uri /index.php =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
        # ssl证书地址
        ssl_certificate     /网站证书.pem;  # pem文件的路径
        ssl_certificate_key  /网站证书.key; # key文件的路径
        # ssl验证相关配置
        ssl_session_timeout  5m;    #缓存有效期
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;    #加密算法
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;    #安全连接可选的加密协议
        ssl_prefer_server_ciphers on;   #使用服务器端的首选算法

}

4. 服务器设置一下git参数

pull只拉取public文件夹就好了,其它的不须要
具体方法是:
打开.git文件夹缓存

 
none
[root@iZbp13m488196desr77aezZ .git]# ls
branches  config  description  FETCH_HEAD  HEAD  hooks  index  info  logs  objects  ORIG_HEAD  refs

修改config文件,sparesecheckout 从false改成true安全

 
none
[root@iZbp13m488196desr77aezZ .git]# cat config
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
        sparsecheckout = true
[remote "origin"]
        url = git@gitee.com:zdong22/hexo.git
        fetch = +refs/heads/*:refs/remotes/origin/*

 

修改config文件
修改config文件

修改 .git/info下的 sparse-checkout文件,添加一行, public/

 

 
none
[root@iZbp13m488196desr77aezZ info]# ls
exclude  sparse-checkout
[root@iZbp13m488196desr77aezZ info]# cat sparse-checkout
public/
[root@iZbp13m488196desr77aezZ info]#

 

修改sparse-checkout文件
修改sparse-checkout文件

 

大功告成!

如今发文逻辑就是
hexo new ->编辑 ->hexo clean -> hexo g -> git push ,几个指令,而后网站就更好了。

ps:还有几个重要的点
一是端口要确保能访问,系统防火墙打开端口以后,云服务器控制台还有一道防火墙
二是文件权限必定要肯定,拿一个普通用户去执行root操做确定失败
三是发布以后检查一下,是否是内容存在问题,访问是否正常


来源: 浅浅的无名小卒
文章做者: Zhang
文章连接: https://zhangdong.site/2021/02/02/tong-guo-gitee-guan-li-hexo-fa-bu-nei-rong/
本文章著做权归做者全部,任何形式的转载都请注明出处。

相关文章
相关标签/搜索