Set up GitLab CI with an iOS project that uses Cocoapods

 


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

Comments

Popular posts from this blog

Cách kiểm tra Website trên VPS sống hay chết

Cách đăng nhập VPS bằng SSH trên MAC OS

iOS CI/CD Integration using Gitlab CI and Fastlane