CI server on Mac OS for iOS using GitLab and Fastlane

There are many advantages of using CI servers. They decrease the risk of multiple integration conflicts and reduce rework. Hence, you save your time and money. Still, setting up CI server can be a real challenge even to experienced developers and sysadmins. Although the code management tools seem easy, no one can escape setting issues. You have probably bumped into them already if you dealt with GitLab or Fastlane.

The good news is you can always turn to those who have managed them successfully. We are here ready to share our experience of working with this program. With us, you will learn how to set CI server faster and thus save your precious time for completing other important tasks.

Creating a new project in GitLab

Prepare your Mac server by creating a new user, new keychain and installing the required utilities. For that, do the following:

 

  1. Creating a system user for CI server

Open System Preferences - Users & Groups

  1. Create a new user account. Select an Administrator type of account.

create_user_1.png

  1. After user creation, you will have to setup a user account to log on automatically. Select Login options in the left panel and then select a new account for Automatic login option.

create_user_2.png

  1. Close System Preferences.

 

  1. Log in to start a new session.

 

create_user_3.png

 

  1. Create a new Keychain and synchronize it with your account. Open the Keychain Access app. Create a new login Keychain.

create_keychain_1.png

create_keychain_2.png

Authenticate with your account login password.

 

create_keychain_3.png

Set a new Keychain as default.

create_keychain_4.png

  1. Now, the utilities will use Keychain as default during build automation and testing.

 

  1. Installation of the required utilities.

You will need the following utilities for build automation and testing:

  1. Xcode. It is easy to install from AppStore.
  2. Fastlane. Install it using the following instructions.

 

Open Terminal app. Make sure Fastline meets the following requirements:

  1. Mac OS 10.9 or newer
  2. Ruby 2.0 or newer (ruby -v)
  3. Xcode

 

Apart from the Xcode installation, you also need the Xcode command line tools set up.

$ xcode-select --install

 

If you have not used the command line tools before (which is likely if you just installed it), you’ll need to accept the terms of service.

$ sudo xcodebuild -license accept

Install the Fastlane gem and all its dependencies (this might take a few minutes).

$ sudo gem install fastlane --verbose

  1. GitLab CI runner. You will install this utility later, after the configuration of GitlLab CI server.

 

  1. Create a script to unlock Keychain. It is necessary to give other utilities access to Keychain, otherwise, locked by default.

Complete the following steps:

  1. Create the ssh script using the next code:

$ security unlock-keychain -p <put password here> <put URL to the keychain here>

Call it: unlock_ci_keychain.sh

 

How do we know the path to Keychain?

Type the following command into the terminal:

$ security list-keychains

Choose the one that you need from the list.

 

Change file system permissions for running the script:

$ chmod a+x unlock_ci_keychain.sh

  1. You can use this tutorial to make your script globally executable, meaning able to run from anywhere.

Warning! Make sure the script is globally executable, otherwise the export will not have an effect.

 

Related: New trend of image editing - iOS apps?

 

Configuring GitLab CI service in GitLab

 

Now, you have to set up GitLab CI service and connect your Mac server to GitLab portal. If this program is rather new for you, learn more about it here.

It does not matter if you use GitLab for corporate accounts or personal use, the following steps will do for both.

 

Log in the system and do the following:

  1. Start a new project or select the existing one and open it. In my case, I choose to create a new project and call it ci_example_project.

 

create_project_1.png

 

  1. Activate CI service.
  1. Click on GitLab CI service in the list of available services Settings - Services.

 

create_project_2.png

 

create_project_3.png

 

  1. Activate GitLab CI service and save the changes.

 

create_project_4.png

 

  1. Additional options will appear in Settings.

 

create_project_5.png

 

  1. Create Runner. Select Runners option. Here, you can manage Runners. As you can see, I have two runners one of which is shared.

 

create_project_6.png

 

Install GitLab CI runner on Mac machine using the instruction, and the following data:

  1. coordinator URL: <from GitLab site, runners page >
  2. token: <your private token from GitLab site>
  3. description (you can set any name): indeema_ci_example_runner
  4. tags (e.g. iOS): example_tag
  5. executor: shell

Now, update the page with Runners and you will see a new one, which has been just created. It is activated automatically and is connected to the project.

 

create_project_7.png

 

Conclusions

We have created the project in GitLab system. It does not have any files or history yet. We connected GitLab CI runner to the project. GitLab CI runner is started on the local Mac Machine adjusted for build automation.

So far, the runner will be inactive during adding sources to the repository, since .gitlab-ci.yml file for your project is missing. Learn more about this file and its configuration first. Below, you will find how to configure .gitlab-ci.yml file specifically for a certain project.

 

Our next steps will be:

  • Create Xcode project and configure it to build automation.
  • Setup Fastlane
  • Import a signing Apple Developer Id certificate to CI server.
  • Configure .gitlab-ci.yml file.

 

So, we will create a project and save it in the repository. You can download the final version of the project resource here.

 

Related: Understanding our iOS development course

 

Preparing Xcode project

  1. Add new build configuration in project’s settings, just duplicate “Release” configuration and name it “TestFlight.”
  1. In Xcode open project settings.
  2. Duplicate “Release” configuration and name it “TestFlight.”

 

xcode_1.png

 

  1. Add and setup new scheme. Open scheme manager and duplicate your active scheme and make it shared.
  1. Open “Manage Schemes.”

 

