参考文章
如何优雅的使用Github Action服务来将Hexo部署到Github Pages
HEXO系列教程 | 使用GitHub部署静态博客HEXO | 小白向教程
GitHub
1.创建仓库
注册并登录一个GitHub账户【注册方法2.2 准备 GitHub】

点击头像,进入仓库

创建仓库【仓库名字为xx.github.io,并不是非要使用你的github的用户名作为xx,可以进行自定义,如我的为ioip.github.io】

2.获取token
点击头像- Settings-Developer Settings 
在个人设置中新增一个Personal access tokens。至少要包含repo权限,然后记住token。
这个token是给Github Action用的,Github Action会把Hexo编译部署到gh-pages分支。



3.将token填入仓库
随后在存放Hexo代码的仓库里把这个Token新增进去,名称为GH_TOKEN(随意,后面需要一致)。
【注意:先回到仓库再点击setting】


4.获取ssh密钥【用于解决推送文件时GitHub国内的网络问题】
你需要在本地电脑上面安装:
nodejs(>16 版本,最新的应该有 20 版本):Node.js — Run JavaScript Everywhere
git(2.44.0):Git – Downloads
在某一个盘里面创建一个文件夹,名字叫 hexo,然后右键选择 Open Git Bash Here
我们在里面输入git config --global user.name "你的 GitHub 用户名" git config --global user.email "你的 GitHub 邮箱"
我们在命令行窗口中输入ssh-keygen -t rsa -C "你的 GitHub 邮箱"
什么都不用管,一路回车就行。然后我们进入 C:\Users\ 用户名 .ssh 目录(勾选显示 “隐藏的项目”)
我们用记事本打开 id_rsa.pub 并复制里面的内容。
这个时候我们回到 GitHub,进入 Settings:
选择左边栏的 SSH and GPG keys,点击 New SSH key:
Title 随便取,然后把 id_rsa.pub 里面的内容到复制到 Key 中,点击 Add SSH key:
保存完毕以后,我们可以在本地验证一下连接。依旧在 Git Bash Here 界面中输入ssh -T git@github.com
出现 “Are you sure……”,输入 yes 回车确认。若出现下图的提示即连接成功:
切换到ssh用于解决推送文件时GitHub国内的网络问题$ git remote set-url origin git@github.com:luojunchong/PLAN.git
本地配置HEXO
1. 初始化Hexo
安装脚手架,初始化hexo,进入之前新建的hexo文件夹,进入后安装依赖。`npm install -g hexo-cli
hexo init blog
cd blog
npm install
`
2. 初始化仓库
`git init
git remote add origin https://github.com/yourusername/your-repo.git
git add .
git commit -m "Initial commit"
git push -u origin main
`
3. 配置Github Action工作流
在.github文件夹下新增workflows文件夹,然后新增deploy.yml文件,内容如下。
里面有个node-version要和你本地的node一致。【使用node -v获取】
步骤大致意思就是使用ubuntu-latest作为基础环境,然后安装各种依赖,随后hexo generate生成博客网站静态文件夹,
把这个文件夹推送到同一仓库的gh-pages分支。`name: Deploy Hexo to GitHub Pages
on:
push:
branches:
- main # 当推送到 main 分支时触发
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: '18'
- name: Install Dependencies
run: npm install
- name: Install Hexo Git Deployer
run: |
npm install hexo-deployer-git --save
npm install hexo-cli -g
- name: Clean and Generate Static Files
run: |
hexo clean
hexo generate
- name: Configure Git
run: |
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
- name: Deploy to GitHub Pages
env:
GH_TOKEN: ${{ secrets.GH_TOKEN }}
run: |
cd public/
git init
git add -A
git commit -m "Create by workflows"
git remote add origin https://${{ secrets.GH_TOKEN }}@github.com/yourusername/your-repo.git
git push origin HEAD:gh-pages -f`3. 推送验证
把刚才更新的所有文件都推送一遍,github就会触发工作流,然后去网站看工作流运转的如何。
等一切运转完毕,就会发现仓库多出一个gh-pages分支。
`git add .
git commit -m "Initial commit 2"
git push -u origin main
`
4. 配置Github Pages
在仓库settings中配置page来源为gh-pages分支即可。等待网站部署完毕,就可以看了。网站链接可以在settings的GitHub Pages看到,也可以去action里看到。