Github ActionsでAstroの画像アセットをキャッシュしたい
ブログを書くようになり、写真の数が増えてきました。
Astroを何も考えずそのままデプロイするワークフローにしていたため、デプロイ時に毎回全ての画像をwebpにコンバートしており、この時間がボトルネックになってきました

現状でも 300ファイルほどを毎度変換しており、80秒も時間を使っています
04:20:39 ✓ Completed in 79.68s.
ワークフローにキャッシュ確認を追加してみる
公式ドキュメントによると、デフォルトのキャッシュディレクトリは ./node_modules/.astroとのこと
https://docs.astro.build/en/guides/images/
The default cache directory is
./node_modules/.astro, however this can be changed using thecacheDirconfiguration setting.
このディレクトリをキャッシュするようにしてみます
- name: cache .astro
uses: actions/cache@v4
with:
path: |
node_modules
node_modules/.astro/assets
key: ${{ runner.OS }}-node-astro-assets-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.OS }}-node-astro-assets-
workflow更新直後のビルドでは、以前のキャッシュがまだ存在しないためキャッシュヒットしませんが
Run actions/cache@v4
Cache not found for input keys: Linux-node-astro-assets-5ac518ce41c045791efdd0e61f85395e245b4272a8b98a9fa44667a2c527c26a, Linux-node-astro-assets-
2度目のビルドからキャッシュヒットするようになってくれました
Run actions/cache@v4
Cache hit for: Linux-node-astro-assets-5ac518ce41c045791efdd0e61f85395e245b4272a8b98a9fa44667a2c527c26a
Received 4194304 of 94715956 (4.4%), 4.0 MBs/sec
Received 94715956 of 94715956 (100.0%), 48.6 MBs/sec
Cache Size: ~90 MB (94715956 B)
/usr/bin/tar -xf /home/runner/work/_temp/3b7ef7e8-8d19-4457-8883-828fcb2565f3/cache.tzst -P -C /home/runner/work/blog/blog --use-compress-program unzstd
Cache restored successfully
Cache restored from key: Linux-node-astro-assets-5ac518ce41c045791efdd0e61f85395e245b4272a8b98a9fa44667a2c527c26a
おしまい

現状300ファイルの変換で80秒ほどかかっていた時間がなくなり
1分ほどでデプロイできるようになりました :)