把hexo博客的源碼和生成的頁面實時同步到github和gitcafe。
html
用搜索引擎搜索」github 博客」等關鍵字會出現大量很好的文章教小白一步步搭建。我這裏列出一些關鍵點,但願可讓你少走彎路。這篇博客的markdown源代碼在:https://gitcafe.com/cwjcsu/cwjcsu/blob/master/source/_posts/hexo-github-gitcafe.md
其餘涉及的源碼在同一個倉庫能夠找到。node
1, 不必定要購買域名
不少文章都有介紹購買域名,並在根目錄下配置CName文件,其實不必定要購買的。Github會給每一個用戶一個二級域名:cwjcsu.github.io。這個二級域名下,你能夠定製樣式、404頁面等等,記住最重要的一點:你建立的github的倉庫名稱必須是cwjcsu.github.io,這樣你只要在master分支上 倉庫根目錄push一個index.html,這個頁面就能夠經過 http://cwjcsu.github.io 訪問到。cwjcsu是個人github用戶名,實際操做中替換成你的便可。git
2,使用gh_pages分支建立的頁面
在github上,你能夠爲你的任何倉庫添加一個網站,你只須要:github
- 把網站的頁面push到這個倉庫的gh_pages分支;(github有嚮導能夠指引你自動建立這個分支)
- 經過http://username.github.io/reponame 進行訪問(即
http://博客地址/倉庫名稱/
)。個人github博客地址是:http://cwjcsu.github.io/
,而joutable是我一個開源項目的倉庫名稱,它的頁面能夠經過http://cwjcsu.github.io/joutable 訪問到
3,舉例說明
我在github上面的首頁是:https://github.com/cwjcsu
我在github上的博客源碼倉庫是:https://github.com/cwjcsu/cwjcsu.github.io
我在github上博客首頁:http://cwjcsu.github.io
個人一個開源項目joutable倉庫是:https://github.com/cwjcsu/joutable ,有兩個分支,一個是master放置開源項目源碼,一個是gh_pages放置項目介紹頁面能夠經過http://cwjcsu.github.io/joutable/ 訪問npm
4,hexo搭建
hexo主頁:https://hexo.io/
通過試用,hexo用來寫博客真是不二選擇(配合sublime Text3 +Markdown Editing,),具備下面的優點:bash
- 使用markdown,完美支持github-flavored-markdown
- 實時本地預覽,(#hexo s 建立一個本地http-server在本地實時預覽你的博客網站)
- 大量豐富的主題模版(https://hexo.io/themes/)
- 支持Tex語法(經過mathjax:https://www.mathjax.org )
- 一鍵部署到多個站點(這個是我本身寫的git腳本,下面有介紹)
使用hexo須要安裝nodejs,npm,以及其餘的依賴工具,網上教程很多,本文不贅述(遇到問題能夠給我留言),不過特別提醒如下幾點:markdown
- .yml 配置文件採用縮進進行分開,key和value之間至少要有一個空格;
- 若是hexo生成的html裏面有亂碼,那是對應的源文件沒有用UTF-8保存,你可使用nodepadd++或者記事本把他們保存爲UTF-8,而後從新生成便可;
- 使用
<!--more-->
用來分割摘要和正文,上面部分是摘要,會出如今主頁。同wordpress。 - description:xxx 會生成網頁的description描述:
<meta property="og:description" content="xxx">
這是SEO須要注意的地方。
推薦幾篇好文章:
hexo搭建博客:http://www.cnblogs.com/zhcncn/p/4097881.html
hexo的Jacman主題:https://github.com/wuchong/jacman
hexo配置介紹:https://hexo.io/docs/configuration.html
markdown大全:http://cwjcsu.gitcafe.io/2015/09/26/markdown-learning/hexo
5,hexo部署
在hexo配置文件_config.yml 有個deploy的配置項目用來配置git倉庫,注意type需設置爲git,是hexo3中的類型,須要安裝:hexo-deployer-git:ide
npm install hexo-deployer-git --save |
網上的教程大可能是hexo2的,不少文章沒有指出這個區別。wordpress
#hexo deploy 能夠一鍵部署到github倉庫,可是我須要部署到不一樣倉庫,因此沒有采用hexo的自動部署,二是寫了一個腳本:
在hexo生成的博客根目錄cwjcsu.github.io下有個腳本:up.sh
#!/bin/bash git commit -am "$1" git push github master:master git push gitcafe master:gitcafe-pages |
因而,能夠經過下面命令一鍵把生成的博客內容更新到github和gitcafe了。
#./up.sh "commit comment" |
下面是個人git配置文件:cwjcsu.github.io/.git/config
[core] repositoryformatversion = 0 filemode = false bare = false logallrefupdates = true symlinks = false ignorecase = true hideDotFiles = dotGitOnly [remote "gitcafe"] url = git@gitcafe.com:cwjcsu/cwjcsu.git fetch = +refs/heads/*:refs/remotes/gitcafe/* [remote "github"] url = git@github.com:cwjcsu/cwjcsu.github.io.git fetch = +refs/heads/*:refs/remotes/github/* [branch "master"] remote = github merge = refs/heads/master |
能夠看到有兩個remote:github和gitcafe,分別配置github和gitcafe上面個人博客所在的倉庫地址。up.sh腳本中gitcafe的分支是gitcafe-pages而不是master,倉庫名稱也不是域名而是直接用戶名。這是由於gitcafe博客與github博客略有不一樣,下面會介紹。
爲了不每次push都提示你輸入用戶名和密碼,你須要在github和gitcafe中添加你的公鑰,具體操做本文不贅述。[user]部分我沒有貼出來。
6,gitcafe博客
與github不一樣的是,建立gitcafe的博客,你只須要建立一個和你的用戶名同樣的倉庫,而後把頁面push到這個倉庫的gitcafe-pages分支便可,而後把源碼push到這個分支的master。
好比:
博客所在倉庫是:https://gitcafe.com/cwjcsu/cwjcsu
博客源碼的分支是master:https://gitcafe.com/cwjcsu/cwjcsu/tree/master
博客頁面分支是gitcafe-pages:https://gitcafe.com/cwjcsu/cwjcsu/tree/gitcafe-pages
這兩個分支與github上兩個倉庫代碼是同一份,我又寫了個腳本,用來實時push博客源碼到兩個倉庫:blog/up.sh: (blog是博客源碼所在目錄)
#!/bin/bash git commit -am "$1" git push github master:master git push gitcafe master:master |
blog/.git/config:
[core] repositoryformatversion = 0 filemode = false bare = false logallrefupdates = true symlinks = false ignorecase = true hideDotFiles = dotGitOnly [remote "gitcafe"] url = git@gitcafe.com:cwjcsu/cwjcsu.git fetch = +refs/heads/*:refs/remotes/gitcafe/* [remote "github"] url = git@github.com:cwjcsu/blog.git fetch = +refs/heads/*:refs/remotes/github/* [branch "master"] remote = github merge = refs/heads/master |
[user]部分我沒有貼出來。
7,配置評論系統和百度統計
我使用了國內很火的一個評論系統:多說,註冊、添加站點後,只須要把ID設置到hexo主題的_config.yml文件裏面便可(注意不是hexo的配置文件而是themes/jacman/_config.yml,jacman是我使用的一個主題)。添加百度統計網上教程不少不贅述。