DevOpsiOS Development

Continuous Integration and Delivery for iOS using Jenkins and Fastlane

By November 14, 2016 No Comments

What is Continuous Integration?

Continuous Integration (CI) is a development practice, which deals with the integration of new code  into a central repository. Developers check-in the code to the central repository multiple times in a day. Every commit done by a developer triggers a new build and is verified for any errors. This ensures that  errors are caught at the early stages of the commit, instead of manually detecting them at a later stage. Continuous Integration is majorly used for automating builds with latest code integrations. It is used for making the builds self testing, keeping the builds fast and for automating the  deployment.

On the other hand, Continuous Delivery / Continuous Deployment (CD) is a methodology for continuous, repeatable and successful deployment of code. Continuous Delivery makes the release of the product reliable and repeatable. At any point, developers can have a ready-to-be-shipped code that can be delivered without having one more check, as the code has already gone through several stages of testing and checks. This makes sure that a stable product gets delivered to the client, whenever they need it

In this series, we will be covering complete CI/CD for iOS in following parts:

DevOps: Continuous Integration for iOS using Jenkins

Why use Jenkins?

Jenkins, an open source automation server, makes the CI and CD process simple. A developer can create configurable jobs to –

  1. Run certain tests
  2. Build a project
  3. Update the code to a staging or production server

Each job in Jenkins has multiple settings and can be modified according to the requirement.

For example, each project requires separate jobs for Unit testing, UI testing, Static analysis, Code coverage and for building the final product. These jobs can be separately created and configured using Jenkins.

Let us see how to setup Jenkins on a Mac system

Jenkins Setup on Mac Machine

Prerequisites:  Latest JDK should be installed on the machine.

Step 1:
Download Jenkins setup from the official website (download the latest LTS release)

Step 2:
Install the downloaded package. During installation, if prompted, check ‘Install Jenkins for all users’ and proceed.

Step 3:
Once installation is finished, a ‘Jenkins’ user is created in the system user group. You need to execute certain terminal commands to give the Jenkins user proper permissions.

Open the terminal and run following command:

sudo dseditgroup -o edit -a jenkins -t user admin

This command makes ‘Jenkins’ the user admin, thus giving it admin rights.

Step 4:
Next, we need to move the ‘Jenkins’ user to the developer group. Execute the below command in the terminal

sudo dscl . append /Groups/_developer GroupMembership jenkins

Step 5:
Now, go to  System Preferences -> Users & Groups. Click on lock icon and authenticate yourself. You will find an empty user with Admin privileges in the ‘other users’  section in the left pane. Right click on it and go to ‘Advanced Options’. You will find that the account name is ‘Jenkins’ and its full name is empty. Fill the full name with ‘Jenkins’ and click on ‘Ok’

Step 6:
Click on ‘Jenkins’ user in the left pane and reset password for the ‘Jenkins’ user with whatever password you like.

Step 7:
Restart your machine

Step 8:
Login to ‘Jenkins’ user with the password you set in Step 6.

Step 9:
Once you are logged in, you need to execute few more terminal commands and create a new keychain so that the Jenkins CI can use that keychain to sign applications.

Creating a keychain for Jenkins CI: Open ‘Keychain Access’ from the spotlight search. Under ‘Keychains’ section in the top left corner, you can find available keychains. Right click anywhere and click on ‘New Keychain’ to create a new keychain called ‘Jenkins’ (keep the save path to default). You will be asked to set a password for the newly created keychain, proceed and do so. Right click on the ‘Jenkins’ keychain to set it as default.

Step 10:
By default, Jenkins CI server runs as a launch daemon, which cannot invoke UI terminals like Simulators (useful during execution of test targets like UnitTests and UITests). So, we need to move the Jenkins .plist file present in the LaunchDaemon folder to LaunchAgents folder. Launch agents are the agents that do not run automatically and need to be invoked by the user. In order to do so, open terminal and run the below commands one by one.

sudo launchctl unload -w /Library/LaunchDaemons/org.jenkins-ci.plist
sudo mv /Library/LaunchDaemons/org.jenkins-ci.plist /Library/LaunchAgents/
sudo launchctl load -w /Library/LaunchAgents/org.jenkins-ci.plist

Step 11:
Once commands are executed, go to http://localhost:8080 in your browser. The Jenkins dashboard should appear. If it does not, go to Finder -> Applications -> Jenkins -> double click jenkins.war. Refresh the localhost URL.

Step 12:
In the terminal, you need to execute the below command to generate public and private key pair. These keys will be useful when accessing Git through SSH.

ssh-keygen -t rsa -C "”

(replace the email id with your Git email id. It is better to maintain a separate Git email id for Jenkins CI server. If it asks for any file name, hit enter multiple times.)

With this, the basic Jenkins setup will be completed.

Jenkins allows to create and configure jobs separately. However, the drawback of creating multiple  jobs for a single project is that it becomes time consuming. Depending on the size of the project, the task of modifying settings and reconfiguring all the jobs can take a lot of developer’s time. This is where a single pipeline job wins over the multiple jobs in Jenkins. The pipeline job is created using Groovy script which has several stages with each stage executing a shell command. The single job will have multiple stages in it, and this is where the maintenance task becomes simpler. We will cover all of this in more details, stay tuned.

Leave a Reply