Automated android deployments with Fastlane 🚀 and Firebase app distribution

Clement Ozemoya
5 min readFeb 23, 2020
Photo by SpaceX on Unsplash

The code-compile-deploy process can be pretty hectic and take long periods of waiting for an average Android developer. When it comes to deploying your shiny new feature to test environments, the developer often has to build, test, generate an executable file (apk) and manually upload the apk by using Firebase App Distribution, this requires a number of steps and a series of wait-time at intervals.

In this article, I will be talking about speeding up the process of deploying beta builds to testers with a run of a command.

Getting Started with Fastlane

Fastlane is an open-source app automation platform that helps in automating development and release processes. It works for both Android and iOS platforms and is open source so you can check them out.

You can automate tasks like:

  • Distributing beta builds to appropriate testers
  • Screenshots generation for your app’s store listing
  • Publishing a new release to the app store
  • Code signing

Automating beta build distribution

Let’s focus on making our beta distribution processes a bit quicker. Then we can look at improving every other step of our deployment pipeline.

Installing fastlane

Install the latest Xcode command-line tools

xcode-select — install

Install fastlane

# Using Homebrew
brew install fastlane
# Alternatively using RubyGems
sudo gem install fastlane -NV

Install Firebase CLI

Download the standalone binary for your OS if you don’t already have it by clicking here, select the appropriate OS and navigating to the standalone binary tab. After downloading the binary you should make it executable by using this command

chmod +x ./firebase-tools-macos

Add the binary to your PATH to enable global access. Login with the login command and give firebase access.

Setting up

Open up terminal and change your directory to your project root folder and hit

fastlane init

You will be prompted to answer some setup questions:

  • Confirm your package name
  • A path to service action json secret file — configuration used to connect and authenticate with Google play api. Press enter to skip this, as we won’t be doing that now
  • Confirm if you will be uploading builds to Google play — again skip this by pressing ‘n’

Once completed, fastlane will automatically generate a configuration based on your answers to the prompts during setup. This might take a minute or two, hold tight. You may need to press enter multiple times to complete the setup process.

It basically generates a Fastlane folder in the root of your application which contains two important files: Appfile and Fastfile

Appfile defines configuration global to your app, like your application identifier for example.

Fastlane uses Fastfile to store automation configurations, within it you’ll see different lanes. Each of these lanes is used to automate different and actions.

Make sure you’re using the latest version if you already had fastlane installed by updating using

fastlane update_fastlane

Add App Distribution to your fastlane by running the following command

fastlane add_plugin firebase_app_distribution

Fastfile Configuration

You do not need to change the contents of Appfile, the default works for now.

A typical Fastfile configuration looks like this

fastlane_version "2.68.0"default_platform :androidplatform :android do    lane :{lane_name} do
# Take some action
# Take some more action...
end

lane :{lane_name} do
# actions
end
end

Where you get to input some actions in the lane to carry out a form of automation, possible actions include running git commands, building your app, distributing to deployment channels and sending a notification message and many more.

Input this into your Fastfile

Fastfile

A fastfile can have several lanes, to run our fastlane command,

Let’s break it down, shall we?

The first thing to note is that right from the first command in the lane, we build our android app using this command

build_android_app(task: "assembleRelease")

There are several other actions that you can take here but our immediate path to glory would be

build -> distribute -> notify

Next, distributing our app using firebase_app_distribution command

firebase_app_distribution(
app: "1:123456789:android:abcd1234",
testers: "tester1@company.com, tester2@company.com",
release_notes: changelog,
groups: "qa-team, dev-team",
firebase_cli_path: "/absolute/path/to/firebase/cli/binary"
)

This command takes a few parameters:

app: firebase app id

testers: comma-separated string list of emails of the testers, this can also be a text file of comma-separated email list

release_notes: can either be a string or a file (My preferred method is a file, which can be updated right before deployment)

groups: comma-separated list of group aliases to be added to the build

firebase_cli_path: Absolute path to firebase cli binary

slack(
slack_url: "https://hooks.slack.com/services/...",
message: "New build is available for testing",
success: true,
default_payloads: [:git_branch, :last_git_commit_message]
)

This block enables a success/failure notification to be sent to a Slack channel immediately after the process is complete. You must have completed setting up a hook on slack and get the url which is passed as slack_url. Set success to true. If you haven’t already done this, you can check out Sending messages using webhooks for how to set it up.

default_payloads specifies how much information is displayed in the slack message.

More options to customize the slack message can be seen here.

We add an error block after the lane to also notify the channel when an error occurs during the process, here we set slack success to false.

error do |lane, exception, options|

# send slack failure notification
slack(
message: "Build failed with exception: #{exception}",
slack_url: "https://hooks.slack.com/services/...",
success: false,
default_payloads: [:git_branch, :last_git_commit_message]
)

end

Indentation matters here so make sure you’re not missing anything.

Running our beta deployment

To deploy we just have to run the following command

fastlane beta

beta here can be any name you give your lanes, only the commands under that lane would be executed.

You may get an error that says App distribution could not find your app if you haven’t activated App distribution in your app like this:

App Distribution error

So head to your firebase console, Select YOURAPP — > App distribution and click “Get started”. Then try again.

SUCCESS!!!

A notification is posted on slack immediately the app distribution completes with a success message

Slack success notification

An error message would look like this:

We can further improve our setup by creating another lane in our fastfile for distributing to google play store. Let’s leave this for another day.

I would like to hear your views and several other ways you are using fastlane on your projects!

--

--