使用 GitHub Actions 自动部署 hexo
写在前面
好朋友林桑推荐(不是鬼子),他说使用 GitHub Actions
可以白嫖 GitHub
的服务器资源来给 hexo
生成静态文件,然后我就开始研究咋整…
GitHub Actions
个人感觉,GitHub Actions
就是一个自动化的工具,设置脚本,它帮你一步一步执行,脚本的具体实现,具体在 .github\workflows\whatever-name.yml
文件。(whatever-name:随便啥名…)
具体去看官方文档:GitHub Actions
理解
而用 Actions
来自动部署 hexo
,就需要理解整个过程…
我们在本地给 hexo
生成静态文件并 deploy
的命令是:
1 | hexo clean |
而我们要自动部署,就需要新建私有库(最好),把源码推到远程库,GitHub Actions
感应到 push
,就会帮我们执行这些命令,生成静态文件,deploy
到 github.io
库中。
这些命令又需要相对应的环境,所以要在之前先安装相关运行环境,如 nodejs
、hexo-cli
等。
让 GitHub
明白我们的指令,就需要上面提到的“脚本文件”:
1 | # name 随便起 |
密钥
理解了上面的整个过程,那么就会想到,在 GitHub
自己的环境下,怎么有权限推内容到我们的库呢?
在本地,我们推内容到远程仓库是需要 ssh
验证的,所以要在 GitHub
的环境下 push
内容,GitHub
环境就需要我们自己的密钥!
而密钥又不能直接写在 yml
中,这时候就需要 GitHub
仓库中的 Actions secrets
了!
就像 yml
提到的:
1 | - name: Setup Hexo |
效果
当你把更改 push
到私有库 master
分支,就会触发 Actions
,你可以在 Actions
页看到:
你还能点进去看具体的执行过程:
最后
完成上述工作,以后写博文就只需要提交更改,生成静态文件和发布的任务就交给 GitHub
!
白嫖党永不为奴