Set up GitLab CI with an iOS project that uses Cocoapods

Roland
Roland
Nov 3, 2018 · 3 min read
Image for post
Image for post

We all write tests for our applications and keep them in version controlled systems like GitLab or GitHub. However, how can we configure GitLab to run Unit and UI tests for us?

I couldn’t find a lot of documentation explaining how to set up continuous integration for iOS on GitLab, therefore I am writing this article.

Victor Peschenkov explains in his article how to use GitLab CI with Fastlane. My article focuses on how to run everything yourself.

Requirements

Please install these tools now before continuing.

Configure Xcode

iOS Project

  • git checkout git@gitlab.com:skofgar/ios-ci-sample.git
  • switch into the project folder and run pod install
  • run the following command on your machine to verify that you can build and run the tests successfully — which means no “failures” or other error messages
xcodebuild test \
-workspace "CI Sample.xcworkspace" \
-scheme "CI Sample" -destination 'platform=iOS Simulator,name=iPhone XR,OS=12.0' \
| xcpretty -s

Installing GitLab Runner

Install GitLab Runner on a Mac

  • use the command brew install gitlab-runner
  • go to your GitLab instance to Settings > CI/CD
  • disable shared runners (alternatively you can use tags to only use your Mac for building the iOS project)
  • go toSpecific Runners and save your GitLab server url and token
Image for post
Image for post
GitLab CI/CD Settings page showing URL and token for specific runner
(token has been removed on purpose)
  • register a your runner as described in GitLabs documentation
  • gitlab-runner register
    1. when prompted, enter theurl from above
    2. next up, enter the registration token
    3. name your runner
    4. leave the tags blank
    5. use the shell executor
  • start your runner
cd ~
gitlab-runner install
gitlab-runner start

Please note, should you restart your computer you’ll likely have to start the GitLab Runner again.

Reload the CI/CD settings page. If everything worked out, you should be able to see your runner in the Specific Runners section:

Image for post
Image for post
Listing of successfully activated specific GitLab runners

Create gitlab-ci.yaml

stages:
- build

variables:
LC_ALL: "en_US.UTF-8" # apparently xcpretty won't function correctly otherwise

before_script:
- gem install cocoapods
- pod install

build_project:
stage: build
script:
- xcodebuild clean -workspace "CI Sample.xcworkspace" -scheme "CI Sample" | xcpretty
- xcodebuild test -workspace "CI Sample.xcworkspace" -scheme "CI Sample" -destination 'platform=iOS Simulator,name=iPhone XR,OS=12.0' | xcpretty -s

As soon as you commit this file, GitLab-CI should kick in and start building your project and then run the tests.

That’s it you’ve done it!

Feedback

Resources


Originally published at Skofgar’s Blog.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store