语雀编写文章一键自动部署博客至Github和Gitee

# 前言

一键发布语雀文章至 github (hexo 博客) 和 gitee 并实现 gitee pages 自动更新。
hexo 博客示例:https://alanhays.gitee.io | https://alanhays.github.io

  1. 语雀编写文章
  2. 同步语雀文章至 hexo
  3. hexo 发布语雀文章更新博客
  4. github actions 自动同步仓库至 gitee 并更新 pages

# 部署 hexo 博客

# 安装 git 和 node (推荐 LTS 版)

git 官网 -> https://git-scm.com/
node 官网 -> https://nodejs.org/en

# 检查安装

image.png

# 安装 hexo 博客

找到一个位置,打开 Git Bash Here 分别执行下面命令,其中 blog 可替换为你喜欢的名字(会在当前目录新建一个文件夹)

1
2
3
4
npm install hexo-cli -g
hexo init blog
cd blog
hexo s

# 验证博客

浏览器打开 -> http://localhost:4000/
打开后正常显示 hexo 默认博客
image.png

# hexo 常用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
npm install hexo -g # 安装Hexo
npm update hexo -g # 升级
hexo init blog # 初始化博客 blog(选填) 创建一个名为blog的目录并初始化

命令简写
hexo n "文章名" == hexo new "文章名" # 新建文章
hexo g == hexo generate # 生成
hexo s == hexo server # 启动服务预览
hexo d == hexo deploy # 部署

hexo server # Hexo会监视文件变动并自动更新,无须重启服务器
hexo server -s # 静态模式
hexo server -p xxxx # 更改端口
hexo server -i xxx.xxx.x.x # 自定义IP
hexo clean #清除缓存

# GitHub 创建仓库

仓库名称格式你的用户名.github.io 比如我这里应该写 alanhays.github.io
image.png

# 生成秘钥 & 公钥

打开 Git Bash Here 执行以下命令
yourname 改为你的 GitHub 的用户名

1
git config --global user.name "yourname"

youremail 改为你的注册 GitHub 时的邮箱

1
git config --global user.email "youremail"

创建 ssh,输入命令,然后一直回车
youremail 改为你的注册 GitHub 时的邮箱

1
ssh-keygen -t rsa -C "youremail"

在文件中找到这个路径 C:\Users\你的用户名\.ssh
image.png
在 GitHub 的 Setting 里面,找到 SSH keys,把 id_rsa.pub(公钥) 里面的内容全部复制到 key 进去,title 随便写即可。
image.png

# hexo 部署到 GitHub

在 blog 文件夹下面找到_config.yml 文件
image.png
在末尾找到 deploy 并修改(以文本格式打开 vscode 或记事本都可)
image.png
yourname 改为你的 GitHub 的用户名

1
2
3
4
deploy:
type: git
repo: git@github.com:yourname/yourname.github.io.git
branch: master

打开 Git Bash Here 安装 git 部署命令工具

1
npm install hexo-deployer-git --save

分别输入以下命令

1
2
hexo g
hexo d

# 开启 GitHub Pages 服务

打开你的仓库 你的用户名.github.io
image.png
按照步骤操作开启 Pages
image.png
完成后等待一会,你可看到如下页面,点击 Visit site 查看你的博客
image.png

# hexo 主题配置

推荐在主题市场选择喜欢的主题,主题中都有详细的使用教程。
优质主题(排序不分先后)

主题市场 https://hexo.io/themes/
创作主题教程 https://hexo.io/zh-cn/docs/themes.html

# 同步语雀文章

项目:https://github.com/x-cold/yuque-hexo
注:教程请看项目就不赘述了

# GitHub Actions 自动同步

项目:https://github.com/yanglbme/gitee-pages-action
注:教程请看项目就不赘述了,存放 ./github/workflows/xxx.yml 位置请看本文。

# hexo 项目 GitHub Actions 存放位置

gitee 仓库依靠 GitHub Actions ./github/workflows/xxx.yml 实现
需要将 ./github/workflows/xxx.yml 放置 .deploy_git/.github/workflows/xxx.yml

1
2
./github/workflows/xxx.yml
.deploy_git/.github/workflows/xxx.yml

如此 hexo d 命令推送时才会将 Actions 文件推送

# 总结

1. 使用下面命令同步语雀最新文章,其中 xxx… 为 token 或 key。

1
set YUQUE_TOKEN=xxx SECRET_ID=xxx SECRET_KEY=xxx && yuque-hexo sync

2. 使用以下两个命令构建和部署博客

1
2
hexo g
hexo d

执行完后等待 2-3 分钟博客就更新了,如果 github 更新了,gitee 没有那么请查看 GitHub Actions 详细日志,可能因为网络不稳定导致 GitHub Actions 执行失败,请手动执行或再次推送。

# 我的实现一键更新博客(参考)

我将变量设置为用户环境变量,相对不可见,变量名不可自定义 YUQUE_TOKEN = 你的语雀 token (可读权限)

1
hexo clean && yuque-hexo sync && hexo g && hexo algolia && hexo d

# 查看 GitHub Actions 详细日志

image.png
image.png