如何一个hexo博客同时部署多git

<0x01> 如何实现

思路是本地多写几份 config 文件
在部署时切换就可以

<0x02> 开搞

首先先新建几个 config 文件
建议直接复制原来的_config.yml
复制的文件名字随意 我们只需要修改最下面的deploy部分就好

比如我在 hexo 目录建了两个文件
分别是_GitPageConfig.yml_ServerConfig.yml

# _GitPageConfig.yml的部分
deploy:
  type: git
  repo: git@github.com:{github用户名}/{github用户名}.github.io.git
  branch: gh-pages
# _ServerConfig.yml的部分
deploy:
  type: git
  repo: git@{服务器ip}:{gitea用户名}/{gitea仓库名}.git
  branch: master

然后新建一个脚本文件
这里用 windows 配置,所以新建了个 powershell 脚本

# deploy.ps1
copy _ServerConfig.yml _config.yml
hexo g -d
del _config.yml
copy _GitPageConfig.yml _config.yml
hexo g -d
del _config.yml

如果要部署到更多的git仓库,以此类推 之后部署只需要执行deploy.ps1即可

<0x03> 为什么不单文件解决

(@ 23-07-07)
经过我的实际测试,确实也可以直接单文件搞定

# _config.yml
deploy:
- type: git
  repo: git@{服务器ip}:{gitea用户名}/{gitea仓库名}.git
  branch: master
- type: git
  repo: git@github.com:{github用户名}/{github用户名}.github.io.git
  branch: gh-pages

这样写也可以,hexo会按从上到下的顺序依次部署

但这样写会导致一个问题,就是网址的根域名会对不上

# _config.yml
# 部署到我自己的网站
url: http://mlacookie.top/
# 部署到GitHubPage
url: https://MLAcookie.github.io/

如果仅靠单一文件的话,两个部署的根域名会是一样的
这样可能会造成一些小问题

powershell还可以顺便做部署前的一些操作,挺方便的

Licensed under CC BY-NC-SA 4.0