自動化部署

有許多方法可以輕鬆自動化 Jekyll 網站的部署。

持續整合服務

設定自動化部署流程最簡單的方法之一就是使用 CI。

這些服務會在 Git 儲存庫中有提交時執行指令碼。您可能希望此指令碼建置網站、對輸出執行測試,然後將其部署到您選擇的服務。

我們有以下供應商的指南

Git post-receive 掛鉤

若要讓遠端伺服器在您每次使用 Git 推送變更時為您處理部署,您可以建立一個使用者帳戶,其中包含所有在 authorized_keys 檔案中被授權部署的公開金鑰。有了這個,設定 post-receive 掛鉤的方式如下

laptop$ ssh deployer@example.com
server$ mkdir myrepo.git
server$ cd myrepo.git
server$ git --bare init
server$ cp hooks/post-receive.sample hooks/post-receive
server$ mkdir /var/www/myrepo

接下來,將以下列新增至 hooks/post-receive,並確保伺服器已安裝 Jekyll

#!/bin/bash -l

# Install Ruby Gems to ~/gems
export GEM_HOME=$HOME/gems
export PATH=$GEM_HOME/bin:$PATH

TMP_GIT_CLONE=$HOME/tmp/myrepo
GEMFILE=$TMP_GIT_CLONE/Gemfile
PUBLIC_WWW=/var/www/myrepo

git clone $GIT_DIR $TMP_GIT_CLONE
BUNDLE_GEMFILE=$GEMFILE bundle install
BUNDLE_GEMFILE=$GEMFILE bundle exec jekyll build -s $TMP_GIT_CLONE -d $PUBLIC_WWW
rm -Rf $TMP_GIT_CLONE
exit

最後,在任何需要使用此掛勾進行部署的使用者筆電上執行以下指令

laptops$ git remote add deploy deployer@example.com:~/myrepo.git

現在,只要告訴 nginx 或 Apache 查看 /var/www/myrepo 並執行以下指令,即可輕鬆部署

laptops$ git push deploy master