📜 ⬆️ ⬇️

Automatically build iOS applications on different versions of Xcode using Jenkins

If you have already been approached with the question “Where can I get a fresh build?”, Then you perfectly understand why assembly and distribution automation is needed. Nobody wants to spend extra time on routine work. We used to use a utility called iOSBetaBuilder ( http://www.hanchorllc.com/betabuilder-for-ios/ ). This application is designed to simplify the distribution of AdHoc assemblies of an iOS application: you just need to enter the name and version of the project, the address (URL) where you want to lay out the assembly, and the generated index.html and manifest.plist are obtained. For the first time this is enough.

But when the project reaches the stage of bug fixing, spending the extra 5 minutes to build and republish for QA is reluctant and there is no time. And when there are a lot of projects, and their assemblies become longer ... Within the company, time spent multiplied by the number of projects becomes too substantial, and automation time comes.

In this article we will explain how to set up automatic building of iOS applications, sending email notifications and publishing applications on an FTP server for testing and demonstration to the customer.
')
For those who are already in the subject, there is an interesting section at the end of the article: how to configure builds with different versions of Xcode on the same machine.

How to set up automatic building of iOS applications using Jenkins?

You need to install Jenkins, add and configure several plugins, add an agent to build and configure OTA distribution. If you want to receive more notifications by mail, then you need to configure an additional plugin.

So.

What is Jenkins? Jenkins is a continuous integration server. Jenkins is written in Java and has a large number of plug-ins to extend the functionality. All the necessary information for installation can be found here https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins

To search for plugins - https://wiki.jenkins-ci.org/display/JENKINS/Plugins , but Google also does a good job when you know what you want to find.

The architecture of our system is the following:




To implement this system, we need the following plugins:


Plugin configuration

Consider the example of building a specific application. We have a project whose source code is in the Git repository:
git@github.com: ShareKit / ShareKit.git

We want to collect it and put it on our internal server for distribution via the Internet. Jenkins access to the server is via FTP, Apache is configured on the server, and everything we post is automatically available to our QA department or customers on a special site.

FTP
To configure the plugin for FTP after installing it, you need to open the Jenkins settings page:
Jenkins -> Manage Jenkins -> Configure System
In the settings, find the section about FTP and register the information for access to the server.



Save.

Git
To set up Git, you need to add a Git installation description on the page
Jenkins -> Manage Jenkins -> Configure System


The path indicates which command Git will run on the agent. You must specify the full path where git is located on the machine where Xcode is installed. You may receive an error message, but do not pay attention to it, because Jenkins checks the path to Git on the local machine, and not on the remote, where we will use it.
We save.

Xcode Agent
For any Jenkins agent to work on the remote machine, you need to add credentials to access the remote machine on the settings page

Jenkins -> Manage Jenkins -> Manage Credentials


You also need to add the public key of the machine on which Jenkins is installed to the authorizedKeys folder of the machine on which Xcode is located. Username in the form must match the username under which account will run Xcode. The path to the file with public keys in our case would be: /Users/jenkins/.ssh/authorizedKeys

In our example, Xcode is installed on the machine with the IP address 192.168.154.24.
To add an agent that will be used to build a project, on the node list page
Jenkins -> Manage Jenkins -> Manage Nodes
Click on New Node, call our node XcodeAgent, select Dumb Slave, click OK.


The number of performers can be set any, depending on the power of the machine and the load. We do not have many applications, and one thread copes well. The directory should be accessible to the user, in our case - / Users / jenkins / jenkinsCI
Specify the IP address of the machine, and select our Credentials:


After we clicked Save, you can proceed to creating a Job.

Build Setup - New Job

Create a new task:
Jenkins -> New Job


Enter the name of the task, select the type Build a free-style software project. Click OK. In the future, to create tasks similar to existing ones, the “Copy existing job” will be the best option.

In the Source Code section, select Git, specify the repository address (ShareKit is shown as an example in the image) and Git executable, which we configured earlier:


Fill in the remaining settings:


With what frequency we want to poll the version control system for updates - we set "every minute". As you can see, Jenkins suggests using the H parameter, which means that the survey will occur 1 time per hour at a random minute to reduce the load.


Add Build Action: Xcode


The configuration is usually pretty simple. We use AdHoc profiles to build applications for testing and demonstration to the customer.

Add another Build Action. This time we are interested in publishing the application on our server - via FTP:




Add mailing notifications by mail

If you have a mail server, on Jenkins you can set up a mailing list based on the results of the assembly.
First on the settings page
Jenkins -> Manage Jenkins -> Configure System
specify the email address from which users will receive emails:


We specify the address of the SMTP server, the port and the same email.


The plugin we installed adds another settings section:


We can specify the format of the subject line and the template used. All templates are in the file system. Details about templates can be found on the plugin page https://wiki.jenkins-ci.org/display/JENKINS/Email-ext+plugin#Email-extplugin-TemplateExamples

In addition, the plug-in allows you to set a list of recipients and different configurations for each build status: for example, if the application is not assembled - a notification is sent to the developers, assembled - to testers. In the picture an example for statuses failed build and repair.

After that we add to our build Post-build Action:


and configure:


Save, run and enjoy!

Second xcode

Everything is good when you first started doing the project and support only the latest versions of iOS, but as time goes on, the world changes, and Apple releases a new iOS and a new Xcode for it. The customer asks to support what was and what is, and the question arises of the need to correct the bugs of the old version while a new one is being developed.
Understandably, we create a new brunch, hang tags and create a new address for publishing a test version. What about Xcode?

The plugin does not support switching between versions of Xcode, so we have two (maybe someone will come up with more - but we could not) options:
  1. set up a second machine with Xcode and add an agent to Jenkins
  2. get Jenkins to switch Xcode using shell script


We went the second way.
You can change Xcode using a fairly simple script:
sudo xcode-select -switch $ XCodePathToDeveloperFolder
- to switch installed Xcode, you must have root rights
However, when running ssh commands, there are difficulties with the password and you can get the following error:
sudo: no tty present and no askpass program specified

This is because the commands on the Jenkins agent are run via ssh, which does not provide a terminal.
To solve this problem, you need to create a file, for example, / Users / jenkins / pass, in which you can place the password of our user.
It is necessary to add through the pipeline to the script for switching Xcode a command for outputting the contents of our password file:

cat / Users / jenkins / pass | sudo xcode-select -switch /Applications/XcodeVX.X.app/Contents/Developer


This command in the form of the Build step - Execute shell is added by the first step in the Build section of our task - before Xcode.

After starting the task, the Xcode will change, and all other tasks configured on the old version of Xcode will be collected incorrectly. To avoid this, you must return the Xcode, which is configured to other projects.
It would be possible to add the same command after executing Xcode, but if the assembly breaks, the execution of the Build step sequence will be interrupted.
You need to add Post-build action - Post build task, and return everything to the place:


We collect. Everything works fine.

It is worth noting that the number of executors that we specified when configuring the agent may affect the build, if you put more than 1. Then for all the builds you will have to add an Xcode switch to the correct version.

Conclusion

Source: https://habr.com/ru/post/192784/


All Articles