Firebase App Distribution with codemagic.yaml

How to deploy an app to Firebase App Distribution using codemagic.yaml

Note: This guide only applies to workflows configured with the codemagic.yaml. If your workflow is configured with Flutter workflow editor, please go to Publishing an app to Firebase App Distribution with Flutter workflow editor.

Requirements

For distributing an iOS application to Firebase Console, your application must use a development, Ad Hoc or Enterprise distribution profile.

To authenticate with Firebase, Codemagic requires either a Firebase token or a service account with Firebase App Distribution Admin role, as shown below:

1. Authenticating via token

To retrieve your Firebase token, follow the instructions in Firebase documentation.

  1. Open your Codemagic app settings, and go to the Environment variables tab.
  2. Enter the desired Variable name, e.g. FIREBASE_TOKEN.
  3. Enter the token value as Variable value.
  4. Enter the variable group name, e.g. firebase_credentials. Click the button to create the group.
  5. Make sure the Secure option is selected.
  6. Click the Add button to add the variable.

2. Authenticating via service account

Using a service account is a more secure option due to granular permission settings. It can also be used to authenticate with various Firebase services, such as Firebase Test Lab and Firebase App Distribution.

  1. On the Firebase project page, navigate to Project settings by clicking on the cog button. Select the Service accounts tab. Click on the X service accounts button as shown on the screenshot.

    Firebase service accounts

  2. This will lead you to the Google Cloud Platform. In step 1, fill in the Service account details and click Create. The name of the service account will allow you to identify it among other service accounts you may have created.

  3. In step 2, click the Select a role dropdown menu and choose the role. Note that Editor role is required for Firebase Test Lab and Firebase App Distribution Admin for Firebase App Distribution.

  4. In step 3, you can leave the fields blank and click Done.

  5. In the list of created service accounts, identify the account you have just created and click on the menu in the Actions column, then click Manage keys.

    Google cloud key

  6. In the Keys section, click Add Key > Create new key. Make sure that the key type is set to JSON and click Create. Save the key file in a secure location to have it available.

    Google cloud json

  7. Configure publishing in codemagic.yaml:

    1. Open your Codemagic app settings, and go to the Environment variables tab.
    2. Enter the desired Variable name, e.g. FIREBASE_SERVICE_ACCOUNT.
    3. Copy and paste the content of the service account JSON file as Variable value.
    4. Enter the variable group name, e.g. firebase_credentials. Click the button to create the group.
    5. Make sure the Secure option is selected.
    6. Click the Add button to add the variable.

Distribution to Firebase

Example configuration for publishing Android and iOS artifacts to Firebase:

publishing:
  firebase:
    # use this line to autenticate via token
    firebase_token: $FIREBASE_TOKEN
    
    # or this line to authenticate via service account
  # firebase_service_account: $FIREBASE_SERVICE_ACCOUNT
  
    android:
      # Add your Android app id retrieved from Firebase console
      app_id: x:xxxxxxxxxxxx:android:xxxxxxxxxxxxxxxxxxxxxx 
      
      # Add one or more groups that you wish to distribute your Android application to.
      # You can create groups in the Firebase console
      groups: 
        - androidTesters
        - ...

    ios:
      # Add your iOS app id retrieved from Firebase console
      app_id: x:xxxxxxxxxxxx:ios:xxxxxxxxxxxxxxxxxxxxxx 
      
      # Add one or more groups that you wish to distribute your iOS application to.
      # You can create groups in the Firebase console
      groups:
        - iosTesters
        - ...

If you wish to pass release notes with your build, create a release_notes.txt file and add it to the project working directory, which is either the repository root directory or the Project path specified in the Build section in your workflow settings. Codemagic will fetch the content of that file and publish it with the build.

In order to distribute an .aab to testers via Firebase App Distribution, your Firebase project must be linked to your Google Play account. More information is available here

Publishing only the Android app bundle or APK artifact to Firebase App Distribution

If you are building both an Android app bundle and an APK in your workflow, Codemagic will, by default, try to publish the bundle to Firebase App Distribution. If you wish to publish the APK, specify the artifact type as apk using the artifact_type field.

publishing:
  firebase:
    firebase_token: $FIREBASE_TOKEN
    android:
      app_id: x:xxxxxxxxxxxx:android:xxxxxxxxxxxxxxxxxxxxxx
      groups:
        - androidTesters
        - ...
      artifact_type: 'apk' # Replace with 'aab' to only publish the Android app bundle

Publishing to Firebase App Distribution with Fastlane

Before running a lane, you should install Fastlane Firebase app distribution plugin.

- name: Install fastlane-plugin-firebase_app_distribution
  script: | 
    gem install bundler
    sudo gem install fastlane-plugin-firebase_app_distribution --user-install

Then you need to call a lane. This code is similar for Android and iOS.

- name: Execute fastlane android publishing task
  script: | 
    cd android
    bundle install
    bundle exec fastlane <your_android_lane>
- name: Execute fastlane ios publishing task
  script: | 
    cd ios
    bundle install
    bundle exec fastlane <your_ios_lane>

Publishing an Android app to Firebase App Distribution with Gradle

To authorize an application for Firebase App Distribution, you need Google service account credentials.

  1. Download the credentials file (named similar to yourappname-6e632def9ad4.json) and copy its content.
  2. Open your Codemagic app settings, and go to the Environment variables tab.
  3. Enter the desired Variable name, e.g. GOOGLE_APP_CREDENTIALS.
  4. Paste the content of the credentials file as Variable value.
  5. Enter the variable group name, e.g. google_credentials. Click the button to create the group.
  6. Make sure the Secure option is selected.
  7. Click the Add button to add the variable.

  1. Specify the filepath in your build.gradle file under firebaseAppDistribution section as serviceCredentialsFile="your/file/path.json".
buildTypes {
    ...
    release {
        ...
        firebaseAppDistribution {
            ...
            serviceCredentialsFile="<your/file/path.json>"
        }
    }

Note that in case the credentials file is not specified in firebaseAppDistribution build type, the environment variable GOOGLE_APPLICATION_CREDENTIALS will be used instead.


  1. Decode application credentials for Firebase authorization:

scripts:
  -name: Decode Google credentials
   script: | 
     echo $GOOGLE_APP_CREDENTIALS > $CM_BUILD_DIR/your/file/path.json


  1. Build the application:
\
scripts:
  -name: Build the app
   script: | 
     echo "flutter.sdk=$HOME/programs/flutter" > "$CM_BUILD_DIR/android/local.properties"
     flutter packages pub get
     flutter build apk --release


  1. Call the gradlew task for distribution
scripts:
  -name: Distribute app to firebase with gradle plugin
   script: | 
     cd android && ./gradlew appDistributionUploadRelease

Note: If you didn’t specify serviceCredentialsFile, you may export it to a random location like /tmp/google-application-credentials.json and then export the file path on the gradlew task.

scripts:
  - name: Export the credentials file
    script: | 
      echo $GOOGLE_APP_CREDENTIALS > /tmp/google-application-credentials.json
  - name: Distribute app to firebase with gradle plugin
  script: | 
    export GOOGLE_APPLICATION_CREDENTIALS=/tmp/google-application-credentials.json
    cd android && ./gradlew appDistributionUploadRelease