Build your Android Self hosted CI with Gitlab

In this article, Self hosted CI will be installed on a MacOS.

Gitlab-Runner

GitLab Runner is an application that works with GitLab CI/CD to run jobs in a pipeline.

In order to perform your pipelines, we need to install runners with Homebrew:

Install Gitlab with homebrew

Then install it as a service and start it:

Start Gitlab-runner

Then we need to register a runner :

Register your runner

The URL and token can be found in Gitlab / Settings / CI-CD / Runners.

It’s important to have tags on runner. Pipeline will identified this runner with his tag.

Then, your runner is available on your gitlab:

Runner is active on Gitlab

If you start your pipeline without any runner, you will see this warning and your pipeline will be stuck.

Android Studio

First thing, install Android Studio on your CI mac and check if Android SDK is present in:

Open Android Studio and install the latest Android SDK

Go in SDK Manager
Select Latest SDK

Create Pipelines

At the root of our project, create a .gitlab-ci.yml that will be filled with your different CI/CD Steps.

Setup Fastlane

To use Fastlane on our Gitlab CI, we need to create a Gemfile in the root of your project with the following content:

Gemfile

Install and init Fastlane

Provide all informations prompted in the console and open fastlane/Fastfile to create our lanes.

For this sample we will create 3 lanes:

Fastfile

You can test your lane with fastlane directly in CLI:

Distribution Lane

Now we will create your lane used to distribute your debug APK with Firebase distribution.

First of all, execute at the root of your Android Project

Configure Firebase distribution plugin

Install firebase CLI on your MacOS used for your CI:

Connect to firebase with Firebase CLI

Accept all authorisations :

Login with the CI

A token is generated with this command, it will be used on your CD in order to deliver your APKs on Firebase.

Create your lane:

The first command is used to generate a debug APK.

The second command is used to upload this APK to Firebase.
The App can be found on Firebase in project settings.
The token is the one previously generated.

In our .gitlab-ci.yml pipeline file, create your distribute step. This step will call your lane previously created and will only be executed when there is a change on your branch dev .

When a change will happened on your branch, a debug APK will be uploaded automatically.

Release lane

This lane will be used to release a signed APK on firebase. A lot of configuration is already done with distribution lane.

We need to configure the App Gradle in ordre to sign the APK with your keystore:

After that, we need just to create the lane in your fastfile:

The first command is used to generate signed APK, the second is used to upload it on Firebase.

In our .gitlab-ci.yml pipeline file, create your distribute step. This step will call your lane previously created and will only be executed when there is a change on your branch master .

When a change will happened on your master branch, a released APK will be uploaded automatically to firebase.

Content of your Pipeline

In your.gitlab-ci.yml , you will have your steps configured like this:

.gitlab-ci.yml

Let’s learn more about this pipeline !

If you want your step to be executed only on merge request, add:

If you want your step to be executed on a specific branch, add:

Enjoy your CI

Push your code on Gitlab and your pipeline will be launched automatically:

Pipeline is launched

Reports are upload to the artifacts part:

artifacts are uploaded

If this article helps you, feel free to 👏 and share it !

Many thanks for the reading.

History:

[18/10/2020] Add release step.
[17/10/2020] Add merge request exception on the pipeline and Firebase distribution step.

Android & iOS, quality in software is 𝛼&ɷ