Show Menu

Do you have a job opening?

Submit your 30 day Job Listing for FREE

In this post I’ll show you how to automatically update your build numbers with every release build!

Xcode provides two places to keep track of your app’s revisions: version and build. To see them, click on your Project name in the Navigator, and then click on your main build target under Targets. Click on the General tab. First, an overview:

Version (CFBundleShortVersionString) is an arbitrary label to specify a build’s relationship to other builds from the perspective of external users or API consumers. According to Semantic Versioning, it is usually in the format MAJOR.MINOR.PATCH, where:

– MAJOR version when you make incompatible API changes
– MINOR version when you add functionality in a backwards-compatible manner
– PATCH version when you make backwards-compatible bug fixes

Build (CFBundleVersion), on the other hand, is used to identify the exact version of a binary executable from the perspective of developers and testers (and the App Store). In a continuous integration environment, this can simply be an integer value that is incremented every time by the build server. However, in a distributed build environment, auto-incrementing build numbers doesn’t really work. Multiple developers build on their machines multiple times per day, so build numbers can become out of sync unless immediately checked in to source control. A more reliable method would be to use source control to generate the build numbers. Could we simply use the “short” SHA hash of the latest git commit?

git rev-parse --short HEAD

The problem is that App Store requires the build number to be an “monotonically increasing string, comprised of one or more period-separated integers.” So instead, let’s count the number of git commits:

git rev-list HEAD | wc -l | tr -d ' '

Perfect! Now let’s make a build script to automatically update the build number in our Info.plist file whenever compiling a release build. Click on your Project name in the Navigator, and then click on your main build target under Targets. Click on the Build Phases tab. Go to Editor > Add Build Phase > Add Run Script Build Phase. Copy and paste the following code over the text that says ‘Type a script or drag a script file from your workspace to insert its path.’

#Update build number with number of git commits if in release mode
if [ ${CONFIGURATION} == "Release" ]; then
buildNumber=$(git rev-list HEAD | wc -l | tr -d ' ')
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "${PROJECT_DIR}/${INFOPLIST_FILE}"

Note: Remember to only generate release builds when working in your release branch in git, typically master. Otherwise you will be using commit counts from another branch. (You really shouldn’t be making release builds from non-release branches anyway!)

having issues?

We have a Questions and Answer section where you can ask your iOS Development questions to thousands of iOS Developers.

Ask Question

FREE Download!

Get your FREE Swift 2 Cheat Sheet and quick reference guide PDF download when you sign up to SwiftMonthly

Sharing is caring

If you enjoyed this tutorial, please help us and others by sharing using one of the social media buttons below.

Written by:

Martin is a New York area software developer and MBA with 10+ years of experience on the Microsoft stack. Over the past few years he has also expanded into iOS development using native Objective-C. he architects and develops full-stack web applications, iOS apps, database systems, and backend services.