Automated Xcode Build Numbers, Early 2019 Edition

Thanks to a suggested edit from friend-of-the-blog Todd Heasley I can say that, for now, the automated git-based build number script I've been working on perfecting for years is finally, dare I say it, perfect.

(Where it all started in 2015, the Watch update, and almost perfect in 2016)

The 2016 version of my build number system was so close to what I wanted – it automated my build numbers for every target based on the git commit count and kept my git history clean (no build number bumps in source control). It worked almost everywhere ... except Siri extensions. For those it would never increment the build number before the Info.plist was copied. Which was inconsequential from a production standpoint, in the wild for all intents and purposes it was great. The only issue was you'd always get a warning about mis-matched build numbers on upload, and more annoyingly, an email with the same warning too.‌                                                                        

So the change that made it work now without warnings on upload? Oh, it was so devilishly simple.

Make the build number edit in the build folder, not in your source on the original Info.plist. 🤦‍♂️

git=`sh /etc/profile; which git`
bundleVersion=`"$git" rev-list --all |wc -l`
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $bundleVersion" "${TARGET_BUILD_DIR}/${INFOPLIST_PATH}"

As a byproduct of this change, this also lets you remove the Clear Build Number phase from my 2016 solution as the script never touches your source-controled Info.plist.

Setup Guide

For those who haven't been following along with my struggles, getting started with this build number automation is super simple. For every target you have that gets shipped to App Store Connect, add a build script phase (with an appropriate title like Set Build Number) and copy and past the above code into the script block.

So your targets will look something like this:

And that's it! Enjoy.