Using Gitlab CI/CD + Fastlane for iOS Project — Part 1
In this tutorial, I will introduce to you how to setup CI/CD on GitLab for iOS project. Through this article, you will approach some concepts as what is CI? What is CD? Why do we use CI/CD in the iOS project?
What is CI?
Continuous integration (CI) is the software engineering practice of frequently integrating isolated changes to a larger code base and testing them immediately.
What is CD?
Continuous Delivery (CD) is the ability to build software that can be released to production at any time.
Why should you use CI/CD in your iOS Projects
You can consider the work flow from start code to release a new feature as the followings:
- Perform regression test
- Increment version/build version
- Take care of signing and provision
- Upload the build to testing service like Hockey or TestFlight.
- Upload to iTunes connect
If your timeline update app on short release cycle, you have to repeat all steps above very frequently, and you are likely wasting a lot time.
We will start setting up automation this cumbersome process with Gitlab CI/CD right now!
Getting started with GitLab CI/CD
You can check out the simple project on my githup then you push to your gitlab to practice in this tutorial.
Are you ready? Okay, let’s go!
First, we will add .gitlab-ci.yml file to the root directory of project by terminal:
$ touch .gitlab-ci.yml
This file will tell with Gitlab runner what to do. Gitlab CI use Runner to run the code defined in .gitlab-ci.yml
Now, we will install Gitlab Runner on MacOS.
- Download the binary for your system:
$ sudo curl — output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-darwin-amd64
2. Give it permissions to execute:
$ sudo chmod +x /usr/local/bin/gitlab-runner
After you installed Runner, you need to register runner for your project.
- Run the following command:
$ gitlab-runner register
2. Enter your GitLab instance URL:
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com) https://gitlab.com
3. Enter the token you obtained to register the Runner. You can get your token in Gitlab Setting -> CI/CD -> Runner
Please enter the gitlab-ci token for this runner
Your token
4. Enter the tags associated with the Runner, you can change this later in GitLab’s UI:
Please enter the gitlab-ci tags for this runner (comma separated): ios
5. Enter the Runner executor:
Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:
shell
Awesome!
Then we will use Fastlane tool to run automatic our process. To get more understand about Fastlane you can prefer on their website. In the scope of this article, I will don’t tell too much about Fastlane to avoid the article too long.
I will declare some actions of Fastlane in this article:
- Match: Automatically sync your iOS keys and profiles across all your team members using git
- Swiftlint: A tool to enforce Swift style and conventions
- Xcov: generate nice looking code coverage reports
- Hockey: upload build for testing
Now, let move in root directory and create Gemfile to install some gems in the project:
$ touch Gemfile
Open Gemfile and add Fastlane gem.
source “https://rubygems.org"
gem “fastlane”
We will install Fastlane gem:
$ bundle install — path vendor/bundle
Okay! Once Fastlane gem installed in our bundle, we can initialize Fastlane file to define some services in automatic process.
In terminal and type command:
$ fastlane init
Type 4 number to manual setup Fastlane. Then just press Enter to finish process.
After your processing is completed, you will see fastlane directory in your project. It contains 2 files: Appfile and Fastfile. We will use Fastfile to declare our process later.
Right now, we will install Swiftlint to make sure Swift style and conventions by CocoaPods. Open Gemfile to add cocoapod gem:
gem “cocoapods”
You run bundle install in terminal. You will see cocoapod gem is installed.
We will continue to create Podfile in root project to install Swiftlint pod.
$ pod init
Then you will see Podfile is created in the project, let open it to add new pod.
pod ‘SwiftLint’
Come back terminal and type command the following command to install pod.
$ bundle exec pod install
Then, we will add script to swiftlint can run on the project. Open .xcworkspace file, in Build Phases, you will see ‘+’ button in top-left corner, press it and choose New Run Script. After that, you will add the script as the followings:
if which swiftlint >/dev/null; thenswiftlintelseecho "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"fi
Okay cool! Now you try to run the project and you will see a lot of warnings and errors that some errors are not in your code.
Don’t worry! We will except this files that do not need to check style convention in next tutorial.
Preferences: