利用 Github Actions Caches 来缩短打包时间

已经有三个多月没有真正的接触代码,加上自己本身就一业余选手,想要折腾一番的话就得不停地看文档,碰巧在 Astro 的官方文档上看到 Astro 有配置参考中提到的 cacheDir1,立马想到可以利用这项配置来缩短在 Github Actions 上打包的时间。

因为之前有配置过 Github Actions Caches 的经验,因此稍微修改一下触发 Github Actions 的 yml 配置文件就线上测试了。

线上测试结果满足了自己的需求,参照对比如下:

CleanShot-2024-08-20-at-15.04.01@2x

配置成功后可在 Actions -> Caches 中找到缓存列表

CleanShot-2024-08-20-at-15.27.57@2x

没有缓存时

CleanShot-2024-08-20-at-15.10.10@2x

开启缓存后

CleanShot-2024-08-20-at-15.14.09@2x

以下为个人 Github Actions 配置,供参考。

name: Deploy Astro site to Pages

on:
  # Runs on pushes targeting the default branch
  push:
    branches: ['astro']

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
  contents: read
  pages: write
  id-token: write

concurrency:
  group: 'pages'
  cancel-in-progress: false

jobs:
  build:
    name: Build Astro Blog
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - uses: pnpm/action-setup@v3
        name: Install pnpm
        with:
          version: 8
          run_install: false

      - name: Get pnpm store directory
        shell: bash
        run: |
          echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV

      - uses: actions/cache@v4
        name: Setup pnpm cache
        with:
          path: ${{ env.STORE_PATH }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-
            
      - name: Cache Astro build artifacts
        uses: actions/cache@v4
        with:
          # Astro cacheDir Path
          path: ./node_modules/.astro
          # Detects whether the image in this path has changed
          key: ${{ runner.os }}-astro-cache-${{ hashFiles('src/assets/images/**') }}
          restore-keys: |
            ${{ runner.os }}-astro-cache-

      - name: Install dependencies
        run: pnpm install

      - name: Build
        run: pnpm build

      - uses: webfactory/ssh-agent@v0.9.0
        with:
          ssh-private-key: ${{ secrets.AL_DEPLOY_KEY }}
          log-public-key: false

      - name: Scan public keys
        run: |
          ssh-keyscan ${{ secrets.AL_DEPLOY_HOST }} >> ~/.ssh/known_hosts

      - name: Copy
        run: |
          rsync -av dist/ laomaiorg@${{ secrets.AL_DEPLOY_HOST }}:/laomaiorg/project/blog/html
      
      - name: Deploy
        uses: peaceiris/actions-gh-pages@v4
        with:
          deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }}
          external_repository: laomaiorg/laomaiorg.github.io
          publish_branch: main
          publish_dir: ./dist

我博客上的图片只有 295 张,在 Github Actions 上总耗时能缩短 1 分钟。虽说,或者,看起来作用不大,但是既然可以如此操作,为什么不利用起来呢?毕竟 Github Actions 也是计算使用时长的,每月 2000分钟。当然,这额度对于普通的个人使用的话是足够了的。

Footnotes

  1. Astro Docs - cacheDir 🔗