Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Xcode Auto-increment Build & Version Numbers
# xcode-build-bump.sh
# @desc Auto-increment the build number every time the project is run.
# @usage
# 1. Select: your Target in Xcode
# 2. Select: Build Phases Tab
# 3. Select: Add Build Phase -> Add Run Script
# 4. Paste code below in to new "Run Script" section
# 5. Drag the "Run Script" below "Link Binaries With Libraries"
# 6. Insure that your starting build number is set to a whole integer and not a float (e.g. 1, not 1.0)
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${PROJECT_DIR}/${INFOPLIST_FILE}")
buildNumber=$(($buildNumber + 1))
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "${PROJECT_DIR}/${INFOPLIST_FILE}"
# xcode-version-bump.sh
# @desc Auto-increment the version number (only) when a project is archived for export.
# @usage
# 1. Select: your Target in Xcode
# 2. Select: Build Phases Tab
# 3. Select: Add Build Phase -> Add Run Script
# 4. Paste code below in to new "Run Script" section
# 5. Check the checkbox "Run script only when installing"
# 6. Drag the "Run Script" below "Link Binaries With Libraries"
# 7. Insure your starting version number is in SemVer format (e.g. 1.0.0)
# This splits a two-decimal version string, such as "0.45.123", allowing us to increment the third position.
VERSIONNUM=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "${PROJECT_DIR}/${INFOPLIST_FILE}")
NEWSUBVERSION=`echo $VERSIONNUM | awk -F "." '{print $3}'`
NEWSUBVERSION=$(($NEWSUBVERSION + 1))
NEWVERSIONSTRING=`echo $VERSIONNUM | awk -F "." '{print $1 "." $2 ".'$NEWSUBVERSION'" }'`
/usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString $NEWVERSIONSTRING" "${PROJECT_DIR}/${INFOPLIST_FILE}"

Thank you! Will test them right now :-)

alokc83 commented Mar 18, 2013

Thanks !! Its really awesome. Just the same I was looking. Works as charm. Just wondering do I have to set $2 by myself. there is no increment thing for that. actually I am not sure when to increase $2. If you can point me in right direction.

These need to be pre-build actions, otherwise Xcode will only copy the updated plist every other build.

To do this:

  1. Edit Scheme
  2. Expand "Build" section by clicking disclosure triangle
  3. Select "Pre-actions"
  4. Add "New Run-Script Action"
  5. Select "Provide build settings from $YOUR_PROJECT"
  6. Add an increment script

guizmo commented Jul 14, 2013

xcode-build-bump.sh works like a charm!

nebiros commented Jul 30, 2013

At Xcode 4.6.3 (4H1503) I got this error:

/Users/nebiros/Library/Developer/Xcode/DerivedData/ZwingIt-glpdhlyoebaqcfdefdfwkgxjapnj/Build/Intermediates/ZwingIt.build/Debug-iphoneos/ZwingIt.build/Script-048F0D0017A7FDD9000E38C5.sh: line 3: File Doesn't Exist, Will Create: /Users/nebiros/Dropbox/Projects/zwingit-ios-2//Users/nebiros/Dropbox/Projects/zwingit-ios-2/ZwingIt-Info.plist + 1: syntax error: invalid arithmetic operator (error token is "'t Exist, Will Create: /Users/nebiros/Dropbox/Projects/zwingit-ios-2//Users/nebiros/Dropbox/Projects/zwingit-ios-2/ZwingIt-Info.plist + 1")

Seems like ${INFOPLIST_FILE} var has all the file path, so ${PROJECT_DIR} is not necessary.

@nebiros, I got the same error and removed ${PROJECT_DIR} also. However, to make it more robust I played with it a little more and found "${PROJECT_DIR}"/"${INFOPLIST_FILE}" works. Not a bash expert so I don't know why.

Works like a charm :)

great

ferbass commented Oct 31, 2013

Works in Xcode 5 thank you

evliu commented Nov 6, 2013

super simple. thanks!

