Skip to content

Instantly share code, notes, and snippets.

@imkevinxu
Last active February 5, 2018 14:45
Show Gist options
  • Save imkevinxu/7a4fe9f258edec42eee9 to your computer and use it in GitHub Desktop.
Save imkevinxu/7a4fe9f258edec42eee9 to your computer and use it in GitHub Desktop.
Xcode build phase script to increment the build number on every build or every commit
if [ -z "${PROJECT_DIR}" ]; then
PROJECT_DIR=`pwd`
fi
if [ -z "${PREFIX}" ]; then
PREFIX=""
fi
SVN_DIR="${PROJECT_DIR}/.svn"
GIT_DIR="${PROJECT_DIR}/.git"
if [ -d "${GIT_DIR}" ]; then
if [ -z "${GIT_BRANCH}" ]; then
GIT_BRANCH=`git rev-parse --abbrev-ref HEAD`
fi
BUILD_NUMBER=`xcrun git rev-list ${GIT_BRANCH} | wc -l | tr -d ' '`
BUILD_HASH=`xcrun git rev-parse --short --verify ${GIT_BRANCH} | tr -d ' '`
elif [ -d "${SVN_DIR}" ]; then
BUILD_NUMBER=`xcrun svnversion -nc "${PROJECT_DIR}" | sed -e 's/^[^:]*://;s/[A-Za-z]//' | tr -d ' '`
BUILD_HASH="${BUILD_NUMBER}"
else
BUILD_NUMBER="1"
BUILD_HASH="1"
fi
OLD_BUILD_NUMBER=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${PROJECT_DIR}/${INFOPLIST_FILE}")
if [ "${BUILD_NUMBER}" != "${OLD_BUILD_NUMBER}" ]; then
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $BUILD_NUMBER" "${PROJECT_DIR}/${INFOPLIST_FILE}"
fi
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}"
@imkevinxu
Copy link
Author

Screenshot of Xcode configuration

Imgur

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