A guide to implement CD in Android projects using GitHub Actions and Firebase App Distribution

Arthur Henz
CodeX
Published in
7 min readMay 28, 2024

--

In this article, I’ll be covering a step-by-step guide to automate the delivery of executable files for users on Firebase, running a workflow in a specific branch and delivering it to a group of testers registered by email.

For beginners, I encourage you to know the difference between CI and CD and the main purpuse of GitHub Actions and Firebase App Distribution.

Nice! Can you show me the steps you will be following?

For sure! Here are the steps to implement our CD:

  • Create a project file
  • Create the project on Firebase App Distributions
  • Setup Google Cloud Console
  • Create a tester group
  • Trigger an Action to send the application to Firebase App Distribution

Right! At this point, we have an essential routine of tasks to run on GitHub Actions. Now, we’ll use a platform to deliver an executable file to anyone who has their email registered. The platform is called Firebase App Distribution.

Create a project file

To make it work on GitHub, you must have a .github folder on the root of the project. Inside of it, create a folder named “workflows”. Then, create a file for your specific goal.

A root folder named “android example project” that contains a folder named “dot github” that contains a folder named “workflows”. Inside of it, there are four yml files named “automatic deploy”, “dev on pull request”, “main on push” and “manual deploy”. All these files are in snake case.

In this example, we’ll work with manual_deploy.yml.

This project file will access your code, generate an executable and send it straight to GitHub Actions. Then it will allow us to manually send the .apk to Firebase App Distribution.

name: Manual deploy

on:
push:
branches: [ deploy_branch ] # define the branch you want to deploy.
# we commonly add a stage branch on this case.
workflow_dispatch:
inputs:
release_notes:
type: string
required: true
default: 'Manual Debug Build'
description: 'Release Notes'
jobs:
build:
name: Building and distributing app
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '18.0.2+101'

- name: Setup Gradle
uses: gradle/gradle-build-action@v3

- name: Make gradlew executable
run: chmod +x ./gradlew

- name: Execute Gradle command - assembleDebug
run: ./gradlew assembleDebug

- name: Upload Artifact to Firebase App Distribution
uses: wzieba/Firebase-Distribution-Github-Action@v1
with:
appId: ${{ secrets.FIREBASE_APP_ID }}
serviceCredentialsFileContent: ${{ secrets.CREDENTIAL_FILE_CONTENT }}
groups: testers
file: app/build/outputs/apk/debug/app-debug.apk
releaseNotes: ${{ inputs.release_notes }}

Let’s dig into all of these parts:

  1. workflow_dispatch: You can create workflows that are manually triggered with the workflow_dispatch event. You will then see a ‘Run workflow’ button on the Actions tab, enabling you to easily trigger a run.

2. actions/checkout: the most common job on GitHub Actions. It is responsible for grant access to the actual Android code you wrote

3. actions/setup-java: you can choose which Java distribution and version to use

4. gradle/gradle-build-action: can be used to configure Gradle and optionally execute a Gradle build on any platform supported by GitHub Actions

5. gradlew assembleDebug — — stacktrace: this step runs the Gradle command assembleDebug to build the Android application in debug mode. The --stacktrace flag provides a detailed stack trace in case of any build errors.

6.1. wzieba/Firebase-Distribution-Github-Action@v1: This step uploads the generated APK file Firebase. On the next steps, we’ll see how to get the FIREBASE_APP_ID and CREDENTIAL_FILE_CONTENT

6.2. groups: this name has to be the same as will be registered on Firebase App Distribution. In this case I’ll name it testers ;)

7. appId: Very straight forward, we’ll search for it on the next session.

8. serviceCredentialsFileContent: permissions to access the content of your project files. We obtain it basically setting up a service account using a json key that we set up inside our google cloud project.

9. release_notes: a note we’ll send to Firebase Distributions furtherly.

Create the project on Firebase App Distributions

  1. After registering in Firebase, go to console:

2. Then, follow all the steps to add a project to Firebase. Don’t worry, it’s fast to create.

3. Access the project and click on Android icon. On this step, you will be adding the right dependencies on your project.

Just go ahead. If you follow all the steps correctly, we’ll be good to go.

4. Still on Console, go to Project Settings

Scroll down and you’ll get this:

5. Copy paste your Application ID and create a new GitHub Secret. Use the same name that is in your YAML CI file. In my case, the Secret will be called FIREBASE_APP_ID.

After that, we’ll set up the project on Google Cloud Console

Setup Google Cloud Console

This part was fully based on the official wzieba’s wiki about Firebase-Distribution-Github-Action.

  1. Go to GCP and select your project

2. Click on Create Service Account

3. Enter some account name, press Create and continue

4. Select Firebase App Distribution Admin in Role input and click Done

5. Then, on the list of accounts find your newly created service account. Click on 3-dots menu and click on Manage keys

6. Click on Add key and then Create new key

7. Select type JSON and click Create

  1. After that, the file should be downloaded to your disk. Open the file, and copy its content.
  2. Go to GitHub > your repository > Settings > Secrets > Actions
  3. Create a new secret. I named it as CREDENTIAL_FILE_CONTENT. The value of the secret is the private_key.
  4. Save the secret. Important! Remember to remove the fetched file from your disk or store it in some secure place.

Now you have it! The CREDENTIAL_FILE_CONTENT is already available on your Action and in your file. The next thing you want to do is manually trigger your Action to make your newest application available.

Create a tester group

In this mandatory section, we’ll need to create a group and explicitly tell who should have the right access to the application. It is an easy step.

Just go to App Distribution (1), Testers and groups (2), create a group. In my case, I set the Action to send it only to testers (3). Choose the tester you want by email. The person will be notified.

Trigger an Action to send the application to Firebase App Distribution

As our last movement, you should be free to choose the deploy workflow (1) and select a specific workflow to run (2, 3). It will programatically send the file to Firebase. In my case, it was main.

FYI, some projects also work with the idea of staging things before sending it to production environment.

And going to Firebase Console, in App Distribution, when workflow succeeded, you will be able to download it and see the testers you added. You can also manually add your executable file there if you think it has a good reason.

On the image below, we can also se the number of tester guests, how many accepted, how many downladed and feedbacks from them. Pretty cool, huh?

And… that’s how implement CD in Android projects using GitHub Actions and Firebase App Distribution. Hope it was useful to you and if anything, just call me ;)

Ohh, and if you want to implement a powerful Github Actions pipeline with multiple analysis, read my other article, A brief guide to implement CI in Android projects using GitHub Actions and SonarCloud.

--

--

Arthur Henz
CodeX
Writer for

An Android developer who loves connections and self improvement :)