In Xcode 5, method of adding script have changed. Please update the comments as per instructions given on the following link.

https://developer.apple.com/library/ios/recipes/xcode_help-project_editor/Articles/AddingaRunScriptBuildPhase.html

thank you! all good with xcode 5 & 7.1

thank you ~ running with xcode 5.1.1 & ios 7.1

Seems like you're reinventing the wheel a little bit here. Why not make use of agvtool?

https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/agvtool.1.html

Edit: I removed the $PROJECT_DIR variable & it worked. Thanks!

I'm getting this error:

Print: Entry, "CFBundleShortVersionString", Does Not Exist
Parse Error: Unclosed Quotes
Value Required for Set Command
Command /bin/sh failed with exit code 1

Xcode 5.1.1, iOS 7.0. My Version number is "1.1.14"

works on Xcode 6.1 & iOS 8.2

@wyattisimo

avgtool does not support git, or is there a newer version that does?

kiennt commented Jul 11, 2015

On Xcode6.3.1, I need to change ${PROJECT_DIR}/${INFOPLIST_FILE} to $PRODUCT_SETTINGS_PATH

Thanks, It's working fine on Xcode 6.4

SLboat commented Aug 3, 2015

Grest tool!

rswilley commented Aug 9, 2015

Works like a charm

troya2 commented Aug 11, 2015

I've tried using agvtool, but ran into config variable issues. Does someone have a working example of using agvtool in Xcode build settings?

Hi,I wrote a Xcode Team Adding Build Number Shell Script Here:
TeamBuildNumberScript

KBvsMJ commented Oct 10, 2015

Thanks, It's working fine on Xcode 6.4 & iOS8.4

Thanks, it's work well!
Xcode 7.0.1 and iOS 9.0

good on Xcode 7.1, iOS 9

This was perfect for me. Albeit I customized mine a little bit to my needs because I wanted date/time. Thought I would share incase someone wanted something like mine.

'''Adding run script in build phases that will auto generate a build number, IE: 1511021323 (year, month, day, hour, minute) that will be used in conjunction with HockeyApp releases.'''
timestamp() {
#date +" at %H:%M:%S on %m/%d/%Y"
date +"%y%m%d%H%M"
}

buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${PROJECT_DIR}/${INFOPLIST_FILE}")
buildNumber=$(timestamp)
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "${PROJECT_DIR}/${INFOPLIST_FILE}"

helsont commented Jan 21, 2016

+1 Great!

+100 Excellent

drulang commented May 27, 2016

@nebiros +1 for xcode 7.3

welemski commented Jun 9, 2016

cool! Just what I needed
Thanks!

aminbenarieb commented Aug 17, 2016

Cool, i used this to add debug suffix to my version.

if [ "${CONFIGURATION}" == "Debug" ]; then

    VERSIONSTRING=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "${INFOPLIST_FILE}")

    VERSIONSTRING = "$VERSIONNUM debug"

    /usr/libexec/PlistBuddy -c  "Set :CFBundleShortVersionString $VERSIONSTRING" "${INFOPLIST_FILE}"

fi

Someone knows a better approach?

Thanks! Works on Xcode 8.1

jVirus commented Dec 12, 2016

Brilliant, thank you!

nguyentanhungbk commented Dec 20, 2016

work well. Thank you

Thank you so much ! it worked : )

tobeyun commented Apr 30, 2017

Awesome! Worked brilliantly.

Good work!

Jusung commented May 26, 2017

Work well like charm in Xcode 8.3.2

cusster commented Aug 15, 2017

I may missed something out when setting up my project, but the above path, ${PROJECT_DIR}/${INFOPLIST_FILE}, throws an exception on my end. I noticed that the ${INFOPLIST_FILE} contains absolute path to my project's Info.plist. Removing the ${PROJECT_DIR}/ did it for me. Using Xcode 8.3.2

Thanks @cusster that worked for me also

freegor commented Oct 15, 2017

Thanks @cusster! Have a nice day.

Still works in Xcode 9.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment