部署指南
部署方式概览
| 方式 | 说明 | 适用场景 |
|---|
| GitHub Actions | 自动化 CI/CD | 本项目使用 |
| GitHub Pages | GitHub 托管 | 免费托管 |
| GitLab Pages | GitLab 托管 | 私有仓库 |
| 一键部署 | hexo-deployer | 传统方式 |
| 手动部署 | rsync/scp | 自有服务器 |
本项目部署架构
1 2 3 4 5 6 7 8 9
| 本地开发 → git push master → GitHub Actions 触发 ↓ npm install ↓ npx hexo generate ↓ SSH Deploy 到服务器 ↓ https://dhongli.cloud
|
GitHub Actions 配置
工作流文件
文件位置:.github/workflows/deploy.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| name: Deploy Hexo
on: push: branches: - master
jobs: build-and-deploy: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v4
- name: Use Node.js uses: actions/setup-node@v4 with: node-version: '22'
- name: Install Dependencies run: npm install
- name: Generate Static Files run: npx hexo generate
- name: Deploy to Server uses: easingthemes/ssh-deploy@v5.0.3 with: SSH_PRIVATE_KEY: ${{ secrets.SERVER_SSH_KEY }} SOURCE: "public/" TARGET: "/var/www/hexo" REMOTE_HOST: "dhongli.cloud" REMOTE_USER: "root" ARGS: "-rltgoDzvO --delete"
|
配置说明
| 参数 | 说明 |
|---|
on.push.branches | 触发条件:push 到 master 分支 |
node-version | Node.js 版本(推荐 22) |
SOURCE | 要部署的目录(hexo generate 生成的 public/) |
TARGET | 服务器目标路径 |
REMOTE_HOST | 服务器地址 |
ARGS | rsync 参数,--delete 会删除目标目录中不存在的文件 |
GitHub Secrets 配置
需要配置的 Secrets
| Secret 名称 | 说明 | 获取方式 |
|---|
SERVER_SSH_KEY | 服务器 SSH 私钥 | ssh-keygen 生成 |
生成 SSH 密钥
1 2 3 4 5 6 7 8
| ssh-keygen -t ed25519 -C "github-actions"
cat ~/.ssh/id_ed25519
cat ~/.ssh/id_ed25519.pub
|
配置服务器
1 2 3 4 5 6 7
| mkdir -p ~/.ssh chmod 700 ~/.ssh
echo "公钥内容" >> ~/.ssh/authorized_keys chmod 600 ~/.ssh/authorized_keys
|
添加 GitHub Secret
- 进入 GitHub 仓库
- 点击 Settings → Secrets and variables → Actions
- 点击 New repository secret
- 名称填写:
SERVER_SSH_KEY - 值填写:私钥内容(包括 BEGIN 和 END 行)
GitHub Pages 部署
配置文件
1 2 3 4 5 6
| deploy: type: git repo: https://github.com/username/username.github.io.git branch: main message: "Site updated: {{ now('YYYY-MM-DD HH:mm:ss') }}"
|
安装部署插件
1
| npm install hexo-deployer-git --save
|
部署命令
1
| hexo clean && hexo deploy
|
配置 GitHub Pages
- 进入仓库 Settings → Pages
- Source 选择
main 分支 - 目录选择
/ (root) - 保存后等待部署完成
服务器部署
Nginx 配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| server { listen 80; server_name dhongli.cloud; root /var/www/hexo; index index.html;
location / { try_files $uri $uri/ /index.html; }
location ~* \.(css|js|png|jpg|jpeg|gif|ico|svg|woff|woff2)$ { expires 30d; add_header Cache-Control "public, immutable"; }
gzip on; gzip_vary on; gzip_min_length 1024; gzip_types text/plain text/css application/json application/javascript text/xml; }
|
HTTPS 配置
使用 Let’s Encrypt 免费证书:
1 2 3 4 5 6 7 8
| apt install certbot python3-certbot-nginx
certbot --nginx -d dhongli.cloud
certbot renew --dry-run
|
文件权限
1 2 3
| chown -R www-data:www-data /var/www/hexo chmod -R 755 /var/www/hexo
|
一键部署
部署到 Heroku
部署到 Vercel
- 连接 GitHub 仓库
- 框架选择 Other
- Build Command:
npx hexo generate - Output Directory:
public
部署到 Netlify
- 连接 GitHub 仓库
- Build Command:
npx hexo generate - Publish Directory:
public
部署验证
检查构建日志
在 GitHub 仓库 → Actions 标签查看构建状态
检查网站
访问网站确认更新生效
常见问题排查
| 问题 | 可能原因 | 解决方案 |
|---|
| 构建失败 | Node.js 版本不兼容 | 使用 Node.js 22 |
| 部署失败 | SSH 密钥配置错误 | 检查 GitHub Secret |
| 样式丢失 | CDN 问题 | 检查 CDN 配置 |
| 图片不显示 | 路径错误 | 检查图片路径 |
| 缓存问题 | 浏览器缓存 | 强制刷新 (Ctrl+F5) |
性能优化
构建优化
1 2 3 4
| ignore: - source/**/*.psd - source/**/*.ai
|
缓存策略
1 2 3 4 5 6 7 8 9 10 11
| location ~* \.(css|js)$ { expires 1y; add_header Cache-Control "public, immutable"; }
location ~* \.html$ { expires -1; add_header Cache-Control "no-cache, no-store, must-revalidate"; }
|
CDN 配置
1 2 3 4 5
| CDN: internal_provider: local third_party_provider: jsdelivr version: true
|
回滚部署
如果部署后发现问题:
1 2 3 4 5 6 7 8 9 10
| git log --oneline -10
git revert <commit-hash> git push origin master
git reset --hard <commit-hash> git push origin master --force
|
下一步