- Introduction
- Prerequisites
- Step 1: Configure Firebase
- Step 2: Generate a Firebase Token
- Step 3: Create a Service Account Key
- Step 4: Enable Bitbucket Pipelines
- Step 5: Create a bitbucket-pipelines.yml File
- Step 6: Add Environment Variables to Bitbucket
- Step 7: Gradle Configuration
- Step 8: Add Testers And Release Notes
- Step 9: Push Your Changes
- Contact
This guide will walk you through the process of setting up Bitbucket Pipelines for your Android project and deploying it to Firebase App Distribution for testing.
- A Bitbucket account with a repository (you will need admin access to enable Pipelines)
- An Android project hosted on Bitbucket
- A Firebase account
- Firebase CLI installed on your local machine
- Node.js installed on your local machine
- A Google Cloud Console account
- Login to Firebase with your Google account.
- Create a new project or use an existing one.
- Navigate to
Project settings > Your apps
. - Download the
google-services.json
file and place it in theapp
directory of your Android project. - Install Firebase CLI by running the following command in your terminal:
npm install -g firebase-tools
NOTE
- You need to have Node.js installed on your machine to use npm. You can download it from Node.js official website.
Run the following command in your terminal to generate a Firebase token:
firebase login:ci
This will open a browser window asking you to log in with your Google account. After logging in, you will see a token in your terminal. Copy this token as you will need it later in the Bitbucket Pipelines configuration.
- Go to the Google Cloud Console.
- Select your project.
- Navigate to
IAM & Admin > Service Accounts
. - Click on
Create Service Account
. - Enter a name for the service account and click
Create
. - Assign the
Firebase Admin SDK Administrator
role to the service account. - Click on
Create Key
and selectJSON
as the key type. - Save the JSON file to your local machine.
- Encrypt the JSON file using Bitbucket Pipelines environment variables. You can use the Bitbucket UI to encrypt the file or use the following command:
cat service-account-key.json | base64 | pbcopy
- Navigate to your Bitbucket repository.
- Go to
Repository settings
. - Click on
Pipelines > Settings
. - Toggle the
Enable Pipelines
switch.
Create a bitbucket-pipelines.yml
file in the root of your repository. This file will define your build pipeline.
NOTE
You will need to enable pipelines in Repository settings > Pipelines > Settings > Enable Piplines
before you can create the bitbucket-pipelines.yml
file.
You can use the following template as a starting point for your bitbucket-pipelines.yml
file.
image: cimg/android:2024.01 # image defines the Docker image to use as the build environment
pipelines: # pipelines is a special keyword that defines the build configuration
default: # pipeline definition for all branches, to run it automatically when a release is created you can use the branches : release/* or tags instead of default
- step: # step to build Android debug application
name: Build Android Debug Application
caches: # caching speed up subsequent execution
- gradle
script:
- ./gradlew assembleRelease appDistributionUploadRelease # ./gradlew assembleDebug appDistributionUploadDebug for debug build
artifacts:
- app/build/outputs/** # artifacts are files that are produced by a step
- step:
name: Deploy to Firebase App Distribution
deployment: test # deployment allows you to define a deployment environment
script:
- pipe: atlassian/firebase-deploy:2.0.0 # pipe that allows you to use a Docker image as a service
variables:
KEY_FILE: $KEY_FILE # KEY_FILE is generated from google cloud console service account and encrypted using bitbucket pipelines environment variables
FIREBASE_TOKEN: $FIREBASE_TOKEN # FIREBASE TOKEN is generated from firebase login:ci
PROJECT_ID: $PROJECT_ID # PROJECT_ID is the firebase project id
- Go to your Bitbucket repository.
- Go to
Repository settings > Repository variables
. - Make sure to check the
Secured
checkbox for theKEY_FILE
andFIREBASE_TOKEN
variables. - Add the following environment variables:
KEY_FILE
: Paste the base64-encoded service account key.FIREBASE_TOKEN
: Paste the Firebase token generated in Step 4.PROJECT_ID
: Your Firebase project ID.
- Click on
Add
to save the variables. - Click on
Save
to apply the changes.
- In your project-level Gradle file, add the App Distribution plugin as a buildscript dependency.
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.1.2'
classpath 'com.google.firebase:firebase-appdistribution-gradle:3.0.3'
}
}
- In your app-level Gradle file, apply the App Distribution plugin.
apply plugin: 'com.android.application'
apply plugin: 'com.google.firebase.appdistribution'
- Sync your project with the Gradle files.
-
Create two files in the root of your repository:
/app/src/release/release-notes.txt
: Add release notes for the release build./app/src/release/testers.txt
: Add a list of testers' email addresses for the release build.- here is an example of the content of the files:
// Release Notes for Release Build ## Version: [Version Number] ### Release Date: [Release Date] ## New Features - [Feature 1]: [Brief description of the feature] - [Feature 2]: [Brief description of the feature] ## Improvements - [Improvement 1]: [Brief description of the improvement] - [Improvement 2]: [Brief description of the improvement] ## Bug Fixes - [Bug 1]: [Brief description of the bug fix] - [Bug 2]: [Brief description of the bug fix] ## Known Issues - [Issue 1]: [Brief description of the known issue] - [Issue 2]: [Brief description of the known issue] ## Deprecated Features - [Deprecated Feature 1]: [Brief description of the deprecated feature] - [Deprecated Feature 2]: [Brief description of the deprecated feature]
// List of Testers for Build [email protected] [email protected] [email protected]
-
Now Add the
firebaseAppDistribution
block to your app-level Gradle file to configure the release notes and testers.
buildTypes {
debug {
minifyEnabled false
firebaseAppDistribution {
releaseNotesFile = file('debug/releasenotes.txt')
testersFile = file('debug/testers.txt')
}
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
firebaseAppDistribution {
releaseNotesFile = file('release/releasenotes.txt')
testersFile = file('release/testers.txt')
}
}
}
- Sync your project with the Gradle files.
- Commit and push your changes to the repository following MTK standards.
- Go to your Bitbucket repository.
- Navigate to
Pipelines
to see the build progress. - Once the build is successful, you will see the app uploaded to Firebase App Distribution.
- You can now distribute the app to testers and view the release notes.
- Congratulations! You have successfully set up Bitbucket Pipelines and Firebase App Distribution for your Android project.
- You can now automate the build and deployment process for your Android app.
- You can also customize the pipeline to include linting, unit tests, and other checks as needed.