前言
為便于敘述,統一說明如下:
- 博客 github 源碼倉庫默認分支為 main,ssh 地址:
git@github.com:username/blog.git
-
username.github.io
:博客部署倉庫,默認分支為 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、配置專用密鑰對
本地重新生成一組密鑰對,如 hexo_deploy.pub
與 hexo_deploy
。私鑰配置到 github 源碼倉庫的 repository secret
,公鑰配置到 username.github.io
部署倉庫的 Deploy keys
。該密鑰對專用于「源碼倉庫」與「部署倉庫」之間的 Action 自動部署。
github 源碼庫新建后即可配置:依次進入
Settings -> Secrets -> Actions -> New repository secret
,名稱取HEXO_DEPLOY_SECRET
,值取自hexo_deploy
內容;username.github.io.git
部署倉庫配置步驟略。
也可以拿用戶公鑰 SSH keys
(可訪問全部倉庫)及其對應的私鑰完成該步驟,但是用戶公鑰不一致時(比如辦公室和家里使用的用戶公鑰不同,除非復制同一份密鑰對),Action 自動部署的私鑰也要更改,使用專用密鑰對可避免這個問題。
3、創建 Action
在本地博客源代碼庫根目錄下創建 .github/workflows/deploy.yml
文件,參考內容如下:
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
# 設置用戶名和郵箱,否則提示 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 源碼倉庫即可觸發 Action
(可選)部署留言欄 Utterances
安裝 github 應用: https://github.com/apps/utterances
next 最新版本已經集成 Utterances,因此不需要復制安裝完成后的模板,直接在主題文件根目錄下設置 _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
(可選)部署域名
參考官方文檔:驗證用戶站點的域
注意:需要在 blog 源代碼庫 source 目錄下放置 CNAME 文件。之后步驟按官方文檔進行