Firebase App Distribution, Fastlane, Docker, Bitbucket Pipelines, Telegram, and all that jazz

Merab Tato Kutalia
ProAndroidDev
Published in
7 min readSep 26, 2019

--

Long-awaited Firebase App Distribution is here🔥

Firebase promo video

Firebase App Distribution is an alternative to the well-known platform Crashlytics later acquired by Google. Firebase constantly tries to fill the gaps from DevOps to the developer and to the reporting assistant, all the necessary tools are getting to appear on the platform. After the partial migration from Crashlytics to Firebase we missed beta app distribution and all the stuff around it.

I had an opportunity to enroll in the closed alpha program to gain access to the very early stages of the upcoming features and I promise there are tons of features coming out 🎉 Using Firebase is becoming fun again.

Firebase app distribution

This blog is about how I used firebase app distribution by uploading beta release APK using Fastlane and firebase-tools plugin. Created custom beta testers groups in the firebase platform. After successful upload, Fastlane will message to the Telegram group. Firebase itself will send emails to the testers separately. Now automate with Bitbucket Pipelines and Docker. We all love this word and CI/CD. Because the project I’m working on is on private Bitbucket I prefer Pipelines and everything in a single platform.

I’m going to write the whole process step by step and some difficulties I came up during set up this pipeline. While this feature was in alpha I couldn’t get any public help and it was like to search a needle in the dark + requires a little bit knowledge of each technology.

Anyways, let’s start our journey to explore the new capabilities of the Firebase platform. There are several ways to upload a build on Firebase: Gradle/Fastlane/CLI and Firebase Console itself. This post is dedicated to Fastlane tools.

The complete roadmap is shown below:

  1. Set up Fastlane with firebase app distribution tools locally
  2. Login to Firebase
  3. Set up Firebase console custom groups
  4. Set up Fastfile with custom scripts, release notes
  5. prepare and move everything to Dockerfile including Android SDK, firebase tools and Fastlane, push to docker hub
  6. setup Bitbucket Pipelines, Pipeline scripting
  7. bitbucket ENVs, Keystore files, and setup
  8. Test! 🤖
  9. Bonus: Telegram bot
How firebase is represented in a whole pipeline

Setup Fastlane with Firebase distribution tools

According to the documentation we need to install Fastlane first. This example is for Android only. Depending on the system here is very straightforward instructions on how to install Fastlane. Link to docs. we need to install Ruby and using RubyGems actual Fastlane.

After the successful installation, we need to install Firebase CLI tools using npm or directly from Github Whichever fits you. I chose to download it from Github and mark as executable if Linux environment https://github.com/firebase/firebase-tools/releases

Now we need to sign in to firebase. Because we are going to use it in the CI

firebase login:ci use this command and this will echo URL. visit the URL and authorize with the account which is the part of firebase projects. browser will redirect to the success page and in command line/terminal we will have auth token. save this for later!

if you want to logout current session forever just type :

firebase logout — token token

After setting up Fastlane run this command as the documentation states

fastlane add_plugin firebase_app_distribution this will enable the plugin.

if we use firebase tools <7.4.0 we need additional command firebase --open-sesame appdistribution because until now this feature was in closed alpha.

Setup Firebase console with custom groups

In the Firebase console, you can see app distribution on the side menu. we can specify testers and divide them into groups under “Testers & Groups” tab.

Firebase console side menu
Firebase console

Set up Fastfile with custom scripts, release notes

here is an example of Fastfile with some custom scripting. We pass group-name alias(can be found in Firebase App distribution panel from the previous topic) as the parameter. also not to edit our Fastfile after every beta release with the release notes, we can write down all the updates in the txt file and reference it in our script. The script also needs app id which can be retrieved from Firebase project settings page.

this actual script builds release android app with the flavor called prod. you can change it to whatever you want and configure. keystore file and password are everything configured in project gradle file.

Fastfile

use like this fastlane distribute group:only-me

Test this and it should work and upload a beta build. This is a standard setup. but we want it on CI.

If there is some problem everything can be found in the documentation.

moving on to the

Prepare and move everything to Dockerfile including Android SDK, firebase tools and Fastlane

Dockerfile

In this case, I use Linux alpine, the most lightweight Linux and add the tools I need manually.

if you want to learn Docker a little bit more follow this link: https://docs.docker.com/get-started/. anyways for now here is the Dockerfile and we need to build this and push to Dockerhub. Bitbucket pipelines will fetch it from docker hub. It’s almost the same as Github for code but for Dockerfile-s. repository can be a private or public. Register first, you will need it anyways https://hub.docker.com/

To create a Docker image build using the provided file, tag it and upload. https://docs.docker.com/get-started/part2/.

you can also pull my image for pipeline https://cloud.docker.com/repository/docker/tatocaster/fastlanedocker

Set up Bitbucket Pipelines, pipeline scripts

Bitbucket Pipeline is a part of the main bitbucket project, embedded CI/CD tool and quite easy to use. To set up pipelines enable from project settings, it needs admin permission to do so. Basically pipeline fetches fresh source code from version control, puts it in the docker container and in this scope you can do any operations you want. No need to configure additional docker inside the pipeline, you are in a docker container already.

Pipelines are configured using .yml files and it needs to be in a root directory of the project.

this is an example of bitbucket-pipelines.yml file

bitbucket-pipelines.yml

to learn more about pipeline scripting visit: https://confluence.atlassian.com/bitbucket/configure-bitbucket-pipelines-yml-792298910.html

The given script pulls, in this case, my docker image from docker hub and runs it. The pipeline runs on the development branch. by default is master, but I prefer beta distributions on the development branch and features on feature branch as a normal git-flow, but this is out of the context of the post. Caches gradle and other directories which are described at the end of the file run the script in order and outputs an APK as an artifact as a bonus. Quite straightforward instructions.

Bitbucket ENVs, Keystore files, and setup

1 thing to discuss you probably noticed it is variable $KEYSTORE. you are right. I converted my production Keystore file as base64, stored it in bitbucket env variables and in the pipeline I decode it again. this is the safest way to provide credentials to the Gradle build system.

Remember when we logged in to firebase and we got the token? We need it now. Store it as a bitbucket repository variable FIREBASE_TOKEN and firebase-tools will automatically fetch it from env.

gradle config

This Gradle config is the key how release app can be built from the local pc + from the CI server. using Groovy scripting we can check if there is an env variable in the system or get it from the properties local file. While the code will be in the pipeline, docker container, it can not access local env file so it will try to fetch it from the env variables.

at this state, we are good to go. we can commit a change to a development branch and our pipeline should start automatically. Notice, first run is the slowest because there is no cache. Bitbucket cache lifetime is 1 week.

Bonus: Telegram Bot

Create telegram bot, easy as ABC. https://core.telegram.org/bots#3-how-do-i-create-a-bot

Install this as a Fastlane plugin and configure Fastfile, basically, just add a telegram part and it will send to a specific chat, just include the token for the telegram bot. Better if stored in env variables. Add the bot to your group chat if you want to send messages to the group or leave it.

Fastfile
initial sketch of the flow

That's it! Congratulations 🎉 Now you have a nice CI with all the fancy techs on the market and super configurable.

👉 Follow me on Twitter and check my other articles.

--

--