在Windows和Macbook上自动更新和推送Hexo至GitHub

前言

Hexo自带deploy模块,网上也有很多大神分享各种各样的CI/CD方案,我没有想过写Blog需要这么复杂,而且Hexo已经很稳定了。我因为开始学习使用MacBook,所以逼着自己把之前用在Windows上的脚本用Python重新写了下,很简陋但也很好用,相信大家可以写出更加通用和优美的代码。

使用Powershell和Python自动化更新和推送Hexo静态Blog

更新历史

2018年07月31日 - 初稿

阅读原文 - https://wsgzao.github.io/post...


Windows

./deploy_hexo.ps1
# hexo g
cd .\hexo
hexo clean
hexo g

# del old files
cd ..\wsgzao.github.io
Remove-Item about,archives,categories,css,fancybox,font,img,index,js,page,post -recurse -force

# deploy github
Copy-Item ..\hexo\public\* .\ -recurse -force
git add *
git commit -m "mod"
git push

cd ..

MacBook

python3 deploy_hexo.py
import os
import sys
import subprocess

hexo = os.path.join(os.getcwd(), "hexo")
wsgzao = os.path.join(os.getcwd(), "wsgzao.github.io")

home = os.getcwd()
print ("current directory %s" % home)

os.chdir(hexo)
retval = os.getcwd()
print ("chdir to hexo %s" % retval)

subprocess.call(['hexo clean'], shell=True)

subprocess.call(['hexo g'], shell=True)

os.chdir(wsgzao)
retval = os.getcwd()
print ("chdir to wsgzao %s" % retval)

subprocess.call(['rm -rf about archives categories css fancybox font img index js page post'], shell=True)

subprocess.call(['cp -rf ../hexo/public/* ./'], shell=True)

subprocess.call(['git add *'], shell=True)
subprocess.call(['git commit -m "mod"'], shell=True)
subprocess.call(['git push'], shell=True)

相关推荐