Deploy Hexo to Github


title: Deploy Hexo to Github
date: 2017-09-06 14:52:25
tags: blog
categories: tools


安裝hexo

hexo中文文檔

必要應(yīng)用:

  • git
  • node.js

安裝git

sudo apt-get install git-core

安裝node.js

  • $ curl https://raw.github.com/creationix/nvm/master/install.sh | sh
  • $ nvm install stable

nvm---node.js的最佳安裝方式(便于管理版本)

安裝hexo

  • 使用淘寶npm鏡像
    • npm --registry https://registry.npm.taobao.org install express
  • 安裝
    • $ npm install -g hexo-cli

npmnode.js的包管理工具("Node Package Manager"), 安裝node.js成功后也已安裝

hexo建站

$ hexo init my_site     # 初始化一個(gè)本地項(xiàng)目
$ tree my_site/ -L  1   # 查看初始化后的項(xiàng)目目錄
my_site/
├── _config.yml
├── node_modules
├── package.json
├── package-lock.json
├── scaffolds
├── source
└── themes
$ cd my_site
(my_site) $ npm install     # 安裝必要包
(my_site) $ hexo server     # 啟動(dòng)本地服務(wù)

github準(zhǔn)備工作

github準(zhǔn)備

  1. github初始化一個(gè){username}.github.io
  2. 添加SSH_key
  3. 復(fù)制倉(cāng)庫(kù)SSH地址:git@github.com:{username}/{username}.github.io.git

修改_config.yml

my_site/

deploy:
  type: git
  repo: git@github.com:{username}/{username}.github.io.git
  baranch: master

安裝deploy插件

$ npm install hexo-deployer-git --save

初始化倉(cāng)庫(kù)

my_site/

$ git init
Initialized empty Git repository in /home/alex/my_site/.git/

初始化提交

my_site/

$ git add .    # add 后面的”.“號(hào),添加倉(cāng)庫(kù)內(nèi)所有文件
$ git commit -m "initial commit"

發(fā)布

my_site/

$ hexo g        # 生成靜態(tài)網(wǎng)(generate)
$ hexo d        # 部署(deploy)
INFO  Deploying: git
...snip...
INFO  Deploy done: git

自定義主題

喜歡Next主題的目錄效果, 還有簡(jiǎn)介的界面, 還有詳細(xì)的部署文檔

下載主題

my_site/

$ mkdir themes/next
$ curl -s https://api.github.com/repos/iissnan/hexo-theme-next/releases/latest | grep tarball_url | cut -d '"' -f 4 | wget -i - -O- | tar -zx -C themes/next --strip-components=1
$ rm -rf themes/lanscape/

修改_config.yml

my_site/_config.yml

titile: Alex's notes
author: Alex
url: http://zhangtengfei.com
theme: next     # 將next主題命名為"next"置于"themes"目錄下

生成頁(yè)面

my_site/

$ hexo new page tags    # 創(chuàng)建tags頁(yè)面,next主題的tags頁(yè)面會(huì)自動(dòng)根據(jù)文章頭信息生成標(biāo)簽云
$ hexo new page categories    # 創(chuàng)建categories頁(yè)面,next主題的categories頁(yè)面會(huì)根據(jù)文章頭信息自動(dòng)分類
$ hexo new page about    # 創(chuàng)建about頁(yè)面

分別在生成的“tags“和”categories“目錄下的”index.md“的頭信息中添加type: ”tags“type: "categories"

tags

my_site/source/tags/index.md

---
title: tags
date: 2017-09-02 10:46:34
type: "tags"
---

categories

my_site/source/categories/index.md

---
title: categories
date: 2017-09-02 10:46:49
type: "categories"
---

修改post模板

my_site/scaffolds/post.md

---
title: {{ title }}
date: {{ date }}
tags:
categories:
---

scaffolds目錄下的post.md是文章模板,在執(zhí)行hexo new [article]時(shí)創(chuàng)建的文章模板基于此模板

添加頭像和網(wǎng)站圖標(biāo)

/my_site/themes/next/_config.yml

avastar: /images/avatar.png
favicon: //images/favicon.ico

在相應(yīng)位置放置圖標(biāo)和圖片

部署到coding.net

  1. coding初始化一個(gè){username}.coding.me
  2. 添加SSH_key
  3. 復(fù)制倉(cāng)庫(kù)SSH地址: git@git.coding.net:{username}/{username}.coding.me.git

注意:

  • 單獨(dú)部署codinggithub
  • 如果同時(shí)部署則repo修改為
deploy:
        type: git
        repo:
            github: git@github.com:{username}/{username}.github.io.git,master
            coding: git@coding.net:{username}/{username}.coding.me.git,master

備份文章原件(創(chuàng)建hexo分支)

my_site/

$ git remote add origin [git@...]
$ git checkout -b hexo
Switched to a new branch 'hexo'
$ git branch
* hexo
 master
$ git add .
The following paths are ignored by one of your .gitignore files:
db.json
node_modules
public
Use -f if you really want to add them.
$ git commit -m "update site"
On branch hexo
nothing to commit, working directory clean
$ git push origin hexo
Counting objects: 435, done.
...snip...
* [new branch]      hexo -> hexo

hexo 分支用來(lái)備份
master 分支用來(lái)部署

備份發(fā)布

在hexo分支下

my_site/

$ hexo new "Deploy Hexo to Github"    # 創(chuàng)建新文章
$ atom source/_post/Deploy-Hexo-to-Github.md    # 編輯新文章
$ git add .    # 添加修改到倉(cāng)庫(kù)
$ git commit -m "add Deploy-Hexo-to-Github.md"    # 提交修改到倉(cāng)庫(kù)
$ git push origin hexo    # 推送hexo分支到云端
$ hexo g    # 生成靜態(tài)文件
$ hexo d    # 發(fā)布master分支到云端

換電腦后操作

只安裝必要應(yīng)用,不需要hexo init

$ git clone [git@...]
$ git checkout hexo
# 已經(jīng)切換到hexo分支
$ npm install    # 安裝hexo必要應(yīng)用
# 如果新電腦都安裝好了,git,hexo-cli,hexo-deployer-git,那么就可以直接開(kāi)始
$ hexo new [new_article]
$ git add .
$ git commit -m "add [new_article]"
$ git push origin hexo
$ hexo g
$ hexo d
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容