Xcode Semantic Auto-Incrementing Versioning System
473 Words ~3 Minute Reading Time • Subscribe to receive updates on Code Snippets
Easily automate your version and build numbers incrementally in Xcode and use git hooks to ensure version consistency. This is a great way to ensure that your versioning is consistent across all of your projects without all the manual hassles.
Xcode Auto Increment
-
Build Number: You can auto-increment the build number using a Run Script Phase in Xcode.
- Go to your target settings, then
Build Phases
. - Click
+
to add aNew Run Script Phase
. - Add the following script:
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE") buildNumber=$(($buildNumber + 1)) /usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$INFOPLIST_FILE"
- Go to your target settings, then
-
Version Number: Xcode doesn't have native support for semantic versioning auto-increment, but you can modify the script to fit semantic rules manually.
Git Hooks for Version Consistency
- Navigate to your git project directory, then go to
.git/hooks
. - Create a file named
pre-commit
(no file extension). - Make it executable:
chmod +x pre-commit
. - Add a script that reads the
CFBundleShortVersionString
andCFBundleVersion
fromInfo.plist
and checks or modifies them as needed.
For example, a simple script could read the Info.plist
and prevent a commit if the version number hasn't been incremented.
Below is an example pre-commit
shell script that reads the CFBundleShortVersionString
and CFBundleVersion
from Info.plist
. It will prevent the commit if the version is not greater than the previously stored version.
Create a file named .last_version
to keep track of the last version.
#!/bin/sh
# pre-commit
# Define the path to your Info.plist file
infoPlistPath="./YourProject/Info.plist"
# Read the current version and build number from Info.plist
currentVersion=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "$infoPlistPath")
currentBuild=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$infoPlistPath")
# Read the last committed version and build number
lastVersion=$(cat .last_version)
lastBuild=$(cat .last_build)
# Logic to check if the version or build number has been incremented
if [[ "$currentVersion" > "$lastVersion" ]] || [[ "$currentBuild" > "$lastBuild" ]]; then
echo "Version or build number has been incremented."
# Store the new version and build number
echo $currentVersion > .last_version
echo $currentBuild > .last_build
# Add .last_version and .last_build to the commit
git add .last_version
git add .last_build
exit 0
else
echo "Error: You must increment the version or build number to commit."
exit 1
fi
Replace YourProject/Info.plist
with the relative path to your project's Info.plist
.
This is a simple example and could be extended to follow more complex semantic versioning rules.
By combining Xcode Run Scripts and Git hooks, you can have a robust automated versioning system.
Supporting My Work
Please consider Buying Me A Coffee. I work hard to bring you my best content and any support would be greatly appreciated. Thank you for your support!