以下の記事を参考に、「GitHubで作成したFlutterリポジトリでPull Requestを作成した時にBitrise経由でFirebase Test LabでIntegration Testを行う方法」について調べたので、その備忘録です。
はじめに
- Firebase Test LabにapkファイルをUploadし、そこでIntegration Testするために必要なGoogle Cloud ConsoleとBitriseの設定についてのお話です
- iOSについて言及しません
- FlutterのIntegration Testについて言及しません
- Android用の環境構築(
android/app/src/androidTest/java/YOUR_APP_PACKAGE_NAME/MainActivityTest.java
とapp/build.gradle
の追記箇所)は参考記事通りで問題ないので割愛します
前提
- Firebase・Bitriseプロジェクト作成済み&Flutterリポジトリと連携済み
Google Cloud ConsoleとBitriseの設定
Google Cloud Console
- サービスアカウントを作成し「編集者」権限を付与
- 権限がないと以下のエラーになる
ERROR: (gcloud.firebase.test.android.run) Unable to access the test environment catalog: ResponseError 403: Not authorized for project [REDACTED]
- 権限がないと以下のエラーになる
- Cloud Tool Results APIを有効化
- Credentialsファイル(JSON)をダウンロード
- 「IAMと管理」→「サービスアカウント」→作成したサービスアカウントを選択→「鍵を追加」→「新しい鍵を作成」→JSONを指定し作成
- サービス アカウント キーを管理するためのベスト プラクティス
Bitrise
FirebaseのプロジェクトIDをSecret($FIREBASE_PROJECT_ID
)に保存
参考記事ではReplace variables in inputs?
をtrueにしていますが、後から上書きする予定がなければfalseにしておきましょう。また、PR作成時などに実行するWorkflowの場合はExpose for Pull Requests?
をtrueにしないと、後述するScript Stepで取得できません。
Expose for Pull Requestsについて
You can decide which secrets should be exposed for / available in Pull Request builds.Be careful, exposing a secret is a potential security risk.
どのシークレットを公開するか/Pull Requestビルドで利用できるようにするかを決めることができます。シークレットを公開することは潜在的なセキュリティリスクとなるので注意してください。
CredentialsファイルをBitriseにUpload
参考記事ではGoogle Cloud ConsoleからダウンロードしたCredentialsファイル(JSON)もコピペしてSecretsに直書きしていると思われますが、やり方が分からなかったので「File Downloder」Stepを使用します。
手順
- 「Code Signing&Files」タブを開き「GENERIC FILE STORAGE」にCredentialsファイルを追加する
- 注意:「File Storage ID」を入力しないと、UploadするFileを選択できない(今回は
FIREBASE_TEST_LAB_SERVICE_ACCOUNT_JSON_KEY
と入力します。これで$BITRISEIO_FIREBASE_TEST_LAB_SERVICE_ACCOUNT_JSON_KEY_URL
でファイルダウンロードURLを取得できます。)
- 注意:「File Storage ID」を入力しないと、UploadするFileを選択できない(今回は
- 「Flutter Build」Stepよりも前に「File Downloder」Stepを追加
- 「File Downloder」Stepで以下の項目を入力する
- Download source URL:
$BITRISEIO_FIREBASE_TEST_LAB_SERVICE_ACCOUNT_JSON_KEY_URL
- Download destination path:
./gcloud_key_file.json
- ファイル名やパスは何でも良いが後述するScript Stepと揃える
- Download source URL:
Flutter Build Stepの後にScript Stepを追加
タイトルは分かりやすく「Firebase Test Lab」
#!/usr/bin/env bash # fail if any commands fails set -e # debug log set -x #1. Install gcloud CLI curl https://sdk.cloud.google.com | bash source $HOME/google-cloud-sdk/path.bash.inc gcloud version #2. Prepare The service account json for auth #3. Authenticate gcloud CLI with firebase and select the firebase project gcloud auth activate-service-account --key-file=gcloud_key_file.json gcloud --quiet config set project $FIREBASE_PROJECT_ID #4. Build the Instrumentation Test pushd android ./gradlew app:assembleAndroidTest ./gradlew app:assembleDebug -Ptarget="integration_test/main_test.dart" popd #5. Run it on Test Lab using gcloud CLI. gcloud firebase test android run \ --type instrumentation \ --app "$BITRISE_SOURCE_DIR/build/app/outputs/apk/debug/app-debug.apk" \ --test "$BITRISE_SOURCE_DIR/build/app/outputs/apk/androidTest/debug/app-debug-androidTest.apk" \ --timeout 30m
Workflowを走らせればFirebase Test LabにapkがUploadされIntegration Testが実行されるはずです。
その他エラー
Flutter Build Stepを追加していなかったため以下のエラーが発生
+ ./gradlew app:assembleAndroidTest /var/folders/sj/_4jj239s3j33w5w1bdh98yj80000gn/T/bitrise432719702/step_src/._script_cont: line 17: ./gradlew: No such file or directory exit status 1
./gradlewはgitignoreになっているので、GitHub上に無い