-
-
Save ckaznable/d24ee4a5c3865dc9a636cf2c8c8531c5 to your computer and use it in GitHub Desktop.
如何使用 Github Actions 自動部署 Hugo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# link: https://sujingjhong.com/posts/how-to-deploy-hugo-automatically-with-github-actions/ | |
# author: lisez <[email protected]> | |
name: Auto publish to public site | |
# 只有推送到 master 才自動化 | |
on: | |
push: | |
branches: | |
- master | |
jobs: | |
hugo-publish: | |
name: publish content to public site | |
runs-on: ubuntu-latest | |
steps: | |
# 使用當前的 repo | |
- name: checkout private repo | |
uses: actions/checkout@v3 | |
with: | |
# 因為目前的 repo 有使用到 submodule,所以 submodule 也要一併同步 | |
# 不然原本的 repo 是沒有 submodule 的內容 | |
submodules: true | |
token: ${{ secrets.GITHUB_PAT }} | |
# 我的公開網站是放置在另一個 repo 所以這裡也要 clone 一份下來處理 | |
# 因為我 Hugo 預設是產生檔案到 public 資料夾,所以這邊我也是 clone 到那裡 | |
- name: checkout public repo | |
uses: actions/checkout@v3 | |
with: | |
# 這裡是那個網站在 github 上的 repo 名稱 | |
repository: lisez/sujingjhong.com | |
path: public | |
# 記得去產生一把 personal access token 放到 repo 的 secrets 裡 | |
# 然後我 secrets 裡的名稱就叫 GITHUB_PAT,用別的名稱的話記得改掉 | |
# 參考 https://help.github.com/en/actions/automating-your-workflow-with-github-actions/authenticating-with-the-github_token | |
token: ${{ secrets.GITHUB_PAT }} | |
# 使用別人做好的 Hugo Actions | |
- name: setup hugo | |
uses: peaceiris/actions-hugo@v2 | |
with: | |
hugo-version: latest | |
extended: true | |
# 開始用 Hugo 產生檔案囉 | |
- name: build content to public site | |
working-directory: ./ | |
# --cleanDestinationDir 清除舊檔案 | |
run: hugo --minify --gc --cleanDestinationDir | |
# 將檔案 commit 到 網站 repo | |
- name: deploy and publish updates | |
working-directory: ./public | |
# user.email 還有 user.name 可以取自己喜歡的,一定要設定不然會出錯 | |
run: | | |
# 當 git 有更動時才進行動作 | |
if [[ `git status --porcelain` ]]; then | |
git config --local user.email "[email protected]" | |
git config --local user.name "GitHub Action" | |
git add . -A | |
git commit -m "[chore] Auto publish" | |
git push origin | |
else | |
echo "content no changes" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment