Firebase FunctionsデプロイでArtifact Registryのクリーンアップポリシー設定エラーに遭遇した話
はじめに
Firebase Functions に久しぶりにデプロイをしたら、エラーが発生してしまいました。
Error: Functions successfully deployed but could not set up cleanup policy in location asia-northeast1. Pass the --force option to automatically set up a cleanup policy or run 'firebase functions:artifacts:setpolicy' to manually set up a cleanup policy.
今回は、このエラーの原因と解決方法について解説します。
エラーの原因
このエラーは、Artifact Registry のクリーンアップポリシーが設定されていないことが原因のようです。先日あった Container Registry の廃止に関係しているのかもしれません。
解決策
エラーメッセージの中で、解決策として以下の 2 つが提案されています。
-
---force
オプションを使って、自動的にクリーンアップポリシーをセットアップする。 -
firebase functions:artifacts:setpolicy
コマンドを実行して、手動でクリーンアップポリシーを設定する。
GitHub Actions でデプロイさせている都合上、Workflow ファイル内で--force
オプションを適用したくなかったため、後者を選びました。
firebase functions:artifacts:setpolicy コマンドの実行
しかし、いざfirebase functions:artifacts:setpolicy
コマンドを実行してみると、
$ firebase functions:artifacts:setpolicy
Error: functions:artifacts:setpolicy is not a Firebase command
「そんなコマンドありませんぜ」とつれない返事が来ました。
これは、インストールされているfirebase-tools
が古いからです。以下のようにして、最新のfirebase-tools
をインストールします。
$ npm install -g firebase-tools@latest
その後、firebase functions:artifacts:setpolicy
コマンドを実行することができるようになります。ただし、このコマンドを実行する際には、ロケーションを指定します。asia-northeast1
を対象にするのであれば、以下のようにコマンドを実行します。
$ firebase functions:artifacts:setpolicy --location asia-northeast1
以下のような感じで設定が適用されます。
$ firebase functions:artifacts:setpolicy --location asia-northeast1
i You are about to set up a cleanup policy for Cloud Run functions container images in location asia-northeast1
i This policy will automatically delete container images that are older than 1 days
i This helps reduce storage costs by removing old container images that are no longer needed
? Do you want to continue? Yes
✔ Successfully set up cleanup policy that deletes images older than 1 days
i Cleanup policy has been set for projects/.../locations/asia-northeast1/repositories/gcf-artifacts
これにより、デプロイ時に Artifact Registry に格納されてから 1 日経過すると自動的に削除されるように設定されます。
まとめ
Firebase Functions のデプロイで Artifact Registry のクリーンアップポリシー設定エラーが発生した場合、最新のfirebase-tools
をインストールし、firebase functions:artifacts:setpolicy --location <location>
コマンドを実行することで解決できます。
この記事が、皆様のお役に立てば幸いです。