1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Codemagicで`flutterfire configure`を実行する🐱

Last updated at Posted at 2025-07-26

要約

FlutterアプリのFirebase設定ファイルをgitリポジトリに含めずに、Codemagicでflutterfire configureを自動実行してセキュアなCI/CDを実現する方法を解説。

はじめに

Flutterアプリ開発でFirebaseを使う際、GoogleService-Info.plistgoogle-services.jsonfirebase_options.dartなどの設定ファイルをgitリポジトリに含めるべきか悩んだことはありませんか?

これらの設定ファイルにはAPIキーなどの機密情報が含まれており、git管理に入れるとセキュリティリスクになります。一方で、CodemagicなどのCI/CD環境では自動ビルドのために設定ファイルが必要です。

この記事では、Firebase設定ファイルをgitから完全に除外しながら、Codemagicで自動的にflutterfire configureを実行してセキュアなCI/CDを実現する方法を紹介します。

対象読者

  • FlutterでFirebaseを使用している人
  • CodemagicでCI/CDを構築したい人
  • Firebase設定ファイルの管理に悩んでいる人

解決したい課題

従来の問題点

  • Firebase設定ファイルをgitに含めると機密情報が公開リポジトリで露出する
  • Codemagicでのビルド時にFirebase設定ファイルが不足してビルドが失敗する
  • チーム開発でFirebase設定ファイルの共有方法に悩む

この記事で実現すること

  • Firebase設定ファイルをgitから完全に除外
  • Codemagicで自動的にFirebase設定を生成してセキュアなCI/CDを実現

FlutterFire CLIとは

FlutterFireは、FlutterアプリをFirebaseに接続するプラグインセットです。FlutterFire CLIは、FlutterプロジェクトのFirebase設定を自動化するコマンドラインツールです。

flutterfire configureコマンドの役割

このコマンドは、Firebase プロジェクトの設定情報を取得し、以下のファイルを自動生成します:

  • firebase_options.dart - プロジェクト固有の設定情報
  • プラットフォーム固有の設定ファイル(GoogleService-Info.plistgoogle-services.jsonなど)

開発メンバーは各自のローカル環境でflutterfire configureを実行することで、開発用のFirebase設定ファイルを生成することができます。

実装手順

1. .gitignoreの設定

まず、Firebase関連のファイルを全てgit管理から除外します。

.gitignore
# Firebase
GoogleService-Info.plist
google-services.json
firebase_options*.dart

既存のプロジェクトでこれらのファイルがgitに含まれている場合は、git rm --cachedで一度削除してからcommitしてください。

2. Firebaseサービスアカウントキーの準備

サービスアカウントキーの取得

  1. Firebase Console → プロジェクト設定 → サービスアカウント
  2. 「新しい秘密鍵の生成」をクリック
  3. JSONファイルをダウンロード

Base64エンコード

サービスアカウントキーファイルはCodemagic上に直接配置できないため、Base64エンコードして環境変数として設定します。

ローカル環境で以下のコマンドを実行してエンコードします。

# macOS/Linux
base64 -i path/to/firebase-service-account.json

# Windows
certutil -encode path/to/firebase-service-account.json encoded.txt

エンコード結果は改行を含まない一行の文字列として保存してください。

3. CI環境での環境変数設定

Codemagicでの設定例

  • プロジェクト → Settings → Environment variables
  • 以下の環境変数を追加:
FIREBASE_SERVICE_ACCOUNT_BASE64: [base64エンコードした内容]

環境変数は必ず「Secure」にチェックを入れて、ログに出力されないようにしてください。

image.png

4. CI実行スクリプトの実装

CodemagicのPre-build scriptに以下を記述します:

echo $FIREBASE_SERVICE_ACCOUNT_BASE64 | base64 --decode > firebase-service-account.json
export GOOGLE_APPLICATION_CREDENTIALS="firebase-service-account.json"

dart pub global activate flutterfire_cli
flutterfire configure --project=your-project-id --platforms=android,ios --yes

your-project-idの部分は実際のFirebaseプロジェクトIDに置き換えてください。

スクリプトの詳細解説

ポイント1: --yesフラグ

対話式プロンプトをスキップして、CI環境での自動実行を可能にします。

ポイント2: GOOGLE_APPLICATION_CREDENTIALS

The FlutterFire CLI depends on the underlying Firebase CLI.
CLI | FlutterFire

FlutterFire CLIは内部でFirebase CLIに依存しており、Firebase CLIがGOOGLE_APPLICATION_CREDENTIALS環境変数を使用してサービスアカウントキーファイルの場所を特定します。そのため、CI環境でFlutterFire CLIを動作させるには、この環境変数の設定が必要です。

実行結果

このスクリプトを実行すると、以下のファイルが自動生成されます:

  • lib/firebase_options.dart
  • android/app/google-services.json(Androidの場合)
  • ios/Runner/GoogleService-Info.plist(iOSの場合)

生成されたファイルは一時的なものなので、CI実行後は自動的に削除されます。

まとめ

この方法により、以下のメリットを得られます:

  • セキュリティ向上: Firebase設定ファイルをgitリポジトリから完全に除外
  • 運用効率化: CodemagicでのFirebase設定の自動生成
  • チーム開発の簡素化: 機密情報を意識せずに安全にCI/CDが可能

参考

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?