部署Hexo博客

部署Hexo Blog到Github Page和Gitee Page

AI导读:本文介绍了如何将Hexo博客同时部署到Github Page和Gitee Page。首先,使用Github的私人仓库存储博客内容,并通过Github Actions实现自动化部署。该过程包括三个主要任务:构建、上传静态网页到Github Page和Gitee Page。在构建过程中,需要配置访问Token,并利用Git子模块管理自定义主题。文章还提到Node.js版本需与开发环境一致,并提供了简化的上传流程,避免在博客仓库生成gh-pages分支。此外,还给出了新建Github Page仓库的步骤和相关参考链接,确保读者能够顺利完成部署。

前言

参考搭建个人博客的方案选择 提供的部署方案,我们选择同时将网站部署到Github Page和Gitee Page。

目前,博客的仓库在Github,由于一些博文不方便公开增加了密码,所以仓库使用私人(Private)访问权限。

我们使用Github Actions,在每次push到仓库的master分支时,自动化部署网站。我们的Github Actions脚本分为3个Job:builddeploy-to-jankiny_github_iodeploy-to-jankiny_gitee_io

1
2
3
4
5
6
7
8
9
10
11
12
name: Deploy to Github Pages and Gitee Pages
on:
push:
branches:
- master
jobs:
build:

deploy-to-jankiny_github_io:

deploy-to-jankiny_gitee_io:

Build

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
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
# token: ${{ secrets.GITHUB_TOKEN }}
submodules: true

- name: Use Node.js 20.x
uses: actions/setup-node@v2
with:
node-version: '20'

- name: Cache NPM dependencies
uses: actions/cache@v2
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 build artifacts
uses: actions/upload-artifact@v2
with:
name: public
path: public/

actions/checkout@v4

参考checkout@v4文档。

讲Token保存在项目Secrets中

Use Submodule

移除现有的 themes/solon 目录(如果存在)。

使用 Git 子模块的方式添加自定义 solon 主题仓库到 themes/solon:

1
git submodule add [email protected]:jankiny/hexo-theme-solon.git themes/solon

这样做后,你可以通过进入到子模块目录来同步主题的更改:

1
2
3
cd themes/solon
# git pull
# git push

注意:使用Submodule不能实现递归的安装子模块的包,即在Blog仓库执行npm install时不会安装themes/solon下package.json的包。我们不安装hexo-theme-icarus,所以要在Blog仓库的package.json中补充主题所需要的包。

Use Node.js 20.x

这部分需要与开发环境使用的npm版本保持一致。我们在开发时使用的Node.js版本为v20.11.1,所以这里选择20版本。

Node.js版本管理

Cache NPM dependencies && Install Dependencies && Build

与官方的部署代码保持一致。

Upload build artifacts

这部分是用于简化我们部署流程。

以存放Hexo源码的“Blog仓库”和存放Github Page源码的“jankiny.github.io仓库”为例,一般的部署流程是将静态网页存放在“Blog仓库”下的gh-pages里 --> 将gh-pages分支复制到“jankiny.github.io仓库”,我们不愿再“Blog仓库”下生成一个存放静态网页的gh-pages分支,所以直接将生成的静态网页上传,然后在“jankiny.github.io仓库”下载。

部署到Github Page

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
deploy-to-jankiny_github_io:
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout target repository
uses: actions/checkout@v4
with:
repository: 'jankiny/jankiny.github.io'
token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
# token: ${{ secrets.GITHUB_TOKEN }}
path: 'jankiny.github.io'

- name: Download build artifacts
uses: actions/download-artifact@v2
with:
name: public
path: public/

- name: Copy files to the repository
run: |
cp -r public/* jankiny.github.io/

- name: Commit and push
run: |
cd ./jankiny.github.io
git config user.name "GitHub Actions"
git config user.email "[email protected]"
git add .
git commit -m "Deploy Hexo site"
git push

前提

需要新建一个Github Page的仓库(参考GitHub Pages),注意仓库命名为*username*.github.io。建完仓库后随便推个文件到仓库,或者直接在创建时勾选创建README.md,保证仓库有一个main分支master分支

Checkout target repository

这部分使用上面的Secrets,访问“jankiny.github.io仓库”

Download build artifacts && Copy files to the repository && Commit and push

下载之前上传的网页,并将其提交到“jankiny.github.io仓库”

参考文章:

在 GitHub Pages 上部署 Hexo | Hexo

Gitee Pages | Gitee 产品文档

搭建个人博客的方案选择 | dqv5.com

作者

Janki

发布于

2024-02-29

更新于

2024-10-12

许可协议

评论