Xcode Build Script for Automatically Updating CFBundleVersion

Since Git doesn’t create numeric build numbers, I haven’t known exactly what to do with my app’s bundle version (CFBundleVersion) since switching to it. According to Apple CFBundleVersion needs to be “a monotonically increased string, comprised of one or more period-separated integers.” My previous (bad) solution was to sort of ignore this and just update when I release a new version. Now that I’ve started using Hockey for beta relases, it requires a unique number for every build, so I need to start paying more attention to it. It’s for the best since it’s better to give this number some significance.

I didn’t want to have to update this manually every time I send a beta build, so I started looking for an Xcode build script that could generate something like this for my ad hoc and App Store builds. Since I didn’t really like any of the code I saw, I decided to write my own using Python, and while I was at it set it to also commit and tag the build in Git.

You can add this as a “Run Script” build phase in Xcode (make sure you set the shell to /usr/bin/python). The only line you should need to update is configurations_list = ['Beta', 'App Store'] to be the names of the configurations you want this to run under. You’ll also need to set your CFBundleVersion to a start point that’s a whole number (0 for example). If you want this to work with a different SCM, just update the os.system lines to run the right commands for your system (or take them out all together if you don’t want that feature at all). For a configuration called App Store, I have the commit message set to be “Automated Commit For Build 25. Configuration: App Store.”, and the tag to be “AppStore_25”. You can change that if you like.

Of course, I take no responsibility if anything bad happens to your project, but it seems to be working great for mine.

Update: There’s a bug where this will cause simulator builds to fail if you don’t set your “Mac OS X Deployment Target” to 10.7 in your build settings.