xcode_2.png

 

  1. Duplicate existing scheme.

xcode_3.png

 

  1. Name the scheme “TestFlight.”

Choose “Archive” option on the left side. Select “TestFlight” build configuration and make it shared.

xcode_4.png

 

  1. Setup provisions and certificates for new configuration in the build settings of the target. I’m using AppStore ones, as I’m going to deploy app to TestFlight.

xcode_5.png

Prepare Fastlane files.

We strongly recommend you to get familiar with Fastlane before completing the next steps.

  1. Initialize Fastlane.

To initialize Fastlane for your project:

  • Open Terminal and proceed to the current project’s root.
  • Type the following command and accomplish initialization procedure:  

$ fastlane init

  • Correct installation will result in a Fastlane folder with files for configuration of the project.

fastlane_1.png

  1. A configuration of the Fastlane.
  • Configure Fastlane file by setting building scheme for the deploy lane which you added in Xcode project, please look at the picture.

Use any text editor for that. I’m using Sublime Text 2.

I have removed some lanes from the file.

Please check lane named “appstore”. These are 3 active functions:

  • sigh - this function gets available provision profiles from the member center depending on parameters from Appfile.

  • gym - makes a build and archive of the project.

  • pilot - uploads just created archive to the AppStore.

fastlane_2.png

  • Configure Appfile file by setting app_identifier, apple_id and team_id, see picture below.

fastlane_3.png

Add .gitlab-ci.yml file to the project

 

According to Gitlab documentation we should have .gitlab-ci.yml file in the root folder of the repository. More details about it you can read here.

  1. Open Terminal and proceed to current project’s root.
  2. Create a file using the following command:

$ touch .gitlab-ci.yml

  1. Open the file via the text editor or Xcode and type the following text.

 

before_script:

 - unlock_ci_keychain.sh

stages:

 - deploy

build_job:

 stage: deploy

 script:

   - fastlane appstore

 only:

   - deploy_branch

 tags:

   - example_tag

 

 

Here is what I received after completing the task:

gitlab-yml_1.png

  1. Edit the file with your personal data.
  • deploy_branch under the only section means that the current job (deploy_job) will be performed only if a new commit appears in the branch deploy_branch.
  • example_tag under the tags section is the runner’s tag we have created a few steps before. Specify the runner tag here. The tags should be identical or the job will not be performed.  

 

Save all the changes to the repository by running the commands commit and push.

 

Create a new branch with the name indicated in .gitlab-ci.yml file. In my case, it is deploy_branch.  Save the branch to the repository. You are likely to find a build with “failed” status in the Builds section on the GitLab web afterward.

gitlab-yml_2.png

In the log, you will see the following:

gitlab-yml_2.png

Your runner has to have access to the Apple Id certificate saved in the Keychain.

 

To make CI server available for Apple Id I will run the following command:

  1. Open Terminal and login to CI server using ssh. Since both CI server and my machine are in the same network I use:

$ ssh indeemaci@192.168.1.72

  1. To access keychain from Terminal unlock it running the following command:

$ unlock_ci_keychain.sh

  1. Run the following command to provide Fastlane with access to Apple Id:

$ fastlane-credentials add --username <put Apple ID here>

Then you will be asked for the password for Apple Id.

 

To add the certificate to the CI server I use the following commands:

  1. Copy certificate(*.p12 file) on the CI server in the home folder using the terminal, BUT not signed in on the server:

$ csh <path to the certificate> indeemaci@192.168.1.72:~/

Then enter the password for Indeema CI user.

 

  1. Log in to CI server.

$ ssh indeemaci@192.168.1.72

 

  1. Unlock keychain calling script:

$ unlock_ci_keychain.sh

 

  1. Go to the folder with certificate:

$ cd ~

 

  1. Install certificate using the command:

$ security import <your copied certificate name> -k ~/Library/Keychains/indeema_ci.keychain -P <password for certificate>

While we suggest this method of installation, feel free to use other ways that will work for you.

 

Now, you can retry your runner for a specific build by pressing the Retry button.

gitlab-yml_4.png

So far, your build should have been successfully created build and sent to the AppStore (iTunes Connect).

To conclude, if you use GitLab and you need to configure CI service, then GitLab CI additional service is a perfect match. Since this service is adjusted to the work with GitLab, it is a convenient and easy-to-use tool. It allows managing CI server directly in GitLab. Together with Fastlane they constitute a powerful and flexible CI server for iOS on Mac OS.

Share via:

Recommended posts:

Smart Lock: Why Sloth Is A Driver of The IoT Progress
8/16/18
7466

Smart Lock: Why Sloth Is A Driver of The IoT Progress

The non-primitive approach of Amazon: How AWS IoT meets IoT challenges
9/18/18
7769

The non-primitive approach of Amazon: How AWS IoT meets IoT challenges

How can you use internet of things in retail - 5 examples
5/22/19
96

How can you use internet of things in retail - 5 examples

Interested in latest news in IT sphere?

Subscribe to start receiving notifications about new posts
22 May 2019

How can you use internet of things in retail - 5 examples

8 May 2019

7 tips & tricks on how to deal with remote teams

25 Apr 2019

How to create a startup: IoT project from idea to production

22 Apr 2019

5 must visit IoT conferences in 2019