美文网首页
将hexo博客同时托管在github,oschina和codin

将hexo博客同时托管在github,oschina和codin

作者: 我叫小小强 | 来源:发表于2017-07-14 22:31 被阅读526次

    我是小小强,这是我的第11篇原创文章,阅读需要大约15分钟。


    背景

    目前个人博客已经完成hexo搭建、在百度及谷歌收录。截止本文写作时,谷歌可收录博客,百度还无法搜索。由于github禁止百度爬虫抓取,所以需要想办法将博客部署到其他的站点上去。
    经过考察,oschina目前无法绑定域名,coding可以;github私有仓库收费,oschina和coding不收费。所以经过一番折腾之后,目前的博客部署情况如下:

    • 博客源码文件部署在oschina和coding的私有仓库上,本地执行git add,git commitgit push后会推送到对应仓库上
    • 博客产生的静态文件部署在github、coding和oschina的公有仓库上。本地执行hexo cl;hexo g -d之后会自动推送到对应的仓库上。
    • github和coding开启pages服务,用于提供博客服务,同时绑定域名。oschina也开启pages服务,但是无法绑定域名,可提供博客服务。

    准备工作

    创建coding帐号,在coding创建公有仓库,仓库名称与coding用户名一致,作者使用的是xxx,不创建分支。

    配置本地git推送环境

    • 本地创建coding对用的ssh key
      ssh-keygen -t rsa -C "xxx@163.com" -f coding,然后复制coding.pub文件内容,拷贝到coding。
    • ssh-add coding 将私钥加入本地库(以后每次开机都要执行)
    • 执行ssh -T git@git.coding.net测试是否成功
    • 配置.ssh/config文件,加入以下内容

    #coding
    Host coding
    HostName git.coding.net
    User git
    IdentityFile ~/.ssh/coding

    • 修改_config.yml,将

    deploy:
    type: git
    repository: https://github.com/xxx/xxx.github.io.git
    branch: master

    修改为

    deploy:
    type: git
    repo:
    github: git@github.com:xxx/xxx.github.io.git,master
    coding: git@git.coding.net:xxx/xxx.git,master

    注意在yml文件中,:后面都是要带空格的。以后执行hexo cl;hexo d -g可以推送到两个版本库。另外经过测试,下面也可以

    deploy:
    type: git
    repo:
    github: https://github.com/xxx/xxx.github.io.git
    coding: https://git.coding.net/xxx/githubblog.git

    创建coding博客服务

    coding启动pages服务

    上文推送成功后,coding的xxx版本库会有静态文件,然后选择pages服务,选择部署来源master。部署成功后会提示http://xxx.coding.me/xxx/部署成功,点击可以看到博客页面。
    这里其实有个坑,如果你在建立版本库时,库名称和coding用户名不一致,在pages启动后,点击页面发现排版混乱,点击链接显示页面无法找到。如果要解决这个问题,就必须要绑定域名,在成功绑定域名后,再次启动pages服务,页面显示正常。所以,为了减少麻烦,默认还是建立与用户名一致的版本库吧,这样即使不绑定域名,博客也是可以正常访问的。

    绑定域名

    绑定过程比较简单,成功后执行xxx.github.iohttp://xxx.coding.me/xxx/以及http://xxx.coding.me,都会跳转到xiaoxiaoqiang.win

    创建coding私有仓库

    之前已经将hexo博客的源文件部署到oschina的私有仓库上,不过既然使用了coding,顺便也想在coding上也部署一个源文件库作为备份。这里就涉及到本地git库如何推送远端不同的仓库的知识。

    创建coding私有库

    在coding创建私有项目hexo,不创建任何分支。

    修改本地git的config文件

    方法1:

    vim .git/config

    最后增加

    [remote "web"]
    url = https://git.oschina.net/xxx/hexo.git
    url = https://git.coding.net/xxx/hexo.git

    每次执行git add .,git commit ,然后执行git push web

    方法二:

    git remote add web https://git.oschina.net/xxx/hexo.git
    git remote set-url --add https://git.coding.net/xxx/hexo.git
    git push all --all

    这种方法实际修改的也是config文件。

    参考

    hexo高阶教程:想让你的博客被更多的人在搜索引擎中搜到吗?
    Git提交到多个远程仓库

    相关文章

      网友评论

          本文标题:将hexo博客同时托管在github,oschina和codin

          本文链接:https://www.haomeiwen.com/subject/ulaohxtx.html