hexo搭建博客deploy失败

用hexo搭建了博客,但是部署不了,网上有说可能是SSH端口没开,但可以用SSH协议从github上克隆代码。为了方便部署,我自己又另外想了方法。

1.新建git仓库

在之前hexo的目录下,新建一个文件夹,我的是hexo\faileddeploy,将放置在github的博客静态页面clone下来。

1
git clone git@github.com:yeahyuanw/yeahyuanw.github.io.git

以后,每次

1
hexo g

之后,就先将本地yeahyuanw.github.io里的代码删除,然后将public里面的文件复制到yeahyuanw.github.io,再手动推送到yeahyuanw.github.io。为了方便操作,我就写了一个脚本,用来代替

1
hexo deploy

当用hexo部署不成功的时候就运行脚本部署。

2.用python脚本来删除和复制文件

delete.py用来删除faileddeploy\yeahyuanw.github.io

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os

def _deletefile_(path):
if os.path.isdir(path):
if os.listdir(path) == None:
os.rmdir(path)
else:
pathlist = os.listdir(path)
for i in pathlist:
temp_path = path + '\\' + i
_deletefile_(temp_path)
os.rmdir(path)
print(path + 'deleted')
else:
os.remove(path)
print(path + 'deleted!')
return None

curpath = os.getcwd()
deploypath = curpath + '\\faileddeploy' + '\\yeahyuanw.github.io'
# .git文件夹保留
filelist = [x for x in os.listdir(deploypath) if x != '.git']
for x in filelist:
temppath = deploypath + '\\' + x
_deletefile_(temppath)
print('Delete Done!')

copy.py用来将public下的文件复制到faileddeploy\yeahyuanw.github.io

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import shutil

def _copyfile_(srcpath,goalpath):
#判断目标路径是否存在,如果不存在则创建
if os.path.exists(goalpath):
pass
else:
os.mkdir(goalpath)
if os.listdir(srcpath) == None:
splitpath = os.path.split(srcpath)
print(splitpath[1] + 'is empty!')
return None
else:
dirpath = [x for x in os.listdir(srcpath) if os.path.isdir(srcpath + '\\' + x)]
filepath = [x for x in os.listdir(srcpath) if not os.path.isdir(srcpath + '\\' + x)]
if filepath != None:
for x in filepath:
shutil.copy(srcpath + '\\' + x, goalpath)
print(srcpath + '\\' + x + ' copied!')
if dirpath != None:
for x in dirpath:
shutil.copytree(srcpath + '\\' + x, goalpath + '\\' +x)
print(srcpath + '\\' + x + ' copied!')
return None

curpath = os.getcwd()
goalpath = curpath + '\\faileddeploy' + '\\yeahyuanw.github.io'
srcpath = curpath + '\\public'
_copyfile_(srcpath,goalpath)
print('Copy Done!')

3.部署脚本

然后编写脚本hexo_deploy.bat来执行上面说的流程:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
call py3 _delete_.py
cd faileddeploy\yeahyuanw.github.io
git add -A
git commit -m "delete"
git pull --rebase origin master
git push origin master

cd ..\..\

call py3 _copy_.py
cd faileddeploy\yeahyuanw.github.io
git add -A
git commit -m "add"
git pull --rebase origin master
git push origin master
cd ..\..\

这里需要注意的是要 用call 来调用py3delete.py和copy.py,不然只会执行第一条命令,因为.py本身也是批处理文件