使用 github+Action 部署 Hexo 博客

前言

為便于敘述,統(tǒng)一說明如下:

  • 博客 github 源碼倉庫默認(rèn)分支為 main,ssh 地址:git@github.com:username/blog.git
  • username.github.io:博客部署倉庫,默認(rèn)分支為 main
  • hexo_deploy:本地私鑰文件
  • `hexo_deploy.pub:本地公鑰文件
  • 主題以 NexT 8.13.2 為例

本地 hexo 安裝、主題部署流程較簡單,步驟省略。

1、配置本地博客源代碼庫

在本地博客源代碼倉庫根目錄 _config.yml 文件中增加如下部署配置:

# Deployment
## Docs: https://hexo.io/docs/one-command-deployment
deploy:
  type: git
  repo: git@github.com:username/username.github.io.git
  branch: main

注意:github 目前僅允許 ssh 提交,部署倉庫必須為 ssh 地址

2、配置專用密鑰對(duì)

本地重新生成一組密鑰對(duì),如 hexo_deploy.pubhexo_deploy。私鑰配置到 github 源碼倉庫的 repository secret,公鑰配置到 username.github.io 部署倉庫的 Deploy keys。該密鑰對(duì)專用于「源碼倉庫」與「部署倉庫」之間的 Action 自動(dòng)部署。

github 源碼庫新建后即可配置:依次進(jìn)入 Settings -> Secrets -> Actions -> New repository secret,名稱取 HEXO_DEPLOY_SECRET,值取自 hexo_deploy 內(nèi)容;username.github.io.git 部署倉庫配置步驟略。

也可以拿用戶公鑰 SSH keys(可訪問全部倉庫)及其對(duì)應(yīng)的私鑰完成該步驟,但是用戶公鑰不一致時(shí)(比如辦公室和家里使用的用戶公鑰不同,除非復(fù)制同一份密鑰對(duì)),Action 自動(dòng)部署的私鑰也要更改,使用專用密鑰對(duì)可避免這個(gè)問題。

3、創(chuàng)建 Action

在本地博客源代碼庫根目錄下創(chuàng)建 .github/workflows/deploy.yml 文件,參考內(nèi)容如下:

name: hexo-blog-deploy

on:
  push:
    branches:
      - main

env:
  GIT_USER: username
  GIT_EMAIL: username@gmail.com
  DEPLOY_REPO: username/username.github.io
  DEPLOY_BRANCH: main

jobs:
  build:
    name: Build on node ${{ matrix.node_version }} and ${{ matrix.os }}
    runs-on: ubuntu-latest
    strategy:
      matrix:
        os: [ubuntu-latest]
        node_version: [19.x]

    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Checkout deploy repo
        uses: actions/checkout@v2
        with:
          repository: ${{ env.DEPLOY_REPO }}
          ref: ${{ env.DEPLOY_BRANCH }}
          path: .deploy_git

      - name: Use Node.js ${{ matrix.node_version }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node_version }}

      - name: Configuration environment
        env:
          HEXO_DEPLOY_SECRET: ${{secrets.HEXO_DEPLOY_SECRET}}
        run: |
          sudo timedatectl set-timezone "Asia/Shanghai"

          # 配置私鑰,否則無法訪問部署庫,提示 git@github.com: Permission denied (publickey).
          mkdir -p ~/.ssh/
          echo "$HEXO_DEPLOY_SECRET" > ~/.ssh/id_rsa
          chmod 600 ~/.ssh/id_rsa
          ssh-keyscan github.com >> ~/.ssh/known_hosts

          # 設(shè)置用戶名和郵箱,否則提示 Please tell me who you are.
          git config --global user.name $GIT_USER
          git config --global user.email $GIT_EMAIL

      - name: Install dependencies
        run: |
          npm install
          npm install hexo-deployer-git --save
          # 全局搜索插件
          npm install hexo-generator-searchdb --save

      - name: Deploy hexo
        run: |
          npm run deploy

4、提交

配置完成后提交到 github 源碼倉庫即可觸發(fā) Action

(可選)部署留言欄 Utterances

安裝 github 應(yīng)用: https://github.com/apps/utterances

next 最新版本已經(jīng)集成 Utterances,因此不需要復(fù)制安裝完成后的模板,直接在主題文件根目錄下設(shè)置 _config.yml 文件即可:

# Utterances
# For more information: https://utteranc.es
utterances:
  enable: true
  repo: username/username.github.io # Github repository owner and name
  # Available values: pathname | url | title | og:title
  issue_term: pathname
  # Available values: github-light | github-dark | preferred-color-scheme | github-dark-orange | icy-dark | dark-blue | photon-dark | boxy-light
  theme: github-light

(可選)部署域名

參考官方文檔:驗(yàn)證用戶站點(diǎn)的域

注意:需要在 blog 源代碼庫 source 目錄下放置 CNAME 文件。之后步驟按官方文檔進(jìn)行

?著作權(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),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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