一、准备
首先电脑部署生成hexo博客,参考:
二、开始教程
前提
Node.js环境和Git都已安装且Hexo可以正常运行与部署。
注意文章里面的master是仓库分支名,根据自身情况修改main或其他分支名。
改根_config.yml 文件中的deploy为如下内容:
deploy: type: git repo: git@github.com:用户名/Pages仓库名.git branch: master
生成key,github部署时需要:
ssh-keygen -t rsa -b 4096 -C "Hexo Deploy Key" -f github-deploy-key -N ""
会在当前目录生成:
github-deploy-key—私钥
github-deploy-key.pub—公钥
新建一个博客仓库存放Hexo源文件,把博客文件放到里面,之后设置博客代码仓库 Settings—secrets and variables—Repository secrets
名字填写:
HEXO_DEPLOY_KEY (大写)
后面Actions会用到不要写错哦!
填入 github-deploy-key(私钥)里面的内容。
公钥放到Pages对应的代码仓库里面。
访问Pages对应的代码仓库 Settings— Deploy keys—Add deploy key
名字:HEXO_DEPLOY_PUB (可自定义)
在 Key 填入 github-deploy-key.pub(公钥)中的内容,别忘了Allow write access 勾上。
在博客代码仓库里点Actions
创建一个新文件:
name: Hexo Deploy
on:
workflow_dispatch:
branches:
- master
jobs:
build:
runs-on: ubuntu-latest
if: github.event.repository.owner.id == github.event.sender.id
steps:
- name: Checkout source
uses: actions/checkout@v4
with:
ref: master
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Setup Hexo
env:
ACTION_DEPLOY_KEY: ${{ secrets.HEXO_DEPLOY_KEY }}
run: |
sudo timedatectl set-timezone 'Asia/Shanghai'
mkdir -p ~/.ssh/
echo "$ACTION_DEPLOY_KEY" > ~/.ssh/id_rsa
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa
ssh-keyscan github.com >> ~/.ssh/known_hosts
git config --global user.email "改成你的邮箱"
git config --global user.name "改成你的用户名"
npm install hexo-cli -g
npm install
- name: Deploy
run: |
hexo clean
hexo deploy
其中:node-version: '20',指nodejs版本。
master指分支,新建好的仓库改名为main。
结尾:别忘了给主题目录里面的.git目录删除,防止Git推送失败。
Git上传博客源码到Github,参考
这么做以后只需要上传.md文件,再手动点击Actions(Actions会自动生成public目录,并把里面的全部文件上传到Pages仓库),舒适。

评论加载中...