XCode Build and Version numbers based on GIT

Since XCode 4.3 you can find in the Summary Tab of your targets the following fields: Version and Build.

  • Version should be used to identify the “comercial” version of your app, something like 2.0, for more details on how to proper change version numbers have a look at semver.org.
  • Build should be a internal number to unique identify versions of your app.

Normally Version numbers are changed manually every time you prepare a deploy of a new app but Build numbers should change automatically every time you do a new build.
One initial idea could be using the current GIT commit hash number to fill the Build number, but there is a issue here: Apple wants that this number always increases from version to version.
So to go around this I decided to use the number of commits existing on the git repository. This is a number that always goes up while you are developing.

But how to integrate this with XCode and better yet make it automated?

Just follow these instructions:

  • In your Xcode project, create a new Bundle target called UpgradeVersionNumber.
  • Select that target and add a new Run Script phase to the Build phases.
  • Insert the following script in Run Script phase, replacing the path to your git executable if need be:
    # grabbing version number from plist.
    
    COMERCIAL_VERSION=`/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" Info.plist`
    
    # grabbing number of commit in developer to create a version number
    
    BUILD_VERSION=`git rev-list HEAD | wc -l | tr -d ' ' | tr -d '\n'`
    
    # grabbing commit hash from git
    
    GIT_VERSION=`git show --abbrev-commit | grep '^commit' | sed 's/commit //' | tr -d '\n'`
    
    echo "#define GIT_VERSION $GIT_VERSION" > InfoPlist.h
    
    echo "#define BUILD_VERSION $COMERCIAL_VERSION.$BUILD_VERSION" >> InfoPlist.h
    
    touch Info.plist
    
    
  • Make UpgradeVersionNumber a dependency of your main target.
  • Add the “InfoPlist.h” to your .gitignore file.
  • In your main target’s build settings:
  • Turn on Preprocess Info.plist File
  • Set Info.plist Preprocessor Prefix File to InfoPlist.h
  • In your main target select the summary tab and on the Version Number field write BUILD_VERSION
  • Now each time you build the main target, the version will be populated in the build’s Info.plist.
  • As an optional step you can also add a custom entry to the plist called GitVersion and set it with the value GIT_VERSION.
  • Now when the script runs you also gonna get the Git hash number for the build on the Info.plist.
  • You can then use the following code to display it inside the app
[NSBundle mainBundle] objectForInfoDictionaryKey:@"GitVersion"];

Posted

in

by

Comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: