基于Github和TravisCI
都是墻外工具,可能速度偏慢;
在部署過(guò)程會(huì)有不同的方法:使用hexo d進(jìn)行部署和在public文件夾下直接push到GithubPage倉(cāng)庫(kù);
Coding的請(qǐng)參考這篇>>
Github建立個(gè)人博客
下面步驟也是最簡(jiǎn)單的在Github上面搭建博客的流程:
1.注冊(cè)Github賬號(hào),創(chuàng)建與用戶(hù)名同名的倉(cāng)庫(kù)名;
2.生成Git密鑰對(duì),并保證能夠連通git@github.com;參考鏈接
3.PC端打開(kāi)命令行工具(定位到博客根目錄)分別運(yùn)行以下命令:
npm install -g hexo --save #hexo基于node.js,需提前安裝,此步驟是安裝hexo;
npm install -g hexo-cli --save #可選擇安裝,非必須;
hexo init #初始化;
hexo new #創(chuàng)建一篇新文章,改文章會(huì)存放在`source-->_posts`文件夾之下;
hexo generate #構(gòu)建博客,同hexo g
npm install -g hexo-depoyer #安裝hexo部署器
# 修改hexo配置文件`_config.yml`,添加如下代碼:
deploy:
type: git
repo:
coding: git@github.com:username/username.git
hexo deploy #部署博客,同hexo d
hexo server #開(kāi)啟本地服務(wù)
進(jìn)入username.github.io查看博客情況;
配置TravisCI
1.使用Github賬號(hào)授權(quán)登錄;
2.找到并開(kāi)啟username的倉(cāng)庫(kù)

找到并開(kāi)啟username的倉(cāng)庫(kù)
3.授權(quán):
a.Github設(shè)置中創(chuàng)建Personal access tokens:

Github設(shè)置中創(chuàng)建Personal access tokens:1

Github設(shè)置中創(chuàng)建Personal access tokens:1

Github設(shè)置中創(chuàng)建Personal access tokens:1
b.將token添加到TravisCI的對(duì)應(yīng)項(xiàng)目中:

將token添加到TravisCI的對(duì)應(yīng)項(xiàng)目中:
4.配置TravisCI配置文件
.travis.yml,添加如下代碼:
language: node_js
node_js: stable
# S: Build Lifecycle
install:
# - npm install -g hexo
# - npm install -g hexo-deploy-git
- npm install
before_script:
# - npm install -g gulp
# - npm install -g hexo
- hexo -v
script:
- hexo g
after_script:
- cd ./public
- git init
- git config user.name "yincheng0807"
- git config user.email "yincheng0807@163.com"
- git add .
- git commit -m "Update blog!"
- git push --force --quiet "https://${GITHUB_TOKEN}@${GH_REF}" master:master #GITHUB_TOKEN為在TravisCI中添加的來(lái)自Github的授權(quán)Token,見(jiàn)上一步
branches:
only:
- hexo
# 創(chuàng)建全局變量,與GithubPage倉(cāng)庫(kù)ssh地址一致
env:
global:
- GH_REF: git@github.com:yincheng0807/yincheng0807.github.io.git
關(guān)聯(lián)githubPage倉(cāng)庫(kù):
1.刪除博客源文件根目錄下的.git文件夾,這個(gè)文件夾是hexo的.git;
2.打開(kāi)Git Bash,執(zhí)行:
git init;
git remote add origin
git@github.com:username/username.git
此時(shí)位于master分支
在pages倉(cāng)庫(kù)創(chuàng)建博客源文件分支hexo:
1.創(chuàng)建hexo分支:
git checkout -b hexo;
git pull origin hexo;
git add .;
git commit -am "test";
git push origin hexo;
至此push到遠(yuǎn)成倉(cāng)庫(kù)之后就能自動(dòng)觸發(fā)TravisCI;
p.s.另一種方式的構(gòu)建,替換配置TravisCI第四步中代碼:
language: node_js
branches:
only:
- hexo #源碼分支名稱(chēng)
before_install:
- npm install -g hexo
- npm install -g hexo-cli
before_script:
- git config --global user.name 'yourname'
- git config --global user.email 'youremail'
- sed -i'' "s~git@github.com:<yourname>/<projectname>.git~https://${REPO_TOKEN}:x-oauth-basic@github.com/<yourname>/<projectname>.git~" _config.yml
install:
- npm install
script:
- hexo clean
- hexo generate
after_success:
- hexo deploy