Skip to content

Instantly share code, notes, and snippets.

@sekati
Created July 24, 2012 20:44
Show Gist options
  • Save sekati/3172554 to your computer and use it in GitHub Desktop.
Save sekati/3172554 to your computer and use it in GitHub Desktop.
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}"
@rswilley
Copy link

rswilley commented Aug 9, 2015

Works like a charm

@troya2
Copy link

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?

@CodeEagle
Copy link

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

@kb100824
Copy link

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

@maykonmeier
Copy link

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

@kristofer
Copy link

good on Xcode 7.1, iOS 9

@fishercraigj
Copy link

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
Copy link

helsont commented Jan 21, 2016

+1 Great!

@DavidGagne
Copy link

+100 Excellent

@drulang
Copy link

drulang commented May 27, 2016

@nebiros +1 for xcode 7.3

@welemski
Copy link

welemski commented Jun 9, 2016

cool! Just what I needed
Thanks!

@aminbenarieb
Copy link

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?

@oleynikd
Copy link

Thanks! Works on Xcode 8.1

@eleev
Copy link

eleev commented Dec 12, 2016

Brilliant, thank you!

@nguyentanhungbk
Copy link

nguyentanhungbk commented Dec 20, 2016

work well. Thank you

@isbaek
Copy link

isbaek commented Mar 19, 2017

Thank you so much ! it worked : )

Copy link

ghost commented Apr 30, 2017

Awesome! Worked brilliantly.

@misanche
Copy link

Good work!

@Jusung
Copy link

Jusung commented May 26, 2017

Work well like charm in Xcode 8.3.2

@cusster
Copy link

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

@chill5018
Copy link

Thanks @cusster that worked for me also

@freegor
Copy link

freegor commented Oct 15, 2017

Thanks @cusster! Have a nice day.

@Eric0625
Copy link

Still works in Xcode 9.1

@rodydavis
Copy link

rodydavis commented Feb 27, 2018

@LordParsley
Copy link

For Xcode 10 support:

https://apple-dev.groups.io/g/xcode/topic/xcode_10_info_plist/22699344?p=,,,20,0,0,0::recentpostdate%2Fsticky,,,20,2,0,22699344

Adding the generated info.plist -- ${TARGET_BUILD_DIR}/${INFOPLIST_PATH} -- as in input file to the script got the dependencies right so Xcode does things in the desired order.

@jndefosse
Copy link

jndefosse commented Sep 25, 2018

For us, the script don't update the build number but by changing the script the build number change (code for xcode 10):

buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$PRODUCT_SETTINGS_PATH")
buildNumber=$(cut -d'.' -f2 <<<$buildNumber)
buildNumber=$(date +"%y%m%d")"."$(($buildNumber + 1))
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$PRODUCT_SETTINGS_PATH"
#180925.1008 First is the current date and after the dot the build number

@iminsight
Copy link

For us, the script don't update the build number but by changing the script the build number change (code for xcode 10):

buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$PRODUCT_SETTINGS_PATH")
buildNumber=$(cut -d'.' -f2 <<<$buildNumber)
buildNumber=$(date +"%y%m%d")"."$(($buildNumber + 1))
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$PRODUCT_SETTINGS_PATH"
#180925.1008 First is the current date and after the dot the build number

Thanks @jndefosse, I use the commit count as the build number.

buildNumber=$(git rev-list --count HEAD)
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$PRODUCT_SETTINGS_PATH"

@foscomputerservices
Copy link

I needed to compare buildNumber with the current value in the plist before updating the plist. If the plist is updated on every build, then the SwiftUI previewer will continually stop:

currentNumber=$(/usr/libexec/PlistBuddy -c "Print :CFBundleVersion" "$PRODUCT_SETTINGS_PATH")
if [ "$buildNumber" -ne "$currentNumber" ] ; then
  /usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$PRODUCT_SETTINGS_PATH"
fi

@ppeelen
Copy link

ppeelen commented Nov 16, 2021

From Xcode 13, a new project will not generate an info.plist. Instead this is embedded in the project.

If you want a project to generate an Info.plist, set Generate Info.plist file to no and then open the Info tab and add a option (e.g. App Transport Security Settings). Xcode will then generate the Info.plist file and add all the values as well as update the build settings.

@m-irfan
Copy link

m-irfan commented Feb 4, 2022

If you want to keep $(MARKETING_VERSION)
in Info.plist for CFBundleShortVersionString but
want to increase version after successful archive or build
then use following script
following script will increase like 1.0 to 2.0
but of-course that logic can be modified using above explained gist etc

Script to increase app version

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