Simiki + Travis-ci + Github-Pages 搭建自动部署的个人 Wiki

一些零散的知识记到博客上太零碎,自己本地记录的也不够系统,决定折腾折腾 wiki。

为什么选择 Simiki,此处不展开,这里说说如何结合 Simiki 和 Travis-ci 自动部署 wiki-Web。要达到的效果是:本地 wiki 写好,一条命令 fab commit,网页端 wiki 就好了。

各种依赖

大致步骤

详细说明

创建自己的 github page 项目

此处略,你可以参考官方文档。我这里绑定了自定义域名,所以域名是 http://wiki.zvv.im/

github 项目在 travis-ci 中开启自动部署

代码库中增加 .travis 文件,包含环境、授权、部署命令

这里再略就不用写什么了 ;)

注:我这里 md 文件在 wiki 分支,部署是到 master 分支

环境

新增 .travis.yml 文件,加入如下内容:

language: python
python:
- '2.7'

install:
- pip install fabric
- pip install ghp-import
- pip install simiki

branches:
  only:
  - wiki

把环境依赖装好,设定持续继承只处理 wiki 分支的提交

授权

这块比较复杂,travis 需要在虚拟机中 push 你的代码到 github,所以需要类似本地的 ssh-key 授权。

项目设置中需要增加 deploy key https://github.com/zvving/zvving.github.io/settings/keys

这里我主要参考了 用 Travis CI 自動部署網站到 GitHub | Zespia 这篇文章。大家看这个吧,够细致。

gem install travis
travis login --auto
travis encrypt-file ~/.ssh/id_rsa_bot --add

部署命令

部署命令本身并不复杂,只有

script:
- simiki g
- fab deploy

不过 fab deploy 中碰到两个问题

问题1:
  • Q:
**0.22s**
**$ fab deploy**
**[localhost] local: which -s ghp-import; echo $?**
**Traceback (most recent call last):**
**  File "/home/travis/virtualenv/python2.7.9/lib/python2.7/site-packages/fabric/main.py", line 743, in main**
**    *args, **kwargs**
**  File "/home/travis/virtualenv/python2.7.9/lib/python2.7/site-packages/fabric/tasks.py", line 427, in execute**
**    results['<local-only>'] = task.run(*args, **new_kwargs)**
**  File "/home/travis/virtualenv/python2.7.9/lib/python2.7/site-packages/fabric/tasks.py", line 174, in run**
**    return self.wrapped(*args, **kwargs)**
**  File "/home/travis/build/zvving/zvving.****github.io/fabfile.py****", line 145, in deploy**
**    func(deploy_item)**
**  File "/home/travis/build/zvving/zvving.****github.io/fabfile.py****", line 72, in deploy_git**
**    if int(res.strip()):**
**ValueError: invalid literal for int() with base 10: 'Usage: /usr/bin/which [-a] args\n2'**
  • A: travis 上是 ubuntu 在部署,which 命令没有 -s 参数,所以要把 fabric.py:71 改成 res = local('which -a ghp-import > /dev/null; echo $?', capture=True)
问题2:
  • Q: 我用 ghp-import 一直碰到 rejected,没有找到具体原因
0.57s$ fab deploy
[localhost] local: which -a ghp-import > /dev/null; echo $?
[localhost] local: git fetch
[localhost] local: ghp-import -p -m "Update output documentation" -r git@github.com:zvving/zvving.github.io.git -b master output
Warning: Permanently added the RSA host key for IP address '192.30.252.131' to the list of known hosts.
To git@github.com:zvving/zvving.github.io.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@github.com:zvving/zvving.github.io.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Traceback (most recent call last):
  File "/home/travis/virtualenv/python2.7.9/bin/ghp-import", line 198, in <module>
    main()
  File "/home/travis/virtualenv/python2.7.9/bin/ghp-import", line 194, in main
    sp.check_call(['git', 'push', opts.remote, opts.branch])
  File "/opt/python/2.7.9/lib/python2.7/subprocess.py", line 540, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['git', 'push', 'git@github.com:zvving/zvving.github.io.git', 'master']' returned non-zero exit status 1
Fatal error: local() encountered an error (return code 1) while executing 'ghp-import -p -m "Update output documentation" -r git@github.com:zvving/zvving.github.io.git -b master output'
  • A: 迫不得已,只能先不用 ghp-import,自己写部署命令。具体也不复杂,把这两行
local('ghp-import -p -m "{0}" -r {1} -b {2} {3}' \
      .format(_mesg, remote, branch, output_dir))

改成

local('git fetch')
local('git checkout -b ' + branch)
local('cp -rf output/* ./')
local('rm -rf output')
local('git add -A && git commit -am "test"')
local('git push -f --repo git@github.com:zvving/zvving.github.io.git')
local('git checkout wiki')

具体参见这里ghp-import 也没有 --force 参数,如果你知道好的解法请一定告诉我。

这两个问题花了我不少时间,麻烦留言告知我你是否碰到一样的问题。确认问题了再给作者发 PR。

上最终版本

language: python
python:
- '2.7'
before_install:
- openssl aes-256-cbc -K $encrypted_3db71d9c9ac7_key -iv $encrypted_3db71d9c9ac7_iv
  -in .travis/id_rsa_bot.enc -out ~/.ssh/id_rsa -d
- chmod 600 ~/.ssh/id_rsa
- eval $(ssh-agent)
- ssh-add ~/.ssh/id_rsa
- cp .travis/ssh_config ~/.ssh/config
- git config --global user.name "zvving-bot"
- git config --global user.email "zvving+bot@gmail.com"
- git config --global push.default simple
install:
- pip install fabric
- pip install ghp-import
- pip install simiki
before_script:

script:
# try travis-ci 12
- simiki g
- fab deploy
branches:
  only:
  - wiki

也可以参见这里

一些问题

总结

  • 如果你愿意的话,可以不把 wiki 分支签到本地书写再 commit&push,直接在 github 网页端编辑文件,wiki-web 就自动更新了
  • 部署坑不少,作为 Python 新手走了些弯路,浪费了不少时间

推荐阅读更多精彩内容