本文最后更新于 2022-03-03T14:46:35+08:00
                  
                  
                
              
            
            
              
                
                
1 问题的提出
谷月姐的博客(https://blog.kukmoon.com,以下简称 本博)是用 Hexo 搭建的,并且通过 Git 部署到采用了 cPanel 面板的虚拟主机。
每次用 hexo d 部署博客时,Hexo 会将上一次提交作为对象文件,写入远程主机的 .git/objects 目录,这样,.git/objects 目录的体积以几何级数增长,很快就吞掉了大多数磁盘空间。
2 用 GitHub Actions 来解决
在上一篇文章中,谷月姐用三个脚本解决了这个问题。这三个脚本分别是:
- 脚本 1:
rebuild_repo.sh 在远程主机上重建博客目录并初始化为 Git 仓库; 
- 脚本 2:
delete_obj.sh 在远程主机上删除博客目录中的 .git/object/ 子目录中的所有内容。 
- 脚本 3:
deploy.sh 在本地执行,它先将 rebuild_repo.sh 上传到远程主机并执行,再执行 hexo clean 和 hexo d -g 部署博客,最后将 delete_obj.sh 上传到远程主机并执行。 
上述解决方案适合在本地发布。但是 本博 是把源码放到 GitHub 的,在本地写完博文,推送到 GitHub,由 GitHub Action 在每次推送时完成部署工作。因此,谷月姐需要修改 GitHub Action workflow,让它在部署博客之前执行脚本 1,在部署博客之后执行脚本 2。
参照这篇文章,先把脚本 1 rebuild_repo.sh 和脚本 2 delete_obj.sh 复制到博客源文件所在目录的 .github/script/ 子目录(如果没有,就新建一个),然后在博客源文件所在目录的 .github/workflows/deploy.yml (即已有的负责自动部署博客的工作流脚本)中编写以下代码。
注意:把脚本中的 kukmoon 改成你的虚拟主机的登录用户名,把 999.999.999.999 改成你的虚拟主机的 IP 地址。
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
   | name: CI
 
  on:   push:     branches:       - master
 
  env:   GIT_USER: Kukmoon     GIT_EMAIL: [email protected]  
  jobs:   build:     name: Build on node ${{ matrix.node_version }} and ${{ matrix.os }}     runs-on: ubuntu-latest     strategy:       matrix:         os: [ubuntu-latest]         node_version: [12.x]  
      steps:              - name: Checkout         uses: actions/checkout@v2                     - name: Use Node.js ${{ matrix.node_version }}         uses: actions/setup-node@v1         with:           node-version: ${{ matrix.node_version }}
               - name: Install hexo with dependencies               run: |           npm install -g hexo-cli
               - name: Install hexo with dependencies               run: |           npm install
                             - name: Configuration environment         env:           HEXO_DEPLOY_PRI: ${{secrets.HEXO_DEPLOY_PRI}}         run: |           sudo timedatectl set-timezone "Asia/Shanghai"           mkdir -p ~/.ssh/           echo "$HEXO_DEPLOY_PRI" > ~/.ssh/id_rsa           chmod 600 ~/.ssh/id_rsa           ssh-keyscan github.com >> ~/.ssh/known_hosts                     ssh-keyscan 999.999.999.999 >> ~/.ssh/known_hosts           git config --global user.name $GIT_USER           git config --global user.email $GIT_EMAIL
               - name: Rebuild remote repo         run: |           scp ${GITHUB_WORKSPACE}/.github/script/rebuild_repo.sh [email protected]:/tmp           ssh [email protected] 'chmod +x /tmp/rebuild_repo.sh;/bin/sh /tmp/rebuild_repo.sh;rm /tmp/rebuild_repo.sh' 
               - name: Deploy hexo         run: |           hexo clean           hexo g -d
               - name: Update Blog         run: |           sh "${GITHUB_WORKSPACE}/.github/script/blog-update.sh"
               - name: Delete remote repo's objects         run: |           scp ${GITHUB_WORKSPACE}/.github/script/delete_obj.sh [email protected]:/tmp           ssh [email protected] 'chmod +x /tmp/delete_obj.sh;/bin/sh /tmp/delete_obj.sh;rm /tmp/delete_obj.sh'
 
 
  | 
 
3 搞定
推送到 GitHub,Actions 自动执行,结果如图所示。

4 图片版权
头图:Banner Image by milkusmaximus from Pixabay
5 参考文献