Skip to content

Instantly share code, notes, and snippets.

@armadsen
Last active Oct 12, 2021
Embed
What would you like to do?
Simple script for creating a zip and DMG, uploading to S3, and posting a new build to an Appcaster instance.
#!/bin/sh
# UploadBuild.sh
# Aether
#
# Created by Andrew Madsen on 11/7/15.
# Copyright © 2015 Open Reel Software. All rights reserved.
S3BucketName=<redacted>
username="<redacted>"
key="<redacted>"
baseURL="http://$username:$key@buildserver.yourwebsite.com"
buildsURLSlug="yourappname"
# confirm we are building a release style build
if [ $CONFIGURATION != 'Release' ]; then
echo "Upload Build target should only be built with Release configuration. This build will not be uploaded."
exit
fi
cd "${PROJECT_DIR}"
if [ -n "$UPDATE_VERSION_NUMBER" ]; then
echo "Updating build number."
newBuildNumber=$(git rev-list --count HEAD)
agvtool new-version -all "$newBuildNumber"
fi
# Get version numbers
shortVersionString=$(xcodebuild -showBuildSettings | grep MARKETING_VERSION | tr -d 'MARKETING_VERSION =')
buildNumber=$(xcodebuild -showBuildSettings | grep CURRENT_PROJECT_VERSION | tr -d 'CURRENT_PROJECT_VERSION =')
echo "Version is $shortVersionString($buildNumber)"
# Create zip file for notarization
cd "${TARGET_BUILD_DIR}"
notariaztionZipFileName="${PRODUCT_NAME}_$shortVersionString($buildNumber).zip"
echo "Creating zip file $notariaztionZipFileName"
zip -q -y -r "$notariaztionZipFileName" "${PRODUCT_NAME}.app"
fileSize=$(stat -f %z $zipFileName)
# Allow suppressing the confirmation alert e.g. when building on the command-line
# Add SUPPRESS_UPLOAD_CONFIRMATION=1 to xcodebuild's arguments to supress
if [ -z "$SUPPRESS_UPLOAD_CONFIRMATION" ]; then
confirmationScript=$(cat <<EOF
on run
tell application "SystemUIServer"
activate
display dialog "Are you sure you want to upload to the Build Hub?" buttons {"Don't Upload", "Upload"} default button 2 with icon caution giving up after 15
set dialogResult to the result
set theButton to button returned of dialogResult
if (gave up of dialogResult) then
set theButton to "Don't Upload"
end if
end tell
tell application "Xcode" to activate
return theButton
end run
EOF
)
# ask user to confirm upload
confirmation=$(echo "$confirmationScript" | osascript)
if [ "$confirmation" != "Upload" ]
then
echo "Not uploading to build hub"
exit 0
fi
fi #SUPPRESS_UPLOAD_CONFIRMATION
# Notarize
cd "${TARGET_BUILD_DIR}"
echo "Submitting for notarization..."
xcrun altool --notarize-app -f "$notariaztionZipFileName" --primary-bundle-id "com.yourcompany.yourapp" -u "yourappleid@whatever.com" -p @keychain:YOUR_SIGNING_KEY --asc-provider "YourCompany" &> tmp
success=$?
cat tmp
if [ $success -ne 0 ]
then
echo "Failed to submit for notarization"
rm tmp
exit $success
fi
uuid=`cat tmp | grep -Eo '\w{8}-(\w{4}-){3}\w{12}$'`
rm tmp
echo "Notarization submitted, received UUID: $uuid"
while true; do
echo "checking for notarization..."
xcrun altool --notarization-info "$uuid" -u "yourappleid@whatever.com" -p @keychain:YOUR_SIGNING_KEY --asc-provider "YourCompany" &> tmp
r=`cat tmp`
t=`echo "$r" | grep "success"`
f=`echo "$r" | grep "invalid"`
rm tmp
if [[ "$t" != "" ]]; then
echo "notarization done!"
xcrun stapler staple "${PRODUCT_NAME}.app"
echo "stapler done!"
break
fi
if [[ "$f" != "" ]]; then
echo "$r"
exit 1
fi
echo "Notarization not finished yet, sleeping for 30 seconds then will check again..."
sleep 30
done
echo "Notarization complete"
# Create a DMG
cd "${PROJECT_DIR}"/"Disk Images"
masterDMGZip="${PRODUCT_NAME} Master.dmg.zip"
masterDMG="${PRODUCT_NAME} Master.dmg"
outputDMGFileName="${PRODUCT_NAME}_$shortVersionString($buildNumber).dmg"
echo "Creating DMG $outputDMGFileName"
unzip "$masterDMGZip"
success=$?
if [ $success -ne 0 ]
then
echo "Failed to unzip master disk image."
exit $success
fi
hdiutil attach "$masterDMG" -mountpoint "${PRODUCT_NAME}"
success=$?
if [ $success -ne 0 ]
then
echo "Failed to mount master disk image"
rm "$masterDMG"
exit $success
fi
rm -r "${PRODUCT_NAME}"/"${PRODUCT_NAME}".app
cp -R "${TARGET_BUILD_DIR}/${PRODUCT_NAME}.app" "${PRODUCT_NAME}"/
hdiutil detach "${PRODUCT_NAME}"
hdiutil convert "$masterDMG" -format UDZO -imagekey zlib-level=9 -ov -o "${TARGET_BUILD_DIR}"/"$outputDMGFileName"
success=$?
if [ $success -ne 0 ]
then
echo "Failed to convert and compress DMG"
rm "$masterDMG"
exit $success
fi
rm "$masterDMG"
echo "Finished DMG $outputDMGFileName"
# Create zip file for distribution
cd "${TARGET_BUILD_DIR}"
zipFileName="${PRODUCT_NAME}_$shortVersionString($buildNumber).zip"
dsymZipFileName="${PRODUCT_NAME}_$shortVersionString($buildNumber) dSYM.zip"
echo "Creating zip file $zipFileName"
zip -q -y -r "$zipFileName" "${PRODUCT_NAME}.app"
echo "Creating zip file $dsymZipFileName"
zip -q -y -r "$dsymZipFileName" *.dSYM
fileSize=$(stat -f %z $zipFileName)
# Generate signature
dsaSignature=$(openssl dgst -sha1 -binary < "$zipFileName" | openssl dgst -dss1 -sign "${PROJECT_DIR}/Scripts/dsa_priv.pem" | openssl enc -base64)
# Upload to S3
echo "Uploading $outputDMGFileName to S3."
aws s3 cp "$outputDMGFileName" "s3://$S3BucketName/" --acl public-read
success=$?
if [ $success -ne 0 ]
then
echo "Failed to upload to Amazon S3"
exit $success
fi
echo "Uploading $zipFileName to S3."
aws s3 cp "$zipFileName" "s3://$S3BucketName/" --acl public-read
success=$?
if [ $success -ne 0 ]
then
echo "Failed to upload to Amazon S3"
exit $success
fi
aws s3 cp "$dsymZipFileName" "s3://$S3BucketName/" --acl private
success=$?
if [ $success -ne 0 ]
then
echo "Failed to upload to Amazon S3"
exit $success
fi
s3URL="https://s3.amazonaws.com/$S3BucketName/$zipFileName"
# Add build to build hub
buildJson=$(cat <<SETVAR
{
"title": "${PRODUCT_NAME} $shortVersionString ($buildNumber)",
"filename": "$zipFileName",
"identifier": "$buildNumber",
"version": "$buildNumber",
"version_string": "$shortVersionString ($buildNumber)",
"minimum_system_version": "${MACOSX_DEPLOYMENT_TARGET}",
"length": "$fileSize",
"download_url": "$s3URL",
"signature": "$dsaSignature",
"channels": ["developer"]
}
SETVAR)
uploadURL="$baseURL/apps/$buildsURLSlug/builds"
echo "Uploading $zipFileName to $uploadURL. ($buildJson)"
curl -H "Content-Type: application/json" -X POST -d "$buildJson" $uploadURL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment