大部分情況下憨降,都是github pages與hexo組合搭建靜態(tài)博客梨州。但不可否認(rèn)的是湾盗,這樣存在一些問題。比如谁鳍,每次文章寫完都需要要編譯婚瓜,才能上傳,如果我僅僅想寫個博客纤房,這樣的操作顯得繁瑣。又比如翻诉,你擁有多臺電腦的情況,你需要在每臺電腦上都配置一樣的環(huán)境捌刮。
所以通過持續(xù)集成CI/CD的方式碰煌,讓編譯自動化蚀苛,是一種很不錯的解決方案怨规。這里將介紹gitlab-ci來簡化環(huán)境的搭建與編譯過程弯淘。當(dāng)然靜態(tài)資源將托管在gitlab-pages上饺蔑。
There is also a tutorial in English
中文教程
環(huán)境搭建
- 點擊打開gitlab pages例子頁面
- fork其中的hexo項目到自己的賬戶(如果沒有需要創(chuàng)建一個新的gitlab賬戶)
- 刪除fork關(guān)系(點擊項目左側(cè)的settings->General骗爆,然后展開Advanced settings兜看,刪除fork關(guān)系)
- 可選:修改項目信息踏揣,如Project name靶溜、Project description眯杏、Path等夜焦。這里建議Path和Project name一同修改。gitlab pages地址規(guī)則與github是一致的
- 可選:建議修改
.gitlab-ci.yml
中node版本為6.11.2 - 修改任意一文件岂贩,如readme.md或者source/_posts下的文章
當(dāng)你完成這些步驟茫经,你可以點擊右側(cè)的CI/CD,可以看到如下畫面:
當(dāng)然你看到的可能是pending或running萎津,這時請耐心等待卸伞。當(dāng)顯示為passed時,在瀏覽器中輸入托管地址https://your-name.gitlab.io/projext-name锉屈,能訪問荤傲,就意味著搭建成功咯
修改主題
這個項目結(jié)構(gòu)是標(biāo)準(zhǔn)的hexo的項目,除了多個一個.gitlab-ci.yml
颈渊,如下圖
我們僅需關(guān)心script部分即可遂黍,而這部分其實是標(biāo)準(zhǔn)的hexo命令。
如果你想使用next主題(最受歡迎的hexo主題)俊嗽,http://theme-next.iissnan.com官網(wǎng)的網(wǎng)站上已有詳細(xì)的教程
English
Get Started
- Click example projects for gitlab pages
- Fork the project of hexo to your account(If you do not have any gitlab account, you can create a new)
- Remove fork relationship(Choose settings->General from the list on the left.And expand Advanced settings to remove fork relationship)
- Optional: modify project info妓湘,such as Project name, Project description, Path etc. Here's a suggestion about that path and Project name modify together. your website will be available at https://username.gitlab.io/projectname, it is familiar with gitlab pages.
- Optional: suggested modify the node's version from
.gitlab-ci.yml
to 6.11.2 - Modify any file, such as readme.md or the article under source/_posts
- Choose CI/CD, waiting job completion
- Click https://your-name.gitlab.io/projext-name
Advanced
- you can modify script in
.gitlab-ci.yml
. - you can change the theme to next, which is most popular theme of hexo, by a official tutorial http://theme-next.iissnan.com
Updated
Command Lines
Use command lines to init a blog with gilab page, written in 2018.9.19
Init hexo project
hexo init your-blog-dir
cd your-blog-dir
hexo s
Hexo is running at http://localhost:4000, you can test it.
Git remote
Assert your project remote url is https://gitlab.com/your-gitlab-name/project-name.
git init
git remote add origin git@gitlab.com:<your-gitlab-name>/<project-name>.git
git add .
git commit -m "Initial commit"
git push -u origin master
Add CI config
Please rename <*-name>
to real name, such as sed -ri 's/.*(url:.*)/url: https:\/\/jiangtj.gitlab.io/g' _config.yml
wget -O .gitlab-ci.yml https://gitlab.com/JiangTJ/hexo/raw/master/.gitlab-ci.yml?inline=false
sed -ri 's/.*(url:.*)/url: https:\/\/<your-gitlab-name>.gitlab.io/g' _config.yml
If project-name is not your-gitlab-name.gitlab.io, update root
var.
sed -ri 's/.*(root:.*)/root: \/<project-name>\//g' _config.yml
Push to remote
git add .
git commit -m "CI"
git push -u origin master
Wait pipeline finish running, your hexo blog is running at https://your-gitlab-name.gitlab.io/project-name.