Blog 部署
概述
由 Github 托管 Blog 代码,并使用 Github Actions 向 GitHub Pages 和 Cloudflare Pages 推送,做到私有代码库,公网访问。
博客框架选择
因为使用 Gihtub Pages 和 Cloudflare Pages 所以选选择了hexo 来搭建 blog。
工作流程
1 | graph TB |
初始化
准备
使用的是 windows 电脑
- Hexo.exe
- git
- Cloudflare 账户和域名
- VsCode
- npm
- yarn
开始
- 在 Github 上创建2个仓库:
- remote-blog
- xxx.github.io
- 在 Github 上创建2个仓库:
- 将remote-blog 仓库 git clone 到本地。
- 打开 Powershell,将工作路径定到remote-blog,输入 将仓库初始化,后面再调用hexo.exe 将写为hexo。
1
\xxx\hexo.exe init
- 打开 Powershell,将工作路径定到remote-blog,输入
- 输入使用 npm 下载相关组件
1
sudo npm install --save
- 输入
- 创建remote-blog/.github/workflows/xxx.yml文件,用来调用 Github Actions 自动化部署
- 6.
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
57name: Pages
on:
push:
branches:
- main # default branch
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Check Out
uses: actions/checkout@v4
with:
token: ${{ secrets.BLOG_TOKEN }}
# If your repository depends on submodule, please see: https://github.com/actions/checkout
- name: Use Node.js 20
uses: actions/setup-node@v4
with:
# Examples: 20, 18.19, >=16.20.2, lts/Iron, lts/Hydrogen, *, latest, current, node
# Ref: https://github.com/actions/setup-node#supported-version-syntax
node-version: "20"
- name: Cache NPM dependencies
uses: actions/cache@v4
with:
path: node_modules
key: ${{ runner.OS }}-npm-cache
restore-keys: |
${{ runner.OS }}-npm-cache
- name: Install Dependencies
run: npm install
- name: Build
run: npm run build
- name: Upload Pages artifact
uses: actions/upload-pages-artifact@v3
with:
path: ./public
- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
with:
personal_token: ${{ secrets.BLOG_TOKEN }}
publish_dir: ./public
external_repository: xxx/xxx.github.io # 更改为你的 GitHub Pages 仓库, username 是你的用户名
publish_branch: main # GitHub Pages 分支
- name: Deploy to Cloudflare Pages
uses: cloudflare/pages-action@v1
with:
apiToken: ${{ secrets.CF_TOKEN }}
accountId: ${{ secrets.CF_ID }}
projectName: ${{ secrets.CF_NAME }}
directory: ./public
wranglerVersion: "3" - 将修改 push 到 Github 上,此时应该能正常调用 Github Actions 但是因为缺少参数无法继续。
[Blog_Token]:
在这里生成一个拥有权限的Token
[CF_ID]:
[CF_Name]:
[CF_TOKEN]:
- 将修改 push 到 Github 上,此时应该能正常调用 Github Actions 但是因为缺少参数无法继续。
- 到此基础 Hexo 博客已经搭建完成了。后续还可以对 Hexo 使用主题和插件等方便使用。
部署
因为使用的是 Github Actions 自动化部署,所以在本地驶入 git push 并回车就会触发 Gihub Actions 推送到 Github Pages 和 Cloudflare Pages。