iOS | CI on fastlane and github actions [1]

Yessen
Deem.blogs
Published in
4 min readApr 22, 2022

--

CI is very important concept in software development. You probably have heard about it before from somewhere, or if you are lucky enough your team is already using it!

Every time we try to accomplish any particular task, we do more or less significant changes to our code base, which sometimes can affect the performance of other parts of our project. In order to prevent any unexpected crashes or bugs, which were caused by changes in different part of code base, it is critical to add automated tests on each change. Besides that, automatic testing helps you to find out bugs in your code relatively faster, since your log will show explicitly where your project crashed!

In current article, I will explain how to setup automatic unit testing on every Pull request to main/developer branches in github using fastlane and github actions.

So before starting, let me briefly explain basic git flow for automatic testing. I usually tend to follow this approach. In here, important branches are called ‘main’ and ‘developer’. Shortly saying, you should push your code to main only when you do new release, and new features, bug fixes, or refactorings should be developed in sub branches from ‘developer’ branch, and then pushed to developer by opening PR. In our case we will create a script which will run automatic unit tests every time developer opens PR to develop branch, so we can safely merge new changes in case of successful tests!

Let’s suppose you already have your github repository with xcode project inside of it, and already using proper git flow inside of it. Besides that, you also should have couple of unit tests in your project, with already built test scheme. If not, this article should help you out.

For starting integrating CI, we should install fastlane to it. Open your terminal, navigate to your xcode project, and type command

brew fastlane init

For that, you need to have home brew installed into your mac. After fastlane installation finishing, you will have fastlane configured in your mac!

Now type this command

fastlane init
You should see this log

You can choose pre defined functions for your fastlane, but since we gonna use it only for testing, we are good to go with number 4.

Fastlane will walk you through couple of documentation links, and suggestion. I recommend to checking up on them for more advanced understanding of fastlane. However, for now you can just continue by tapping enter button.

Now you can see fastlane folder inside of your xcode project!

It should look like this:

Inside of that folder you can see Appfile and Fastfile. Fastfile is the place where all of the magic will happen! type on your terminal following commands:

cd fastlane
open Fastfile

You should see ruby file opened in front of you after it.

You can consider ‘# add actions here: ‘ part as your function’s(lane’s) body, where you should write code which will tell fastlane what to do. Let’s change our lane’s name and description to appropriate test.

So now our lane’s name is test. Whenever we will want to run this lane, all we have to do is type command

fastlane test

Now let’s do the funny part, and write test lane what it should do.

In here, we tell our lane to begin with run_test function, the description of which you can find in official fastlane documentation website. Fastlane is popular tool for CI/CD, which means that it has huge support from developers, and have a lot of supporting functions, which we do not have to write by ourselves! run_tests is one of them. All we need to do, is to pass the name of our test scheme, and device on which we want to test. Here, I just copied the scheme name, and decided to test on iPhone 11.

I also have private functions on_success_ci, and on_error_ci(exception), basic functions of which to inform you about test status. In our team, we use slack, so on success it will send successful message to our private channel, and failure reason on error case.

Detailed explanation of slack messaging from fastlane you can find in here. For the security reasons, I can not show the code for unfortunately. However, as documentation is well written, you can catch up with idea pretty fast.

Now let’s test it locally!

Save your file, and go back to your terminal. Write command:

fastlane test

You should see something like this at the end.

That’s it! Part with fastlane is done. Next step will be setting up our github actions.

I will be talking about it second part of this article!

For any inquiries, you can contact me here

--

--