为你的hexo博客添加QEXO后端
原理
我们通过在GitHub
上面开启两个仓库,一个用来存放博客的源码private仓库,另一个用来存放博客的静态文件即之前用来存放GitHub pages的仓库,然后通过GitHub Actions
来实现自动部署,这样我们只需要在github
上提交commit
,然后后端就会自动帮我们部署到github
上了。
此时,我们还要在vercel
上部署Qexo
,使用supabase
提供的数据库服务来储存博客数据,然后通过vercel
提供的api
来与Qexo
进行交互,实现博客后端的自动部署。
至此,我们的博客后端就部署完成了。
graph TD
subgraph 存储层
A[GitHub: 博客源码 private仓库]
G[(Supabase\n博客数据存储)]
end
subgraph 构建层
B[GitHub Actions\n自动部署]
C[GitHub Pages\n静态文件仓库]
end
subgraph 展示层
D[GitHub Pages\n前端展示]
E[Vercel\nQexo前端]
end
subgraph 用户层
F[普通用户]
H[博客作者]
end
A -->|代码提交| B
B -->|生成静态文件| C
C -->|部署| D
A -->|同步| E
E -->|API交互| G
F -->|访问博客| D
F -->|动态数据请求| G
H -->|登录管理| E
H -->|发布文章| G
H -->|修改配置| G
classDef repo fill:#f9d5e5,stroke:#e88bb5;
classDef service fill:#b8d8d8,stroke:#7ac2c2;
classDef storage fill:#fed8b1,stroke:#f9a03f;
classDef user fill:#c5e1a5,stroke:#7cb342;
class A,C repo;
class B,D,E service;
class G storage;
class F,H user;
实现步骤
1. 创建GitHub仓库
首先,我们需要在GitHub上创建两个仓库,一个用来存放博客的源码,另一个用来存放博客的静态文件。
创建博客源码仓库
在
GitHub
上创建一个新的仓库,命名为blog-source
,并将你的博客源码上传到这个仓库中。使用命令:1
2
3
4
5git init
git add .
git commit -m "Initial commit"
git remote add origin https://github.com/your-username/blog-source.git
git push -u origin main
创建博客静态文件仓库
这个之前在Hexo博客部署到GitHub Pages中已经讲过了,不再赘述。
2. 配置GitHub Actions
申请GitHub Token:在生成GitHub密钥,建议选择
classic
并需要 Repo & Workflow 下的权限不建议给出所有权限。在
blog-source
仓库中创建一个.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60name: Deploy Hexo to GitHub Pages
on:
push:
branches:
- main # 当推送到 main 分支时触发
paths: # 监测所有 source 和 themes 目录下的文件变动,所有 yml,json 后缀文件的变动。
- '*.json'
- '**.yml'
- '**/source/**'
- '**/themes/**'
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
with:
submodules: false # 禁用子模块检查
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: 'v20.17.0'
- name: Install hexo
run: npm install -g hexo-cli
- name: Install yarn
run: npm install -g yarn
- name: Install full
run: yarn install
- name: Install Hexo Git Deployer
run: |
yarn add hexo-deployer-git --save
yarn add hexo-cli -g
- name: Clean and Generate Static Files
run: |
hexo clean
hexo generate
- name: Configure Git
run: |
git config --global user.name '{$yourGithubName}'
git config --global user.email '{$yourGithubEmail}'
- name: Deploy to GitHub Pages
run: |
cd public/
git init
git add -A
git commit -m "Create by workflows"
git branch -M main
git remote add origin https://{$yourGithubToken}@github.com/{$yourGithubName}/{$yourGithubName}.github.io.git
git push origin HEAD:main -f这是我的
GitHub Action
配置,你可以根据自己的需求进行修改。然后在命令行输入:
1
2
3git add .
git commit -m "Add GitHub Action"
git push- 等待GitHub Actions运行完成,如果一切顺利,你的博客就会自动部署到GitHub Pages上了。
3. 配置Vercel
- 登录Vercel,点击右上角的
Sign Up
,使用GitHub账号登录。 - 在Vercel中创建一个新项目,这里我们通过
Qexo文档
的一键部署链接来创建。 - 在Vercel中配置你的域名,点击
Storage
,选择Create Datebase
创建supabase
的数据库,区域一定要选择N. Virginia (us-east-1)。 - 在
Settings
的Environment Variables
中添加以下变量:
变量名 | 意义 | 值(示例) |
---|---|---|
PG_HOST | PostgreSQL 数据库连接地址 | db.xxx.supabase.co |
PG_PORT | PostgreSQL 数据库通信端口 默认应填写 5432 | 5432 |
PG_USER | PostgreSQL 数据库用户名 | postgres |
PG_DB | PostgreSQL 数据库名 | postgres |
PG_PASS | PostgreSQL 数据库密码 | password |
具体信息可以在Storage
的supabase
中找到
配置完成后,在Deployments
里点击Redeploy
即可完成配置,剩下Qexo的使用就按照Qexo文档来操作即可。
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 浮生若梦!