10

I am trying to implement the answer here:

Better way of incrementing build number?

but cannot get it to work properly. It fails with error 2 saying "No build number in plist"

But if I put a build number in my plist, the script clears it on the next build, then the same thing happens all over again.

Any ideas?

Community
  • 1
  • 1
ourmanflint
  • 105
  • 1
  • 6

3 Answers3

25

Here's how I increment build numbers:

In the Target > Summary tab, set the initial build # enter image description here

Then use this script to increment the build number:

#!/bin/bash
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE")
buildNumber=$(($buildNumber + 1))
buildNumber=$(printf "%04d" $buildNumber)
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$INFOPLIST_FILE"

or if you want build numbers in hex:

#!/bin/bash
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE")
buildNumber=$((0x$buildNumber))
buildNumber=$(($buildNumber + 1))
buildNumber=$(printf "%04X" $buildNumber)
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$INFOPLIST_FILE"
rgov
  • 3,516
  • 1
  • 31
  • 51
FluffulousChimp
  • 9,157
  • 3
  • 35
  • 42
  • Thanks..maybe that was the problem I initially set build to 1.1.0.0 as I thought that was how build versions were formatted, doing it without decimals does seem to work but now build is simply single digit. How can I make it a 4 digit build number format? – ourmanflint Sep 11 '12 at 13:46
  • @ourmanflint I think the snippets above should give you a four digit build number (`%04d` and `%04X`) or do you mean a four digit number like 1.1.0.0? Depends on whether you want to increment build numbers `CFBundleVersion`, version numbers `CFBundleShortVersionString`, or both. – FluffulousChimp Sep 11 '12 at 13:49
  • 4
    That buildNumber=$((0x$buildNumber)) line should be removed as it converts in hex a decimal number. – Fabiano Francesconi Jul 03 '13 at 10:22
  • this does not work for me the build numbers always range between 0001 - 006 – Parth Jan 19 '21 at 16:33
3

My solution is the following:

#!/bin/bash
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE")
buildNumber=$(echo $buildNumber | sed 's/0*//')
buildNumber=$(($buildNumber + 1))
buildNumber=$(printf "%04d" $buildNumber)
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$INFOPLIST_FILE"

Using sed to remove leading zeroes, incrementing the value and printing it back in the plist file using a four-digit-zero-padded number.

Fabiano Francesconi
  • 1,769
  • 1
  • 19
  • 35
0

And if you use Jenkins you can use the Jenkins build number

/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $BUILD_NUMBER" "$INFOPLIST_FILE";
Rivera
  • 10,792
  • 3
  • 58
  • 102
  • Tried this but it does not work. Am I missing something? The build number in plist is always 1, build number in jenkins is 60 – tiltem Jul 09 '13 at 22:58
  • Besides the path to your Plist that could be wrong, I can only imagine that maybe the original Plist needs any dummy `CFBundleVersion` entry so that PlistBuddy can find and replace it. – Rivera Jul 11 '13 at 08:18