0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GCP Cloud Buildでnode_modulesに変更が無ければキャッシュしない

Last updated at Posted at 2024-02-11

以前、下記の記事でAngularプロジェクトをGCPにデプロイするのを高速化したけど、もう少し高速化する。

デプロイ後にnode_modulesをGCPのStorageに保存してるが、node_modulesを圧縮するのに時間がかかる(40秒程度)

今回は、これを何とかする

問題

冒頭で記載した通り、node_modulesの圧縮が遅い。
前回記事の最後に載せた表をもう一度記載する

ステップ 修正前 修正後
Dockerビルド 21秒 -
node_modulesダウンロード - 10秒
node_modules解凍 - 18秒
pnpm install - 29秒
yarn install 2分7秒 -
Angular build 3秒 3秒
デプロイ 32秒 44秒
node_modules圧縮 - 37秒
node_modulesアップロード - 7秒
合計 3分10秒 2分34秒

node_modules圧縮で37秒、アップロードで7秒かかってる。
全体が2分34秒なので中々の時間だ。

node_modulesに変更が無ければキャッシュしない

いつも通りChatGPTに聞いて、色々試行錯誤した結果、下記の結論になった

  • pnpm-lock.jsonのハッシュ値を取得
  • 前回のハッシュ値と比較して違う場合、node_modulesをキャッシュする

cloudbuild.yaml修正

pnpm-lock.jsonのハッシュ値を取得

md5sumコマンドでpnpm-lock.jsonのハッシュ値を取得し、current_pnpm_lock_hash.txtに保存。

そして、前回のハッシュ値をStorageから取得。
Screenshot 2024-02-11 at 14.14.42.png

前回のハッシュ値と比較して違う場合、node_modulesをキャッシュする

もし、ハッシュ値が一致するなら、そのまま終了。

一致しないならnode_modulesを圧縮してStorageに保存。
current_pnpm_lock_hash.txtもStorageに保存。

結果

TinyPNG Screenshot Feb 11.png

Step7〜9がなくなったので、3分33秒から2分37秒と1分近くCIの時間が短縮された。
※前回の記事ではデプロイが44秒で今回は1分21秒と倍近くかかってるのは不明。

コードはこちら

※一致しない場合、node_modulesを圧縮してStorageに保存する部分がちょっと間違ってました。ubuntuではなく、gcr.io/cloud-builders/gsutilを使う必要があるので、下記の対応も必要。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?