Continuous Integration: Scripting Xcode Builds by Preview

Tutorial DetailsProgram: Xcode, BashDifficulty: IntermediateEstimated Completion Time: 1h
Join to Access

In this tutorial series we will explore a rarely discussed (but highly valuable) process of developing software that is disappointingly absent in the iOS and mobile world: Continuous Integration.


Where We Left Off

In part 1, we discussed the concept of Continuous Integration and how it can assist us in developing software faster. Part 2 went through installing “Apache Tomcat”, the web server that runs our CI server software. In Part 3, we installed and configured Hudson to monitor our project and begin the build process whenever we update our project repository.


Introducing Bash

The main subject matter of this tutorial will be Bash scripting. Bash scripts allow us to automate a great many things such as adding users to a system, iterating and verifying all files in a group of folders, performing automatic archiving of old files, and a lot more. In this tutorial, we will discuss writing a Bash script that automatically builds and signs a “.ipa” file from our Xcode project.

Bash scripts work just like any other script or code you might have written before. We specify terminal commands to be executed in a specific order and the operating system is responsible for running them.


Step 1: Write A Basic Bash Script

As always the best way to learn about something is to jump into it, so lets practice writing a very basic script that will print your name onto into the terminal.

Open a text editor and create a new file on your desktop called ‘BashScript.sh’ end enter the following text:

#!/bin/sh
echo "Hello world from Bash!"

You should be able to figure out what happens in this script. Firstly (and the less obviously) we specify the shell we will be using for the script, in this instance ‘sh’ (if you don’t know what a ‘shell’ is don’t worry too much, its not required to write basic Bash scripts. You just need to know that the line is there for now) The second line simply prints ‘Hello there!’ onto your terminal window.

Let’s try running it. Open a new terminal window and type the following:

. /Users/username/Desktop/BashScript.sh

If you see “Hello world from Bash!” it all worked!

Hello World From Bash!

Step 2: Using Variables In Bash Scripts

Bash scripts can make use of variables but they can be a bit tricky to use if you haven’t done them before, so we’ll cover this very briefly before going any further.

Edit your code to say the following:

#!/bin/sh

name="Frank"

echo "Hello world from $name"

If you execute the script again you should see “Hello world from Frank” (or whatever name you decided to put there). We can also pass variables into the script from the command line. Change your ‘name=”frank”‘ line to:

name=$1

and then execute the script from the terminal window but add a name on the end:

. /Users//Desktop/BashScript.sh Frank

The script will use the variable that was passed in as variable ’1′ (in this case ‘Frank’).

These are the basics of using variables in Bash. Some common slip ups to avoid are:

  • You must use double quotations (“) when printing out a string containing a variables, or it will not work correctly.
  • Variable declarations cannot have a space between the name and value. Eg name = "Frank" will not parse correctly.

Now you’ve got the basics of Bash down! Now to put it to some use.

...and that's the end of the preview!

Unlock the full tutorial by becoming a member of Tuts+ Premium

Including...

  • 800 Exclusive Tutorials

  • Course Library

  • 30 Top-selling eBooks

  • New Content Weekly

Take the Tour or Join Tuts+ Premium

If you've already got a Premium account, just Sign in to Access