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:
brew install gitlab-runner
Then install it as a service and start it:
brew services start gitlab-runner
Then we need to register a runner :
gitlab-runner register
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:
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:
~/Library/Android/sdk
Open Android Studio and install the latest Android 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:
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:
You can test your lane with fastlane directly in CLI:
fastlane tests
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
fastlane add_plugin firebase_app_distribution
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 :
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
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
.
#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
In your.gitlab-ci.yml
, you will have your steps configured like this:
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
Push your code on Gitlab and your pipeline will be launched automatically:
Reports are upload to the artifacts part:
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.