Automatically Deploy Flutter Web Project to Firebase Hosting with Codemagic CI/CD

1k views Asked by At

How can I use CodeMagic.io to automatically deploy my Flutter web application builds to firebase hosting?

Currently the only options that appear to be supported are Codemagic Static Pages and Amazon S3 Buckets. Unless a custom build script is used.

I have done some research on the custom build script option and there is not much information online pertaining to automatically deploying to firebase hosting. Although I suspect you may be able to modify provided example in the Codemagic distribution documentation ('Publishing an app using Firebase CLI' [https://docs.codemagic.io/publishing-yaml/distribution/]) to achieve this. See code extract below.

- name: Publish the app to Firebase App Distribution
  script: |
    apkPath=$(find build -name "*.apk" | head -1)

    if [[ -z ${apkPath} ]]
    then
      echo "No apks were found, skip publishing to Firebase App Distribution"
    else
      echo "Publishing $apkPath to Firebase App Distribution"
      firebase appdistribution:distribute --app <your_android_application_firebase_id> --groups <your_android_testers_group> $apkPath
    fi

Does anyone know how I can write a custom script that achieves the desired outcome?

3

There are 3 answers

2
Mikhail Tokarev On

before deploy from CI/CD service ensure you completed following steps:

  1. you have created firebase project via firebase init
  2. you have added firebase.json to your repository
  3. make sure you have firebase token (https://firebase.google.com/docs/cli#cli-ci-systems)

The next step is configuring CI/CD workflow

  1. add FIREBASE_TOKEN to environment variables (check https://docs.codemagic.io/flutter/env-variables/ if you use Codemagic UI configuration)
  2. add this script to your post-build script
#!/bin/sh

cd $FCI_BUILD_DIR
firebase deploy -m 'my comment'
0
Arnold Veltmann On

as per my e-mail

Firebase CLI is installed to Codemagic machines by default, thus like Mikhail said as well, you will have to add FIREBASE_TOKEN to your environment variables, please encrypt it first with Codemagic UI.

You can obtain the token by running login:ci in your local console. Then after the build step you will just have to run firebase deploy --token "$FIREBASE_TOKEN" (either in the post-build script in the UI, or add it to your .yaml) in the root of your project, where Codemagic is at by default.

0
Leonardo Rignanese On

Following step by step this guide made me successfully configure the deployment on Firebase Hosting from Codemagic.

It's important to run the command firebase config in the root of your Flutter project (using build/web as a public directory) so that firebase.json and .firebaserc are created.