本地push远程更新的HEXO部署

材料: github bitbucket VPS

部署

  • vps上安装hexo并在/source/_post目录下建立git仓库,同步bitbucket上的私有仓库。
  • vps上的hexo配置文件中填写github站点的deploy信息。
  • 本地只保存/source/_post下的文件。本地不安装hexo,自己写程序生成md文件。
  • 将vps上的ssh public key生成并加入到github和bitbucket上。
  • bitbucket上的仓库增加webhook,当有push时会调用。这一webhook只是告之有更新,在vps上用来更新flag文件的时间戳。
    1
    2
    3
    <?php
    system("touch /home/wangfan/public_html/ghblog/hexod_flag");
    ?>
  • vps上启进程监控此文件,当时间戳更新后:
    • pull下bitbucket中的更新
    • 执行hexo g 和hexo d
      至此,可以完成本地push后实现自动的更新操作。

小问题

  • 权限问题
    使用www-data用户会产生种种比较恶心的权限问题,比如ssh publickey的问题等等。因此,选择以个人用户启动nohup的服务程序是个比较好的选择。这也是为什么不使用php直接进行deploy的原因。
  • ssh rsa key
    需要使用ssh-agent。在服务程序启动时
    1
    2
    3
    4
    eval "$(ssh-agent -s)"
    ssh-add /home/wangfan/.ssh/ram
    #文件监控程序,监控hexod_flag的时间戳
    /home/wangfan/public_html/ghblog/hexod /home/wangfan/public_html/ghblog/hexod_flag

当发生变化时候执行操作:

1
2
3
4
5
6
#!/bin/bash
git pull -f origin master
git checkout -f master
git clean -f
hexo g
hexo d

本地方便留个bash4deploy

1
2
3
git add .
git commit -m "post"
git push origin master

完事。