双线部署遇到的问题

本来博客是放在github上面,但是由于国内网络环境(大家懂的),github访问不是很稳定,随时被查水表。所以采取网友的建议,在gitcafe也部署一个。gifcafe是国内的,速度杠杠的~

这样国内访问走gitcafe,国外访问走github,再做个dnspod双线解析就大功告成。

下面纪录重新双线部署过程中遇到的问题,作学习之用。

hexo部署单个平台没有问题,但是添加一个gitcafe,再deploy就报错了。

deploy:
- type: git
repository: github url
branch: master
- type: git
repository: gitcafe url
branch: gitcafe-pages

github可以正常deploy,gitcafe报错:permission denied。 我想应该是生成的ssh key出了问题。

这里简单说一下ssh key, 为了避免没错连接到github或gitcafe都要输入密码,可以用邮件地址在本地生成id_rsa 和 id_rsa.pub,前者是私钥自己保存,后者是公钥需要添加到github和gitcafe,这样就不用每次连接都输入密码做身份验证了。关于ssh key的生成请戳这里

我之前是针对github和gitcafe分别生成了id_rsa,id_rsa.pub和cafe_rsa和cafe_rsa.pub。

但是问题来了,这么多私钥ssh不知道应该用哪一个。其实~/.ssh/id_rsa是基本私钥,ssh总是把它当作提交私钥的首选项。但是如果存在其他的私钥,就需要向ssh提及,好像登记样,并且配置~/.ssh下config文件来指定不同host应该使用的私钥文件。具体看下面:

ssh-add ~/.ssh/cafe_rsa

检查是否添加成功:

ssh-add -l

如果是这样就成功了:

2048 dc:0f:0d:5b:…:a0:0b:a7:d3:62:a6:bc:bc /Users/username/.ssh/id_rsa (RSA)
2048 d8:2c:1c:25:…:56:14:c7:79:7e:20:43:20 /Users/username/.ssh/cafe_rsa (RSA)

config文件的配置:

Host username.github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa

Host username.gitcafe.com
HostName gitcafe.com
User git
IdentityFile ~/.ssh/cafe_rsa

同理,如果是一个平台有多个用户,只需要添加相同的项并修改Host的username就行了~

但是我想同时生成那么多key也不是很方便,所以取消了cafe_rsa,直接把id_rsa.pub作为github和gitcafe的ssh key。这样两个平台就公用一个本地私钥。也不用再去设置什么config文件。需要注意的是,生成密钥的邮箱和我github,gitcafe的邮箱是一样的。

再修改一下hexo的_config.yml文件:

deploy:
type: git
repository:
github: git@username.github.com:username/username.github.io.git,[branch]
gitcafe: git@username.gitcafe.com:username/username.git,[branch]

这下就可以同时对两个平台进行部署,尽情发布了~

参考资料