Build your self hosted iOS CI with Gitlab

Gitlab is a wonderful tool to create your own pipeline. Combined to Fastlane, you will be able to have quickly your own CI scalable and maintainable.

Kevin ABRIOUX
4 min readNov 17, 2020

Register Gitlab Runner

In your Gitlab UI, go in Settings -> CI/CD

Go in Runners, check Gitlab URL and registration token.

Go on your MacOS client where you will register your runner. Then we need to register a runner, with the associated URL and token:

gitlab-runner register
Register a runner

Install Fastlane

The first step is to install and init Fastlane:

brew install fastlane
fastlane init
fastlane init

Create Pipelines

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

Configure Lanes

For a complete CI, we need several lanes in order to check your code quality and generate your application.

Cocoapods Lane

This lane is used to install your pods on your CI. Add this to our fastlane/Fastfile

Pod install lane

Add gem "cocoapods" to your Gemfile

Test your lane with fastlane pod_install

Test your podfile lane

Test Lane

The first lane must run our unit tests. Add this to our fastlane/Fastfile

Test your lane with fastlane tests

Lint Lane

The purpose to this lane is to check lint rule on Swift code.

For this, we need to create a .swiftlint.yml at the root of our XCode project and add exception rule in order to ignore Pods or Carthage libraries.

And create our lane in fastlane/Fastfile

Test your lane with fastlane lint

fastlane lint

Build lane

Finally, our last lane is used to check if your app build correctly

Test your lane with fastlane build

Generate Debug Lane

With this lane, you will generate a debug IPA and upload it to Firebase App Distribution. First of all, you need to create an App Store Connect API Key on the Users page. You need to log in with the owner’s account and generate a .p8 file. In this same page, you will find the Issuer’s id, copy it.

Now it’s time to configure Firebase App Distribution Plugin. At the root of your project, execute this command:

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 and accept all authorisations.

firebase login:ci
Generate your Firebase App Distribution Token

Your lane will execute several steps with Fastlane methods:

  • Generate App Store connect access with app_store_connect_api_key , you have to configure it with your Key ID, the issuer’s ID and the key content (find it in your P8 file)
  • Get your certificates with get_certificates , Get all certificates needed to generate and sign your Application.
  • Generate provisioning profiles with sigh , this method will get existing provisioning profile or generate them.
    It’s important to use the option force: true in order to renew provisioning profiles with newest devices. This also make your CI will not failed if someone delete your provisioning profile.
    If you generate on a Mac where XCode is not connected to any developper account, indicate the team_id and the username .
  • Set up upate_code_signing_settings in order to set your XCOde with correct Team’id, provisionning profile for each target.
  • Generate IPA with gym , this method will generate your IPA.
  • Upload it to Firebase App Distribution with firebase_app_distribution , this method will upload your previously generated IPA to Firebase App Distribution.

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.

Configure your Gitlab pipeline

At the root of our project, create a .gitlab-ci.yml

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

Find your pipelines executions

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

Many thanks for the reading.

History:

[06/01/2021] Add upate_code_signing_settings in order to configure XCode before gym
[11/12/2020]
Explain force parameter during provisioning profile generation step.
[23/11/2020]
Add Firebase distribution step.

--

--