Automating Deployment of Android Apps Using Fastlane with CI/CD.
What Is Fastlane?
Fastlane is a great tool to automate your app deployment and release process for Android as well as iOS. It is responsible in handling tedious tasks like generating screenshots, code signing and releasing your application
Installing and Setting Up Fastlane
Today we will be looking forward on deployment of Android apps over CI/CD using Fastlane. Below are steps to achieve a successful Android Fastlane Deployment. This process has to be done locally in the source directory of your project, not over the CI/CD.
On your CLI (Command Line Interface), navigate to your project folder and run the following command to install
sudo gem install fastlane
To verify that the installation was successful type the command:
Setting up Fastlane
Navigate your terminal to your project’s directory:
fastlane init( It Initializes Fastlane )
and then you can follow the instruction here to configure the rest. After the successful setup , Fastlane will automatically generate a configuration file on the basis of information. This process generates our Appfile and Fastfile, in a directory Fastlane.
What Is Appfile?
Appfile is used to define configuration that is global to the app. It contains information of where your package name and json secret key (path to Google Play Console) are.
What Is Fastfile?
Fastfile is the most interesting file as it has all the information that is needed to distribute the app. It defines different lanes that drive the behavior of Fastlane to perform different tasks. The group of steps defined in a fast file are called lanes. Examples: Tasks to run tests, assemble a debug build & upload your beta app to Crashlytics, deploy/upload a new version to Google Play and so on..
For creation of your Fastfile and Appfile I will recommend you to check this out. Here you will get all the information on creating different tracks and running tests as well.
What Is Continuous Integration?
Continuous Integration (CI) is a development practice where developers integrate code into a shared repository frequently, preferably several times a day. Each integration can then be verified by an automated build and automated tests. While automated testing is not strictly part of CI it is typically implied- Codeship
Integration of Fastlane With CI/CD
Now comes the tricky part where you will like it to be more secure than ever before because you will be pushing the code to a git repository.
What’s worse then pushing a code with hard coded values? Right! so you will want it to be more secure as it is going to be over the internet and is also considered a good practice. So whatever values are to be provided in Appfile and Fastfile, we will use Environment Variables to pass them while over CI/CD. Below is a snippet how:
Now locally we can easily declare these variables and can deploy it to playstore but the challenge comes when it is on CI/CD as you need to create an environment that is suitable for development of Android Apps, with required SDK’s and tools.
So, for that we will use a docker image theanam/react-native, which I find certainly the best to get the task done.
Now with few things remaining the main challenge is to get those environment variables inside the docker container. Which is pretty easy I guess follow the command below but first you need to build the docker image as well:
docker run -it -e ENVIRONMENT_VARIABLE1=$ENVIRONMENT_VARIABLE1 -e ... -e ... DOCKER_IMAGE
Now as the project has been copied inside the docker and is ready to run simply define the ENTRYPOINT of docker container in Docker file as
ENTRYPOINT npm install && fastlane android beta
*beta is the name of the track in the Fastfile, so you can use the one defined by you and to be used.*
The only thing now left is configuring your CI file that is different for every CI/CD platform, but don’t forget to add those ENVIRONMENT_VARIABLES in you CI/CD platform.
The things you need to define in CI/CD file are building the docker image with your project inside it and then running that Docker Instance. Rest Fastlane will take care of and make sure your app is up and running for users.
You have successfully implemented Fastlane with CI/CD and pushed application to PlayStore.