使用 GitHub Actions 實現 Hexo 博客自動部署

Image result for image Hexo

1、Hexo 相關知識點

靜態博客簡單,可是發佈博文時稍顯麻煩,通常須要下面兩步:html

hexo clean
hexo g -d // 至關於 hexo g + hexo d

若是考慮到同步源文件,還須要每次更改後,將源文件 push 到指定倉庫:node

git push origin master

咱們能夠將 Hexo 文件分爲兩類,一類是源文件,即下面這些文件:git

.
├── _config.yml
├── package.json
├── scaffolds
├── source
|   ├── _drafts
|   └── _posts
└── themes

一類是 public 文件,即網站文件:github

public
├── 2020
├── categories
├── tags
....

發佈博文的這三個操做表明:web

  • hexo clean:刪除網站(public)文件
  • hexo g:生成網站(public)文件
  • hexo d:將本地網站(public)文件同步到指定倉庫(如:yourname.github.io)中

我使用一個私有倉庫存放 Hexo 源文件,在 deppwang/deppwang.github.io 中存放網站文件。因此每次發佈或者更新博文時,須要使用 push 操做更新源文件,再執行 hexo cleanhexo g -d 更新博客,比較麻煩。shell

因此咱們但願能在 push 源文件後,由 CI/CD(持續集成/持續部署)工具爲咱們執行 hexo cleanhexo g -d 這兩個操做。npm

Image result for github action

2、GitHub Actions

CI/CD 工具前有 Travis CI,現有 GitHub Actions,這裏使用 GitHub Actions。json

GitHub Actions 的工做原理:咱們提早設置好須要自動化執行的任務,GitHub Actions 監控當前倉庫的某一個操做(如:push),一旦有此操做,就自動化執行這些任務。ubuntu

因此咱們但願使用 GitHub Actions 後,只須要往源文件倉庫 push 更新源文件,GitHub Actions 監控到 push 操做時,就自動化執行 hexo cleanhexo g -d 操做,完成博文發佈。緩存

Action 存放在項目根目錄的 .github/workflows 下,後綴爲 .yml。一個 Action 至關於一個工做流 workflow,一個工做流能夠有多個任務 job,每一個任務能夠分爲幾步 step。任務、步驟依次執行。

每一個 Action 是一個獨立腳本,因此能夠做爲代碼倉庫。

  • actions/setup-node 就表示 github.com/actions/setup-node 這個 倉庫,表明安裝 node.js。Action 爲 action.yml

能夠經過下面這種格式來使用別人寫好的 action,@借用了指針的概念:

actions/setup-node@74bc508 # 指向一個 commit
actions/setup-node@v1.0    # 指向一個標籤
actions/setup-node@master  # 指向一個分支

關於 GitHub Actions 更多知識,請看 GitHub Actions 入門教程 - 阮一峯

如今須要實現一個 Action,使其可以執行 hexo cleanhexo g -d 操做。

Image result for hexo action

3、Hexo Action

我是使用的 sma11black 已經開發好的 Hexo Action,這個 Action 針對的是存放 Hexo 源文件和網站文件分開存放的場景。請先看 教程,如下爲教程的補充。

非第一次生成 SSH Key:

ssh-keygen -t rsa -f ~/.ssh/id_rsa_x -C "yourmail@xxx.com"

將生成的 private key 做爲 Hexo 源文件倉庫 Settings > Secrets 的 一個名叫 DEPLOY_KEYSecret。注意:須要複製包括 -----BEGIN OPENSSH PRIVATE KEY----------END OPENSSH PRIVATE KEY----- 的整個內容。Secret 至關於一個變量,可使私有變量不公開。

將生成的 public key 做爲網站文件倉庫 Settings > Deploy Keys 的 Deploy Key。Deploy Keys 中的公鑰針對於當前倉庫。

爲何要用 SSH Key?

  • SSH Key,是一對密匙:公鑰+私鑰,用於加密本地倉庫和遠程倉庫的傳輸內容。是非對稱加密,可公鑰加密、私鑰解密;或私鑰加密、公鑰解密。
  • 使用 GitHub Actions 是藉助 GitHub 提供的環境,跟本地環境同樣,也須要有私鑰。當 GitHub Action 執行 hexo g -d 時,用私鑰 DEPLOY_KEY 加密,GitHub 用網站文件倉庫的 Deploy Key 進行驗證。

下面是具體的 action.yml:

name: Deploy # workflow name

on:
  [push] # 觸發事件

jobs:
  build: # job1 id
    runs-on: ubuntu-latest # 運行環境爲最新版 Ubuntu
    name: A job to deploy blog.
    steps:
    - name: Checkout # step1 獲取源碼
      uses: actions/checkout@v1 # 使用 actions/checkout@v1
      with: # 條件
        submodules: true # Checkout private submodules(themes or something else). 當有子模塊時切換分支?

    # Caching dependencies to speed up workflows. (GitHub will remove any cache entries that have not been accessed in over 7 days.) 緩存壓縮 node_modules,不用每次下載,使用時解壓,能夠加快工做流的執行過程,超過 7 天沒有使用將刪除壓縮包。
    - name: Cache node modules # step2
      uses: actions/cache@v1
      id: cache
      with:
        path: node_modules
        key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
        restore-keys: |
          ${{ runner.os }}-node-
    - name: Install Dependencies # step3 name
      if: steps.cache.outputs.cache-hit != 'true' # 若是變量 cache-hit 不等於 true
      run: npm install # 安裝 node modules 相關依賴

    # Deploy hexo blog website.
    - name: Deploy # step4
      id: deploy
      uses: sma11black/hexo-action@v1.0.0
      with:
        deploy_key: ${{ secrets.DEPLOY_KEY }}
        user_name: your github username
        user_email: your github useremail

Image result for error

4、可能出現的問題:Deploy 失敗

可能出現如下 3 三種狀況:

一、

image-20200215220503185

Host key verification failed
fatal: Could not read from remote repository.

出現此問題請檢查 站點配置文件,看 deploy 是否存在出 GitHub 之外的倉庫,如 Coding 。

解決方式:去除 GitHub 之外的倉庫。

二、

image-20200215220635619

Load key "/root/ssh/id_rsa": invalid format
gitagithub.com: Permission denied (publickey)
fatal: Could not read from remote repository.

出現此問題請檢查 SSH Key,看 Private Key 是否正確且完整的複製

三、

image-20200216183926423

ERROR Local hexo not found in /github/workspace
ERROR Try running: 'npm install hexo --save'

解決方式:將 uses: sma11black/hexo-action@v1.0.0 改成 uses: deppwang/hexo-aciton@v1.0.1@v1.0.1entrypoint.shhexo g -d 前添加了命令 npm install hexo --save

5、總結

關於 Action 具體如何執行,可結合運行日誌理解。

6、參考

我的公衆號

相關文章
相關標籤/搜索