Build your Android Self hosted CI with Gitlab

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

Gitlab-Runner

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

brew install gitlab-runner
Install Gitlab with homebrew

Then install it as a service and start it:

brew services start gitlab-runner
Start Gitlab-runner

Then we need to register a runner :

gitlab-runner register
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

~/Library/Android/sdk

Open Android Studio and install the latest Android SDK

Go in SDK Manager
Select Latest SDK

Create Pipelines

Setup Fastlane

Gemfile

Install and init Fastlane

brew install fastlane
fastlane init

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:

fastlane tests

Distribution Lane

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

fastlane add_plugin firebase_app_distribution
Configure Firebase distribution plugin

Install firebase CLI on your MacOS used for your CI:

curl -sL https://firebase.tools | bash

Connect to firebase with Firebase CLI

firebase login:ci

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 .

distribute:
stage: distribute
tags:
- mac-os
script:
- fastlane distribute
only:
refs:
- dev

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

Release 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 .

#Step used to generated signed APK and upload it
release:
stage: release
tags:
- mac-os
script:
- fastlane release
only:
refs:
- master

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

Content of your Pipeline

.gitlab-ci.yml

Let’s learn more about this pipeline !

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

only
- merge_requests

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

only
refs:
- your_branch

Enjoy your CI

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:

Android & iOS, quality in software is 𝛼&ɷ