News & Blog

Using Jenkins for mobile projects - Part 1 (iOS)

05.11.2015 | Entwicklerblog

I bet everyone who develops software for his living should at least have heard something about terms like Test-Driven development, Continuous Integration and Continuous Delivery. In a world were more and more people are coding and requirements for software get more complicated, especially in terms of maintainibility and extensibility, you need to have your tools to make sure that everything is going the right way.

Now having a CI Server like Jenkins, Travis or Bamboo is a big part of that, but also a very tricky part, at least if you are going for mobile projects. Most of the time you are developing for multiple platforms, sometimes you have major changes in the way of building projects (Android changing from Ant to Gradle) and of course you need to maintain the stuff.

I want to show you what I experienced while setting up a new Jekins-Node based on OS X for building iOS projects with metrics like SLOCCount (Single lines of code), CPD (Code duplication), performing Unit tests and getting the test coverage.



Clean install of your Jenkins OS X Node

First step - install Homebrew

The first thing you should do on a freshly installed OS X system is installing Homebrew. It makes Life so much easier because you don’t need to care for installing packages any more. Just head to http://brew.sh/, copy and paste the one-liner and that’s it! Additionally you should install Homebrew Cask to extend Homebrew by being able to install OS X applications and large binaries. Just type the following in your terminal:

brew install caskroom/cask/brew-cask



Second step - General tools and metrics

For getting your projects from the version control system of your choice, you need to install either git or mercurial (you shouldn’t use any other).

brew install git
brew install mercurial

Since we want to know the quality of our code, we need to implement some metrics to measure it. The ones we want to use are very common and give you a good feeling of the job you did.

Code metrics help you improve and maintain the quality of the source code you produce. SLOCCount (image on the left) shows you the single lines of code of your project, CPD shows you how much code duplication you have.

These tools should be used a lot in order to reduce redundancy and have a clean architecture.

SLOCCount

This one counts the single lines of code a project has in sum. Just tells you how much you coded!

brew install sloccount

This is an example of how to use sloccount to show all code duplicates in a given project folder, ignore some files and export the result to a file.

sloccount --duplicates --wide --details "PROJECT_FOLDER" | grep -v -e 'IGNORE_1' -e 'IGNORE_2' > "REPORTING_FILES_FOLDER/sloccount.sc"



CPD

With CPD you can detect duplicated code. If you did use copy-paste a lot it will be recognized. CPD is part of PMD.

brew install pmd

CPD can be used like this, giving it a project folder, the programming language and setting the output folder for the result.

pmd/bin/run.sh cpd --files "PROJECT_FOLDER" --minimum-tokens 50 --language "LANGUAGE_NAME" --encoding UTF-8 --format net.sourceforge.pmd.cpd.XMLRenderer | iconv -f macRoman -t utf-8 | sed 's/MacRoman/UTF-8/g' > "REPORTING_FILES_FOLDER/cpd-output.xml"



gcovr

Writing unit tests is important, and you should always know how much of your code is unit tested. This is exactly what gcovr does, it tells you how high your test coverage rate is.

brew install gcovr

Here we point to the build intermediates of iOS.

gcovr --object-directory="REPORTING_FILES_FOLDER/build_ccov/PROJECT/Build/Intermediates PROJECT.build/Debug-iphonesimulator/ PROJECT.build/Objects-normal/i386" --root=. --xml-pretty --gcov-exclude='.*#(?:ConnectSDKTests|ExternalFrameworks)#.*' --print-summary --output="REPORTING_FILES_FOLDER/coverage.xml"



Third step – iOS projects 

For building iOS projects we need Xcode, the default IDE from Apple for iOS and OS X development. So install Xcode from the Mac App Store, accept the license agreements, go to Xcode -> Preferences -> Downloads and install all the components you need.

Now we could build iOS projects, but to be able to run our XCTest Unit tests and generate test coverage files, we need to install xctool, a replacement for Apple’s xcodebuild:

brew install xctool



Now we need to establish the connection between the OS X Node and the Jenkins server. There are several ways to do this, we do it via Java Web Start. You need to download the jar-File to execute from your Jenkins server at this path:

http://yourserver:port/jnlpJars/slave.jar 

If you didn’t already create the node on the Jenkins you should do this now. When it’s created you should see all the nodes in the lower left corner. Click on the newly created one (it should be stated as „offline“) and on the detail page of the slave you see the command that you should execute on your terminal on the OS X machine.

 

Now for the following steps you need to install the following Jenkins Plugins to be able to add certain build steps for your projects:

- Cobertura Plugin
- Credentials Plugin
- Duplicate Code Scanner Plug-in
- JUnit Plugin
- SLOCCount Plug-in
- Xcode Integration



Apple developer profile

One thing that can really be annoying is the code signing with Xcode. But there is one simple solution: an Apple developer profile!

This means that you export all the stuff you need for code signing (Signing key, certificate, provisioning profiles) as one file that can be used by Jenkins.

 

How to export this file:

Xcode -> Preferences -> Accounts, click on the Settings item in the lower left corner and select „Export Accounts“. Enter a secure password, and that’s it!

20150724_Blog_devprofile.jpg

 

Now on Jenkins, you have to select „Credentials“ on the main page on the left, create a domain or use your existing one and select „Add Credentials“ on the left side. Select „Apple Developer Profile“ as Kind, enter a description, upload the exported file and enter the Password, and you’re done!



Configuring the Jenkins project

Now comes the last part, the configuration of the Jenkins project. At first you need to configure the project itself, the most important steps are:

- Check „Restrict where this project can be run“ and set the correct node name/label
- Check the correct Source Code Management system and add the respository URL
- Define when the project should be built (on every VCS push, nightly, every hour, ...)

Build steps

1. Import developer profile

Add build step, select „Import developer profile“ and select your previously imported Apple developer profile.

20151104_buildstep_1.png

2. Execute shell

This is needed somehow to make the keychain visible to the system after it got imported. Currently this is some kind of workaround, I don’t know if this can be done in a better way.

20151104_buildstep_2.png

3. Xcode

Add build step, select „Xcode“. The settings should be clear, the important ones are „Target“ (select your build target), „Clean before build“ (should be enabled), „Configuration“ (Debug/Release) and under „Advanced Xcode build options“ you can specify a certain SDK, the workspace file, the project directory and so on.

20151104_buildstep_3.png

4. Execute shell

This time we need to perform all the metrics. In this example we simply perform all the metrics seperated by semicolons, that means that the commands will run respectively.

20151104_buildstep_4.png

Post-build steps

1. Show sloccount results

Publish the results of the SLOCCount metric by simply pointing to the file that should have been created.

20151104_postbuild_1.png

2. Show CPD results

The same as for SLOCCount, simply point to the file. Additionally you can define tresholds for warnings.

20151104_postbuild_2.png

3. Show JUnit Test results

This one will show the results of the Unit tests. Same as for the previous ones, point to the file.

20151104_postbuild_3.png

4. Show Cobertura Test coverage

This one is very cool, it simply reads out of the XML-File generated for the test coverage and shows you how much percent of your code is covered by Unit Tests.

20151104_postbuild_4.png

Now you should be able to set up your iOS-Node for Jenkins. In the next part we will do the same for Android, and hopefully you will be able to setup your whole continuous integration ecosystem.