Automating Flutter Publishing with GitHub Actions & Firebase App Distribution

Zvi Karp
Flutter Community
Published in
5 min readJun 4, 2020

One of the most important parts of freelancing is engaging the client in your development process. This tutorial will show you how to automatically send new versions of your Flutter app to your client with Firebase App Distribution and GitHub Actions.

Before You Start

  • You’ll need a basic knowledge of Flutter, Git, and GitHub 💪.
  • To follow this tutorial you need to have a Flutter project using Firebase. To create one, take a look at the Flutter Documentation and Firebase Documentation.

Note: This tutorial is Windows and Android-based. It hasn't been tested on any Apple devices.

The tutorial is divided into three parts: first, use Firebase App Distribution, then set up GitHub Actions, and lastly, merge the two.

Part 1: Firebase App Distribution

Firebase App Distribution allows you to share your new app release with groups of testers as quickly as sending an email! No more waiting for days for it to be approved on the Google Play Store. The reasons we use Firebase App Distribution are because it’s free to use, easy to set up, a “set and forget” process, and, most importantly, easy for your client to use. As we know, clients often have very little or no technical background, and you don’t want to send them apk files manually.

Let’s use Firebase App Distribution in an existing project:

  1. Build an apk from your Flutter project by running flutter build apk in the command line.
  2. Open your Firebase console and navigate to App Distribution > Get started > Testers & Groups.
  3. Let’s add a group called “testers”, and add your email as a tester to the group.
  4. Now switch back to the “Releases” tab and upload your app.
  5. Add the group “testers” as testers and follow the instructions.
  6. The last step is to check your email and follow the instructions.

Now you have a way to send your apk to a group of testers, but it needs to be done manually, and we are programmers, so let’s automate it!

Part 2: GitHub Actions

GitHub Actions is a CI/CD automation tool built into your GitHub workflow. It allows you, for example, to send automated messages to new contributors in your repo, or test your application before releasing it to production. It’s free for public repositories and some free computing minutes per month are included for private repositories. See GitHub Pricing for more details.

In this part, we create an action that attempts to build your application on every commit or pull request. We create a new branch “dev”, and every time we push from “dev” to “master”, GitHub builds your app and notifies you if it fails. The catch here is that we have some secret files that we don’t want to upload to GitHub (e.g. the google-services.json file for Firebase). These files are listed in the .gitignore file. So first, we need to encrypt the secret files and only then upload them to GitHub and provide GitHub with the keys to decrypt them. This is how to do it:

  1. Assuming that you have more than one secret file, we are going to combine all the secret files into one .tar file. In the command line, in your working directory, run the following command and make sure to update the list of secret files you want to combine in the .tar. You can add as many files as you want.
    tar cvf secrets.tar android/app/google-services.json path/to/another-secret-file.
  2. To encrypt the .tar install GnuPG and then run the command gpg -c secrets.tar. Enter a password in the GPG window. Make sure you remember it — there’s no way to recover it! Now you’ll find a new file secrets.tar.gpg in your directory that is encrypted with the password you entered.
  3. Make sure to add the secrets.tar to the .gitignore file.
    IMPORTANT ❗ : You don't want to upload your secret files to GitHub!
  4. Open GitHub, go to the repository, and navigate to Settings > Secrets > New Secret. Enter “SECRET_PASSPHRASE” as the name, and the password from step 2 as the value.
  5. Back to your Flutter app, add the file .github/workflows/main.yml, we are going to create our GitHub Action in it.
  6. Add the following to the main.yml file:

Now what’s left is to push to GitHub and check if the action passes. It might take a while for the action to run. You can see the progress by going to the
GitHub “Actions” tab.

Part 3: Firebase App Distribution with GitHub Actions

By now we have all the components working and tested 😎, the last step is to combine them. We extend the current GitHub Action so that, on a pull request to the “master” branch, it uploads the new version of the app to Firebase App Distribution.

First, we need to give GitHub Actions authentication information and permissions to your Firebase account.

  1. Create a Firebase token by running firebase login:ci in the command line.
  2. Open GitHub and go to the repository. Navigate to Settings > Secrets > Add Secret. Enter “FIREBASE_TOKEN” as the name, and the token created in step 1 as the value.
  3. Go to the settings in your Firebase console and copy the App ID. Add it as a secret to GitHub with “FIREBASE_ANDROID_APPID” as the name, and the ID as the value.

All that’s left is to update the GitHub Action. Add the following lines to the end of main.yml. You can do this in the “dev” branch and then push your changes to GitHub.

OK, we’re all set! Create a pull request from “dev” to “master” and you can see the state of the action in the pull request. After it finishes, you should get an email from Firebase with the new version of the app 🤯. Now that you know it works, add the client’s email to the testers group in Firebase App Distribution so they get an email the next time the action runs.

--

--