Automating Deployment of Android Apps using Fastlane with CI/CD.
Fastlane is a great tool to automate your app deployment and release process for Android as well as iOS. 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.
Installing and Setting up Fastlane
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 Fastlane:
sudo gem install fastlane
And 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 setup is done successfully, Fastlane will automatically generate a configuration file on the basis of information
This process generates our Appfile and Fastfile, in a directory fastlane.
Appfile: It is used to define configuration that is global to the app. It conatins information of where your package name and json secret key (path to Google Play Console) are.
Fastfile: It 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 behaviour of fastlane to perform different tasks. The group of steps defined in a fastfile as 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 CI
Continuous integration is the practice that requires that every time somebody commits any change into source control, the entire application is built and a comprehensive set of automated tests is run against it. Crucially, if the build or test process fails, the development team stops whatever they are doing and fixes the problem immediately.
Integration of Fastlane with CI/CD
Now comes the tricky part where you will like it to be more secure than ever before, as you will be pushing the code to a git repository.
What’s worse then pushing a code with hard coded values? Right, so you willl like 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 is 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 Dockerfile 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 you 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 you application to Playstore. Feel free to reach for any queries.