Hexo + Github Action部署博客

本文首发地址\](https://hubojing.github.io/2023/11/16/Hexo+GithubAction%E9%83%A8%E7%BD%B2%E5%8D%9A%E5%AE%A2/ ## 更换部署方式的原因 (不关心的朋友可以跳过这一节)   以前许多年一直是`hexo clean && hexo g && hexo d`一键部署,这样导致每台机器上都需要配置环境,以前还挺喜欢折腾这些,写过[Hexo多机同步](https://hubojing.github.io/2017/11/19/hexo%E5%A4%9A%E6%9C%BA%E5%90%8C%E6%AD%A5/) ,但是通过篇幅可以看出,太麻烦,不符合工作后能够快速更换设备的需求。近年来我逐渐注意到CI/CD的方法,先是在Gitlab上体验了一把,确实好用,后来某天惊喜发现Github出了官方的Github Action,于是有了本文的尝试。   更换后,换机再也不用安环境了,只需要把源文件项目`git clone`一下,写文,在`git add .`、`git commit -m 'update'`、`git push`三部曲就好啦! ## 两个项目 源文件 user/blog-source (privare)   前端显示 username/username.github.io (public) ## 两把钥匙 在源文件项目git bash ssh-keygen -f blog-deploy-key 生成 * 私钥 `blog-deploy-key` * 公钥 `blog-deploy-key.pub` 配置: * 源文件项目 settings - Secrets and variables - Actions 放入私钥 * 前端展示项目 settings - Deploy keys 放入公钥 ## workflow 创建相关文件夹和文件,源文件根路径下blog/.github/workflows/deploy.yml   参考模板在这里https://github.com/marketplace/actions/hexo-action # Deploy hexo blog website. - name: Deploy id: deploy uses: sma11black/hexo-action@v1.0.3 with: deploy_key: ${{ secrets.HEXO_DEPLOY_PRI }} user_name: username # (or delete this input setting to use bot account) user_email: your email # (or delete this input setting to use bot account) PUBLISH_REPOSITORY: username/username.github.io BRANCH: master PUBLISH_DIR: ./public commit_msg: ${{ github.event.head_commit.message }} # (or delete this input setting to use hexo default settings) ## 关于主题的配置 这一部分写的人太少,疯狂采坑,我来详细说一下。   主题是作为submodule加入的,但是配置如果不想被看到的话,最好单独写成文件,具体看这里:   但是单独配置文件的写法需要hexo5.0以上才能实现,indigo主题才3.0+,所以最简单的方法是fork原主题后,设置为private。   要访问private项目是需要授权的。 ### 具体做法 * Github-Settings-Developer settings-Personal access tokens-Tokens(classic)-Generate new token(classic) * 复制该token * 在源文件项目blog-source中在源文件项目blog-source中:Settings-Secrets and variables-Actions-New repository secret,粘贴该token * 在workflow的配置文件中写 with: token: ${{ secrets.PERSONAL_TOKEN }} submodules: true ## 完整的deploy.yml name: Deploy on: [push] jobs: build: runs-on: ubuntu-latest name: A job to deploy blog. steps: - name: Checkout uses: actions/checkout@v1 with: token: ${{ secrets.PERSONAL_TOKEN }} submodules: true # Checkout private submodules(themes or something else). # Caching dependencies to speed up workflows. (GitHub will remove any cache entries that have not been accessed in over 7 days.) - name: Cache node modules uses: actions/cache@v1 id: cache with: path: node_modules key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} restore-keys: | ${{ runner.os }}-node- - name: Install Dependencies if: steps.cache.outputs.cache-hit != 'true' run: npm ci # Deploy hexo blog website. - name: Deploy id: deploy uses: sma11black/hexo-action@v1.0.3 with: deploy_key: ${{ secrets.HEXO_DEPLOY_PRI }} user_name: username # (or delete this input setting to use bot account) user_email: your email # (or delete this input setting to use bot account) PUBLISH_REPOSITORY: username/username.github.io BRANCH: master PUBLISH_DIR: ./public commit_msg: ${{ github.event.head_commit.message }} # (or delete this input setting to use hexo default settings) # Use the output from the `deploy` step(use for test action) - name: Get the output run: | echo "${{ steps.deploy.outputs.notify }}" ## 无伤大雅的缺点 这个应该是git操作的哪个部分出问题了,导致`git push`后前端项目历史push记录丢失了,损失就是github页的绿点没了,其它的没什么影响,毕竟前端页面的渲染结果没源文件有用。`git reflog`只剩下最新的一条,历史的都丢失了,不过不影响博客就算了。这个问题这些年也遇到好几次了,如果有大佬遇到过一样的问题,如愿赐教,将非常感谢。 ## 参考 [Hexo\&github action持续部署](https://www.cnblogs.com/lastkiss/p/16955696.html) [Using Git Submodules for Private Content](https://www.taniarascia.com/git-submodules-private-content/) [github action 部署 hexo踩坑记录](https://zhuanlan.zhihu.com/p/626270948) [Github Actions: submodule 下公私有仓库授权和通信](https://zhuanlan.zhihu.com/p/408319831) [利用 Github Actions 自动部署 Hexo 博客](https://sanonz.github.io/2020/deploy-a-hexo-blog-from-github-actions/)

相关推荐
DeepSeek-大模型系统教程3 小时前
推荐 7 个本周 yyds 的 GitHub 项目。
人工智能·ai·语言模型·大模型·github·ai大模型·大模型学习
ai小鬼头6 小时前
AIStarter最新版怎么卸载AI项目?一键删除操作指南(附路径设置技巧)
前端·后端·github
不午睡的探索者14 小时前
告别性能瓶颈!Python 量化工程师,进击 C++ 高性能量化交易的“必修课”!
c++·github
光溯星河14 小时前
【实践手记】Git重写已提交代码历史信息
后端·github
独立开阀者_FwtCoder15 小时前
URL地址末尾加不加 "/" 有什么区别
前端·javascript·github
独立开阀者_FwtCoder15 小时前
Vue3 新特性:原来watch 也能“暂停”和“恢复”了!
前端·javascript·github
寻月隐君15 小时前
告别 Vec!掌握 Rust bytes 库,解锁零拷贝的真正威力
后端·rust·github
inhere19 小时前
gookit/goutil v0.7.0 新版本发布:模块调整与功能增强
开源·go·github
用户6210245309219 小时前
React:当UI开发从「搬砖」变成「搭乐高」的魔法时刻!
github
用户39183841721119 小时前
Jupyter Notebook:不只是个笔记本,而是你的科学计算驾驶舱!
github