脚本

由于将博客从 Hexo 迁移到了 Hugo , 没有发现好的自动化部署方法,查资料用的什么 Hook 等机制这些又觉得太麻烦。所以,自己动手写了个自动化部署的脚本,从博客内容自动化部署到 Github Pages 和 Coding Pages 上:

前提:创建一个静态博客的目录,这里假设为/Users/emacsist/public-blog, 用 Hugo 生成的站点目录,这里假设为:/Users/emacsist/git/hugo-blog

然后在你的public-blog上,添加两个远程分支:(自行修改为自己对应的分支路径)

#Github
git remote add origin http://github.com/emacsist/emacsist.github.io

#Coding
git remote add coding http://xxxxx

脚本内容:

#!/bin/bash

SITE_SOURCE="/Users/emacsist/git/hugo-blog"
PUBLIC_DIR="/Users/emacsist/public-blog"

cd "${SITE_SOURCE}"

rm -rf "${SITE_SOURCE}/"public/*

# 将 vec 修改为你的主题名

hugo -t vec  || exit 1

rm -rf "${PUBLIC_DIR}/"* && cp -R "${SITE_SOURCE}/public/"* "${PUBLIC_DIR}/"

cd "${PUBLIC_DIR}"
git add .
git commit -m 'update blog files'
git push origin master --force
git push coding master --force

效果

[18:24:23] emacsist:hugo-blog git:(master) $ ./deploy.sh 
Started building sites ...
Built site for language en:
0 draft content
0 future content
0 expired content
271 pages created
0 non-page files copied
220 paginator pages created
132 tags created
49 categories created
total in 562 ms
[master 72b4092] update blog files
 32 files changed, 923 insertions(+), 580 deletions(-)
 create mode 100644 2016/11/13/Github-上发起-Pull-request/index.html
 create mode 100644 img/first-git-pull-request.png
 create mode 100644 tags/pull-request/index.html
 create mode 100644 tags/pull-request/index.xml
 create mode 100644 tags/pull-request/page/1/index.html
Username for 'https://github.com': xxx
Password for 'xxx': 
Counting objects: 65, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (47/47), done.
Writing objects: 100% (65/65), 277.14 KiB | 0 bytes/s, done.
Total 65 (delta 34), reused 0 (delta 0)
remote: Resolving deltas: 100% (34/34), completed with 28 local objects.
To xxxx
   a298898..72b4092  master -> master
Counting objects: 65, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (47/47), done.
Writing objects: 100% (65/65), 277.14 KiB | 0 bytes/s, done.
Total 65 (delta 34), reused 0 (delta 0)
To xxxx
   a298898..72b4092  master -> master

之前用 Hexo,每次部署一下,都要成分钟。现在 1 秒钟都不用了,飞一般的感觉。Enjoy it !