在 Hexo 推送博客期间出现的问题与解决方案

问题背景

近期使用Hexo部署网站到Github远端仓库的时候,经常会出现如下报错:

[noone@open-source blog]$ hexo d
INFO  Validating config
INFO  Deploying: git
INFO  Clearing .deploy_git folder...
INFO  Copying files from public folder...
INFO  Copying files from extend dirs...
[master 595037e] Site updated: 2021-03-17 11:54:50
 1 file changed, 1 insertion(+), 1 deletion(-)
fatal: unable to access 'https://github.com/srezone/srezone.github.io.git/': Encountered end of file
FATAL { err:
   { Error: Spawn failed
       at ChildProcess.task.on.code (/home/noone/WorkSpace/blog/node_modules/hexo-deployer-git/node_modules/hexo-util/lib/spawn.js:51:21)
       at ChildProcess.emit (events.js:198:13)
       at Process.ChildProcess._handle.onexit (internal/child_process.js:248:12) code: 128 } } 'Something\'s wrong. Maybe you can find the solution here: %s' '\u001b[4mhttps://hexo.io/docs/troubleshooting.html\u001b[24m'
[noone@open-source blog]$

根因定位

几经搜索尝试,无果,恰好前段时间收到一封Github的邮件,告知如下:

Github弃用密码访问博客仓库的通知

OK,基本了解报错的根因:Github的问题,而不是我这边仓库不一致导致提交冲突等乱七八糟的原因。

接下来的思路是,配置Hexo使用密钥认证方式完成Git提交:

解决步骤

生成密钥

[noone@open-source ~]$ ssh-keygen -t rsa -b 4096 -C "noone@open-source.cc"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/noone/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/noone/.ssh/id_rsa.
Your public key has been saved in /home/noone/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:Z***1/cn********************u/L*****9 noone@open-source.cc
The key's randomart image is:
+---[RSA 4096]----+
|                 |
|=. . .           |
|=. . .           |
|=. . .           |
|=. . .           |
|=. . .           |
|=. . .           |
|  o+o... o o. o =|
|  o+o... o o. o =|
+----[SHA256]-----+
[noone@open-source ~]$ cd .ssh/
[noone@open-source .ssh]$ ll
total 12
-rw------- 1 noone noone 3243 Mar 17 13:33 id_rsa
-rw-r--r-- 1 noone noone  746 Mar 17 13:33 id_rsa.pub
-rw-r--r-- 1 noone noone 1049 Mar 15 22:11 known_hosts

配置Github使用密钥认证

妥善保存私钥文件(id_rsa),将公钥文件(id_rsa.pub)内容复制,粘贴到Github对应的页面中,完成GitHub端的具体配置:

Github设置密钥登录的配置界面

验证密钥配置是否生效,如果配置成功会返回具体提示,如下:

[noone@open-source .ssh]$ ssh -T git@github.com
The authenticity of host 'github.com (13.250.777.333)' can't be established.
RSA key fingerprint is SHA256:n******************************************.
RSA key fingerprint is MD5:16:27:ac:a5:16:27:ac:a5:16:27:ac:a5:16:27:ac:a5.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,13.250.777.333' (RSA) to the list of known hosts.
Hi srezone! You've successfully authenticated, but GitHub does not provide shell access.
[noone@open-source .ssh]$

配置Hexo使用SSH密钥认证方式

修改Hexo站点根目录下的配置文件,在deploy字段将相关配置行修改为SSH格式:

[noone@open-source blog]$ vim _config.yml
......
# Deployment
## Docs: https://hexo.io/docs/one-command-deployment
deploy:
   type: git
#  repository: https://github.com/srezone/srezone.github.io.git
## 删除上面这行,上面的URL是用于HTTPS连接,使用用户名密码登录
## 修改为下面这行的格式,用户名和仓库名使用自己的替换即可
   repository: git@github.com:srezone/srezone.github.io.git
   branch: main
......

重新部署,没有问题,且没有了输入用户名和密码的环节,显得非常流畅顺滑:

[noone@open-source blog]$ hexo g
INFO  Validating config
INFO  Start processing
INFO  Files loaded in 329 ms
INFO  0 files generated in 383 ms
[noone@open-source blog]$ hexo d
INFO  Validating config
INFO  Deploying: git
INFO  Clearing .deploy_git folder...
INFO  Copying files from public folder...
INFO  Copying files from extend dirs...
# On branch master
nothing to commit, working directory clean
Branch master set up to track remote branch main from git@github.com:srezone/srezone.github.io.git.
Everything up-to-date
INFO  Deploy done: git
[noone@open-source blog]$

在 Hexo 推送博客期间出现的问题与解决方案
https://srezone.open-space.cc/article/1517646518.html
作者
Richard Li
发布于
2021年3月17日
许可协议