前言
在以前的文章中,我们学习了如何使用 Hexo 和 GitHub Pages 搭建基础版个人博客。本文将在此基础上,引入 Hermes Agent 实现 AI 辅助写作,并结合 GitHub Actions 实现自动部署和 AI 摘要生成,打造一个真正自动化的现代博客系统。
回顾:基础版 vs 进阶版
| 功能 | 基础版 | 进阶版 |
|---|---|---|
| 博客框架 | Hexo | Hexo |
| 托管平台 | GitHub Pages | GitHub Pages |
| 写作方式 | 手动编写 Markdown | Hermes Agent AI 辅助 |
| 部署方式 | 手动 hexo d |
GitHub Actions 自动部署 |
| 文章摘要 | 手动编写 | AI 自动生成 |
| 持续集成 | 无 | GitHub Actions |
Hermes Agent 简介
Hermes Agent 是一个强大的 AI 智能助手,可以帮助我们:
- 辅助写作:根据你提供的主题和大纲,自动生成高质量博客文章
- 代码审查:检查文章中的代码示例是否正确
- 内容优化:改进文章结构和语言表达
- 自动化任务:执行重复性任务,如生成摘要、更新目录等
准备工作
假设你已经按照基础版教程完成了以下准备:
- 已安装 Node.js 和 Hexo
- 已创建 GitHub 仓库
<username>.github.io - 本地博客项目已初始化
使用 Hermes Agent 辅助写作
工作流设计
提供主题/大纲 → Hermes Agent 生成草稿 → 人工审核修改 → 发布示例:让 Hermes Agent 生成文章
向 Hermes Agent 发送请求:
"请帮我写一篇关于『Vue 3 Composition API 使用指南』的博客文章,包含以下部分:
- Composition API 简介
- ref 和 reactive 的使用
- 计算属性和侦听器
- 生命周期钩子
- 实战示例
要求:详细实用,包含代码示例,适合初学者阅读。"
Hermes Agent 会生成完整的 Markdown 文章,你只需审核并做少量修改即可发布。
批量生成文章摘要
在 scripts/ 目录下创建 generate-excerpt.js:
const fs = require('fs');
const path = require('path');
// 调用 Hermes Agent API 生成摘要
async function generateExcerpt(articleContent) {
// 这里集成 Hermes Agent 的 API 调用
// 返回文章前 200 字的精炼摘要
const response = await fetch('HERMES_AGENT_API', {
method: 'POST',
body: JSON.stringify({
prompt: `请为以下文章生成一个简洁的中文摘要(100字以内):\n${articleContent}`
})
});
return response.json();
}配置 GitHub Actions 自动部署
创建 Workflow 文件
在博客项目根目录创建 .github/workflows/deploy.yml:
name: Deploy Hexo to GitHub Pages
on:
push:
branches:
- main # 监听 main 分支的推送
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout source
uses: actions/checkout@v4
with:
submodules: true
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '18'
- name: Cache Node modules
uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
- name: Install dependencies
run: npm ci
- name: Generate static files
run: |
npx hexo clean
npx hexo generate
- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./public
publish_branch: gh-pages关键配置说明
| 配置项 | 说明 |
|---|---|
on.push.branches |
监听哪个分支的推送触发部署 |
actions/setup-node |
设置 Node.js 环境 |
actions/cache |
缓存 npm 依赖,加速构建 |
peaceiris/actions-gh-pages |
专门用于将静态文件部署到 GitHub Pages 的 Action |
推送代码触发自动部署
git add .
git commit -m "feat: add new article"
git push origin main推送后,GitHub Actions 会自动执行构建和部署,无需手动运行 hexo deploy。
AI 摘要自动生成
原理说明
利用 Hermes Agent 的 AI 能力,在 GitHub Actions 构建过程中自动为每篇文章生成摘要。
实现方案
创建 .github/workflows/ai-excerpt.yml:
name: AI Generate Excerpt
on:
push:
paths:
- 'source/_posts/**.md'
jobs:
generate-excerpt:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '18'
- name: Generate excerpts with Hermes Agent
env:
HERMES_API_KEY: ${{ secrets.HERMES_API_KEY }}
run: node scripts/ai-excerpt.js
- name: Commit and push
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: "chore: auto-generate excerpts"AI 摘要生成脚本
创建 scripts/ai-excerpt.js:
const fs = require('fs');
const path = require('path');
const frontmatter = require('hexo-front-matter');
const postsDir = path.join(__dirname, '../source/_posts');
async function generateExcerptForFile(filePath) {
const content = fs.readFileSync(filePath, 'utf-8');
const parsed = frontmatter(content);
// 如果已有 excerpt,跳过
if (parsed.excerpt) return;
// 调用 Hermes Agent API
const excerpt = await callHermesAgent(parsed.content);
// 更新 frontmatter
parsed.excerpt = excerpt;
const newContent = frontmatter.stringify(parsed);
fs.writeFileSync(filePath, newContent);
}
// 遍历所有文章
fs.readdirSync(postsDir)
.filter(f => f.endsWith('.md'))
.forEach(f => generateExcerptForFile(path.join(postsDir, f)));Hermes Agent 进阶使用技巧
1. 自动校对文章
"请帮我检查这篇文章的错别字、语法错误和表达不通顺的地方,并给出修改建议。"
2. 生成文章配图 Prompt
"这篇文章需要一个封面图,请生成一个适合 Stable Diffusion 或 Midjourney 的英文 Prompt,主题是『程序员在深夜写代码』。"
3. 自动生成系列文章
"我想写一个『Docker 从入门到精通』的系列文章,包含 5 篇,请帮我规划每篇的主题和大纲。"
4. 自动翻译文章
"请将这篇中文文章翻译成英文,保持技术术语的准确性。"
完整的目录结构
my-blog/
├── .github/
│ └── workflows/
│ ├── deploy.yml # 自动部署
│ └── ai-excerpt.yml # AI 摘要生成
├── scripts/
│ └── ai-excerpt.js # AI 摘要脚本
├── source/
│ ├── _posts/ # 文章目录
│ └── CNAME # 自定义域名
├── themes/
│ └── next/ # Next 主题
├── _config.yml # 站点配置
└── package.json常见问题排查
GitHub Actions 部署失败
问题:Permission denied 错误
解决:确保仓库的 Settings → Actions → General → Workflow permissions 设置为 Read and write permissions。
Hermes Agent API 调用失败
问题:401 Unauthorized
解决:在仓库 Settings → Secrets → Actions 中添加 HERMES_API_KEY 密钥。
自定义域名不生效
问题:DNS 解析正常但无法访问
解决:检查 source/CNAME 文件是否正确,以及 GitHub Pages 设置中的 Custom domain 是否填写。
总结
通过引入 Hermes Agent 和 GitHub Actions,我们实现了:
- AI 辅助写作:提高写作效率和质量
- 自动部署:推送代码即部署,无需手动操作
- AI 摘要:自动为文章生成精炼摘要
- 持续集成:构建、测试、部署全流程自动化
这个进阶方案极大地提升了博客的维护效率,让我们可以专注于内容创作,而不是繁琐的技术操作。
下一步
- 添加 Gitalk 评论系统
- 配置 Algolia 站内搜索
- 使用 hexo-generator-feed 生成 RSS 订阅
- 探索更多 Hermes Agent 的自动化能力