你是否厌倦了每次写完博客后手动执行 hexo deploy?想让博客在提交代码后自动部署到服务器?

今天教你如何配置 Jenkins + Hexo 自动化部署!

整体架构

1
GitHub Push → Jenkins Webhook → Jenkins 构建 → SSH 部署 → VPS 服务器

准备工作

在开始之前,你需要准备:

  1. GitHub 仓库:存放 Hexo 博客源码
  2. Jenkins 服务器:负责构建和部署
  3. VPS 服务器:托管博客静态文件
  4. SSH 密钥:Jenkins 访问 VPS 的凭证

Jenkins 任务配置详解

1. General(常规配置)

1
2
✅ 启用项目
JDK: (System) - 使用系统默认

说明:保持默认配置即可,Jenkins 会自动管理 Java 运行环境。

2. 源码管理(Source Code Management)

1
2
3
4
仓库类型: Git
Repository URL: https://github.com/XiaoSiHwang/blog.github.io.git
Credentials: - 无 - (公开仓库不需要认证)
Branches to build: */hexo

关键配置

  • Repository URL:替换为你自己的 GitHub 仓库地址
  • Branches to build:填写要监听构建的分支(这里是 hexo 分支)

💡 如果使用私有仓库,需要在 Credentials 中添加 GitHub 账号或 Personal Access Token。

3. 构建触发器(Build Triggers)

1
✅ GitHub hook trigger for GITScm polling

说明

  • 当 GitHub 收到 push 请求时,会通过 Webhook 通知 Jenkins
  • Jenkins 收到通知后自动触发构建
  • 无需配置用户名密码,基于 GitHub Webhook 认证

配置 GitHub Webhook

  1. 进入 GitHub 仓库 → Settings → Webhooks
  2. 添加新 Webhook:
    • Payload URL: https://your-jenkins-url/github-webhook/
    • Content type: application/json
    • Events: Just the push event

4. 构建环境(Build Environment)

1
2
3
4
5
6
✅ Provide Node & npm bin/ folder to PATH
- NodeJS Installation: Node16.20.2
- npmrc file: use system default
- Cache location: Default

✅ 在构建日志中添加时间戳前缀

说明

  • 安装 Node.js 16.20.2 环境(Jenkins 需要安装 NodeJS Plugin)
  • 构建日志会添加时间戳,便于排查问题

Jenkins 安装 NodeJS 插件步骤

  1. 进入 Jenkins → 系统管理 → 插件管理
  2. 搜索 “NodeJS” 并安装
  3. 进入 系统管理 → 全局工具配置
  4. 添加 NodeJS 安装,选择版本 16.20.2

5. 构建步骤(Build Steps)

执行 Shell 脚本

1
2
3
4
5
6
7
node -v
npm install
#npm install hexo-cli -g
hexo clean
hexo g
rm -fr blog.tar.gz
tar -zcvf blog.tar.gz public/

步骤详解

命令 说明
node -v 打印 Node.js 版本,确认环境正常
npm install 安装 package.json 中的依赖
hexo clean 清理旧的缓存和生成文件
hexo g 生成静态网站(hexo generate)
rm -fr blog.tar.gz 删除旧的压缩包
tar -zcvf blog.tar.gz public/ 打包 public 目录为压缩包

6. 构建后操作(Post-build Actions)

Send build artifacts over SSH

1
2
3
4
SSH Server: Blog
Source files: blog.tar.gz
Remote directory: /home/blog/
Exec command: sh /home/blog/blog.sh

配置说明

配置项 说明
SSH Server Blog 预配置的 SSH 服务器名称
Source files blog.tar.gz 要上传的文件
Remove prefix 不去除任何前缀
Remote directory /home/blog/ 上传到服务器的目录
Exec command sh /home/blog/blog.sh 上传后执行的脚本

远程服务器部署脚本 (/home/blog/blog.sh):

1
2
3
4
5
6
7
8
9
10
#!/bin/bash

# 解压博客文件
cd /home/blog/
rm -rf public/*
tar -zxvf blog.tar.gz
rm blog.tar.gz

# 可选:重启 Nginx
# systemctl reload nginx

完整配置清单

配置项
仓库 https://github.com/XiaoSiHwang/blog.github.io.git
分支 hexo
Node 版本 16.20.2
构建命令 npm install && hexo clean && hexo g
部署方式 SSH 上传 + 远程脚本执行
触发方式 GitHub Webhook

常见问题

Q1: 构建失败,提示找不到 hexo 命令

解决方案:在构建步骤中添加 npm install hexo-cli -g 全局安装 Hexo,或者确保 package.json 中包含 Hexo 依赖。

Q2: SSH 部署失败,提示权限拒绝

解决方案

  1. 检查 Jenkins 服务器上的 SSH 密钥是否正确配置
  2. 确认目标服务器允许密钥登录
  3. 检查目标目录的写入权限

Q3: Webhook 没有触发构建

解决方案

  1. 确认 GitHub Webhook URL 正确
  2. 检查 Jenkins 日志确认收到请求
  3. 尝试手动点击 “立即构建” 测试

Q4: 构建成功但网站没有更新

解决方案

  1. 检查远程服务器上的 blog.sh 脚本是否正确执行
  2. 确认 public 目录内容是否正确更新
  3. 检查 Nginx 配置是否正确

部署流程图

1
2
3
4
5
6
7
8
9
10
11
┌─────────────┐     Push      ┌──────────┐    Webhook    ┌─────────┐
│ GitHub │ ─────────────→ │ GitHub │ ────────────→ │ Jenkins │
│ Repository │ │ Webhook │ │ │
└─────────────┘ └──────────┘ └────┬────┘

│ 构建

┌─────────────┐ SSH ┌──────────┐ 解压 & 部署 ┌─────────┐
│ VPS │ ←─────────── │ Jenkins │ ────────────────→ │ Blog │
│ /home/blog │ │ │ │ Files │
└─────────────┘ └──────────┘ └─────────┘

总结

通过 Jenkins + Hexo 的自动化部署方案,你可以:

  • 🚀 自动构建:代码 push 后自动触发构建
  • 🔄 一键部署:无需手动登录服务器
  • 📊 日志可查:每次构建都有完整日志
  • 🛡️ 安全可靠:SSH 密钥认证,部署过程可控

配置完成后,每次向 hexo 分支推送代码,Jenkins 就会自动完成构建和部署,真正的「提交即发布」!


本文基于实际 Jenkins 配置整理,配置路径:https://jenkins.xiaosihwang.top/job/blog/configure