Skip to content

Instantly share code, notes, and snippets.

@siema
Last active August 15, 2022 10:26
Show Gist options
  • Save siema/3cd7460e1a1f5e562a0a to your computer and use it in GitHub Desktop.
Save siema/3cd7460e1a1f5e562a0a to your computer and use it in GitHub Desktop.
Build script for Xcode workspace with automatic upload to AppBlade
#!/bin/bash
# based on https://gist.github.com/jonah-williams/949831
# keychain unlock for codesign:
# security unlock-keychain "$HOME/Library/Keychains/login.keychain"
# this script assumes that you have signing and provisioning configured in project's plist
# use -m "message" to set AppBlade build description as attribute
# use -n to force notify, by default it's off to avoid notifying when forgot to add message :P
# Need to generate AppBlade Continuous Integration Token for the app
# Config
workspace="Test.xcworkspace"
scheme="Test"
configuration="AppBlade"
sdk="iphoneos"
ab_notify=false
ab_upload=true
ab_upload_dsym=true
ab_ci_token=""
ab_notes=""
while getopts "m:n" opt; do
case $opt in
m) ab_notes="$OPTARG" ;;
n) ab_notify=true ;;
esac
done
function failed()
{
local error=${1:-Undefined error}
echo "Failed: $error" >&2
exit 1
}
start_time=`date +%s`
echo "**** Build $scheme-$configuration"
devired_data_path="$HOME/Library/Developer/Xcode/DerivedData"
#get the name of the workspace to be build, used as the prefix of the DerivedData directory for this build
workspace_name=$(echo "$workspace" | sed -n 's/\([^\.]\{1,\}\)\.xcworkspace/\1/p')
#replace spaces in workspace name with underscores, just like xcodebuild does for derived data path
workspace_name="${workspace_name// /_}"
#build the app
echo "Running xcodebuild..."
xcodebuild -verbose -workspace "$workspace" -scheme "$scheme" -sdk "$sdk" -configuration "$configuration" clean build >| xcodebuild_output
if [ $? -ne 0 ]
then
tail -n20 xcodebuild_output
rm xcodebuild_output
failed xcodebuild
fi
#locate this project's DerivedData directory
project_derived_data_directory=$(grep -oE "$workspace_name-([a-zA-Z0-9]+)[/]" xcodebuild_output | sed -n "s/\($workspace_name-[a-z]\{1,\}\)\//\1/p" | head -n1)
project_derived_data_path="$devired_data_path/$project_derived_data_directory"
#rm xcodebuild_output
#locate the .app file
#infer app name since it cannot currently be set using the product name, see comment above
app_name=$(ls -1 "$project_derived_data_path/Build/Products/$configuration-$sdk/" | grep ".*\.app$" | head -n1)
if [ $(ls -1 "$project_derived_data_path/Build/Products/$configuration-$sdk/" | grep ".*\.app$" | wc -l) -eq 0 ]
then
echo "Failed to find a single .app build product."
failed locate_built_product
fi
echo "Built $app_name in $project_derived_data_path"
app_path="$project_derived_data_path/Build/Products/$configuration-$sdk/$app_name"
echo "**** Package Application"
#sign build for distribution and package as a .ipa
xcrun -sdk $sdk PackageApplication "$app_path" -o "$app_path.ipa" || failed codesign
if [ "$ab_upload" = true ]; then
echo "**** Upload to AppBlade"
if [ "$ab_upload_dsym" = true ]; then
echo "Zipping dSYM"
zip -r "$app_path.dSYM.zip" "$app_path.dSYM"
fi
echo "Uploading..."
if [ "$ab_upload_dsym" = true ]; then
curl -# -H "Accept: application/json" -H "Authorization: Bearer $ab_ci_token" -F "version[bundle]=@$app_path.ipa" -F "version[commit_id]=$(git rev-parse HEAD)" -F "version[changelog]=$ab_notes" -F "version[send_notification]=$ab_notify" -F "version[dsym]=@$app_path.dSYM.zip" https://appblade.com/api/3/versions | tee /dev/null
else
curl -# -H "Accept: application/json" -H "Authorization: Bearer $ab_ci_token" -F "version[bundle]=@$app_path.ipa" -F "version[commit_id]=$(git rev-parse HEAD)" -F "version[changelog]=$ab_notes" -F "version[send_notification]=$ab_notify" https://appblade.com/api/3/versions | tee /dev/null
fi
echo
fi
echo "**** Done in $(expr `date +%s` - $start_time)s!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment