Android Continuous integration using Gitlab CI and Fastlane

Learn how to streamline app deployments to the PlayStore effortlessly with Fastlane in our latest blog post!
Sep 22 2021 · 3 min read

Background

In a previous article, we set up .gitlab-ci.yml to run unit tests and build apk/bundle. We started by setting up the docker image and went through all necessary components of `.gitlab-ci.yml`.

Please have a look at the previous/next stories related to gitlab and CI for more information in case you haven’t yet.

Part 1:

Part 3:

In this article, we are going to push builds to PlayStore using Fastlane.

We are what we repeatedly do. Excellence, then, is not an act, but a habit. Try out Justly and start building your habits today!

Let’s get started!

Why Fastlane?

Fastlane is the easiest tool to automate the deployments and release process of your apps. It lets you automate all tedious tasks like automate screenshots, beta deployment, app store deployment, and code signing, etc...

Installation

Install fastlane using:

# Using RubyGems(macOS/Linux/Windows)
sudo gem install fastlane
# Alternatively using Homebrew(macOS)
brew install fastlane

Configure Fastlane in our project

After successful installation, run the following command in your root project directory.

fastlane init

It’ll first ask you about the package name for your application. Secondly, you’ll be asked for the path of your JSON secret file, press enter without giving any input and the third input you have to give is about your plan on uploading to Google play store, answer it ’n’ when asked because we can set up it later.

Hurry!!. Now you can see two new files in the Fastlane directory of your project

AppFile: contains information about package name and JSON secret file path that you entered in Fastlane initialization.

json_key_file("json_secret_path") 
# Path to the json secret file - Follow # #https://docs.fastlane.tools/actions/supply/#setup to get one
package_name("com.package.name") # e.g. com.krausefx.app

Fastfile: defines lane to execute. Open fastfile and you will see:

default_platform(:android)

platform :android do

  desc "Builds the debug code"
  lane :buildDebug do
    gradle(task: "assembleDebug")
  end

  desc "Builds the release code"
  lane :buildRelease do
    gradle(task: "assembleRelease")
  end

  desc "Runs all the tests"
  lane :test do
    gradle(task: "test")
  end

Syntax of writing config in Fastlane

desc "Your lane description"
lane :your_lane_name do
  ...
  ...
end

In your fastFile the default first lane is lane :testinside that lane has gradle(task:"test").It means this lane is going to run the unit tests for your whole project.

Check this out for detailed explanation of lanes.

Before we start lane setup for release deployment, check this detailed instruction to collect the required credentials to upload a build to Google play.

Update your fastlane/Appfile and change the json_key_file line and set JSON secret path

json_key_file("~/google_play_api_key.json")

Let’s modify the lane to submit a new internal build to Playstore.

platform :android do
  desc "Submit a new Internal Build to Play Store"
  lane :upload_internal do
    gradle(
      task: "bundle",
      build_type: 'release') # compile a release bundle application app.aab
    upload_to_play_store(
      track: 'internal',
      skip_upload_metadata: true,
      skip_upload_images: true,
      skip_upload_screenshots: true)
  end

Here we push the release bundle on an internal track and we skip metadata, images, screenshots uploading.

Signing Configuration

An android application needs to be signed with a Keystore in the building process. Also, we shouldn’t store a Keystore file and credentials in the version control system. We can use Gitlab’s CI/CD variables to store it.

Gitlab’s CI/CD process can read the value of its CI/CD variables at runtime when running the script in gitlab-ci.yml file.

Create Base64 string of Keystore file by using the following command:

sudo base64 -i ~/path_to_keystore.keystore -o outputFile.txt

Now that we have the base64 string of our Upload Keystore file available in the text file, the Keystore file is no longer needed to be stored in the project folder. Copy-paste the base64 string value to a Gitlab CI/CD variable.

Now refactor signingConfigs block in the app build.gradle to use newly added CI/CD environment variable.

signingConfigs {

      release {
            storeFile file(System.getenv("APKSIGN_KEYSTORE"))
            storePassword System.getenv("APKSIGN_KEYSTORE_PASS")
            keyAlias System.getenv("APKSIGN_KEY_ALIAS")
            keyPassword System.getenv("APKSIGN_KEY_PASS")
        }
}

Now it’s time to modify our .gitlab-ci.yml.

stages:
   ...
  - deploy
deploy-internal-app-bundle:
  stage: deploy
  before_script:
    - apt-get -qq update
    - apt-get install -qqy --no-install-recommends build-essential ruby-full
    - gem install bundler fastlane
    - echo $APKSIGN_KEYSTORE_BASE64 | base64 -d > release.jks
    - export APKSIGN_KEYSTORE=`pwd`/release.jks
    - echo $APP_PLAY_SERVICE_JSON > ~/google_play_api_key.json
  script:
    - bundle exec fastlane upload_internal
  when: manual

Commit and push. Now your lane will upload your application as an internal version on Google Play Console. Please note that you need to update path of google_play_api_key.json and Keystore file paths as per your configurations.
 

1_8TJ_xTElLo964Hb9RAzw-Q.png

radhika-s image
Radhika saliya
Android developer | Sharing knowledge of Jetpack Compose & android development


radhika-s image
Radhika saliya
Android developer | Sharing knowledge of Jetpack Compose & android development

Whether you need...

  • *
    High-performing mobile apps
  • *
    Bulletproof cloud solutions
  • *
    Custom solutions for your business.
Bring us your toughest challenge and we'll show you the path to a sleek solution.
Talk To Our Experts
footer
Subscribe Here!
Follow us on
2024 Canopas Software LLP. All rights reserved.