Skip to content

Instantly share code, notes, and snippets.

@pietbrauer
Last active September 9, 2016 20:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pietbrauer/959d74a7289d281b04d8 to your computer and use it in GitHub Desktop.
Save pietbrauer/959d74a7289d281b04d8 to your computer and use it in GitHub Desktop.
Universal CLI for automated iOS deploys and tests

Steps

  1. Download the cli.sh and move it into a scripts/ directory and execute chmod 755 scripts/cli.sh, this will make the script executable.
  2. Export your iOS Distribution private key from your Keychain into the scripts/ folder and name it ios_distribution.p12 make sure to note down the password as we will need it later. It is highly recommended to give it a very strong password.
  3. Download your Distribution provisioning profile from the Member Center
  4. Rename the provisioning profile to Release.mobilprovision
  5. At the top of the script you may need to adjust a few values:
SCHEME="" # Your scheme
KEYCHAIN="$SCHEME.keychain"
PROVISIONING_PROFILE="$SCRIPT_DIR/Release.mobileprovision"
INFO_PLIST="$SCHEME/Info.plist"
APP_ID="" # something like 123456 find it in itunesconnect.apple.com
export ITUNES_CONNECT_ACCOUNT="" # the user used for deployment

NOTE: You may need to adjust the test script, if you are using a Xcode workspace.

  1. On your CI server set the following Environment variables:
ITUNES_CONNECT_PASSWORD # The password of the Apple ID for your upload user
KEYCHAIN_PASSWORD # a random password which will be used during deployment
KEY_PASSWORD # The password for your private key

Now if you push to production it will automatically test, build and upload your iOS App.

general:
artifacts:
- "build/$SCHEME.ipa"
- "build/$SCHEME.dSYM.zip"
machine:
xcode:
version: "6.3.1"
dependencies:
pre:
- make ci_pre
test:
override:
- make ci_test
deployment:
production:
branch: production
commands:
- make deploy
#!/bin/bash
set -ex
export SCRIPT_DIR=$(dirname "$0")
SCHEME="Git2Go"
KEYCHAIN="$SCHEME.keychain"
PROVISIONING_PROFILE="$SCRIPT_DIR/Release.mobileprovision"
INFO_PLIST="$SCHEME/Info.plist"
APP_ID="" # something like 123456 find it in itunesconnect.apple.com
export ITUNES_CONNECT_ACCOUNT="" # the user used for deployment
is_ci_build(){
local CI=0
if [ -z "$KEYCHAIN_PASSWORD" ]; then
CI=1
fi
return $CI
}
prepare_keychain() {
delete_keychain
security create-keychain -p "$KEYCHAIN_PASSWORD" "$KEYCHAIN"
security default-keychain -s "$KEYCHAIN"
security list-keychains -s "$KEYCHAIN"
security unlock-keychain -p "$KEYCHAIN_PASSWORD" "$KEYCHAIN"
security set-keychain-settings -t 3600 -l "$KEYCHAIN"
local certpath="$SCRIPT_DIR/apple_wwdr.cer"
curl 'https://developer.apple.com/certificationauthority/AppleWWDRCA.cer' > "$certpath"
security import "$certpath" -k "$KEYCHAIN" -T /usr/bin/codesign
security import "$SCRIPT_DIR/ios_distribution.p12" -k "$KEYCHAIN" -P "$KEY_PASSWORD" -T /usr/bin/codesign
}
delete_keychain() {
if [ -f "$HOME/Library/Keychains/$KEYCHAIN" ]; then
security delete-keychain "$KEYCHAIN"
fi
}
copy_provisioning_profile() {
local PROV_PROFILE_DIR="$HOME/Library/MobileDevice/Provisioning Profiles/"
if ! [ -d "$PROV_PROFILE_DIR" ]; then
mkdir -p "$PROV_PROFILE_DIR"
fi
cp "$PROVISIONING_PROFILE" "$PROV_PROFILE_DIR"
}
set_build_number() {
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $CIRCLE_BUILD_NUM" "$INFO_PLIST"
}
build() {
set -o pipefail && xcodebuild -scheme $SCHEME -configuration Release clean build -sdk iphoneos CONFIGURATION_BUILD_DIR=$PWD/build | xcpretty -c
}
archive() {
xcrun -sdk iphoneos PackageApplication -v $PWD/build/$SCHEME.app -o $PWD/build/$SCHEME.ipa
zip -r build/$SCHEME.dSYM.zip build/$SCHEME.app.dSYM
}
upload_to_testflight() {
bundle exec ipa distribute:itunesconnect -i $APP_ID --upload -f $PWD/build/$SCHEME.ipa
security delete-keychain "$KEYCHAIN"
}
export -f is_ci_build
export -f prepare_keychain
export -f copy_provisioning_profile
export -f set_build_number
export -f build
export -f archive
export -f upload_to_testflight
export -f delete_keychain
if [ "$#" -eq 0 -o "$#" -gt 2 ]; then
exit 1
fi
COMMAND="$1"
case "$COMMAND" in
######################################
# Deploy
######################################
"deploy")
if ! is_ci_build; then
echo "Not a CI build"
exit 0
fi
prepare_keychain
copy_provisioning_profile
set_build_number
build
archive
upload_to_testflight
exit $?
;;
######################################
# Prepare keychain
######################################
"prepare-keychain")
if ! is_ci_build; then
echo "Not a CI build"
exit 0
fi
prepare_keychain
exit 0
;;
######################################
# Delete keychain
######################################
"delete-keychain")
if ! is_ci_build; then
echo "Not a CI build"
exit 0
fi
delete_keychain
exit 0
;;
######################################
# Bootstrap
######################################
"bootstrap")
bundle install
brew update
brew install carthage
exit $?
;;
######################################
# Test
######################################
"test")
set -o pipefail && xcodebuild -scheme $SCHEME build test -sdk iphonesimulator8.3 ONLY_ACTIVE_ARCH=NO -destination name="iPhone 6" | xcpretty -c -r junit --output $CIRCLE_TEST_REPORTS/xcode/results.xml
exit $?
;;
*)
echo "Unknown command '$COMMAND'"
exit 1
;;
esac
source "https://rubygems.org"
gem "xcpretty"
gem "shenzhen"
all: install test
install:
bundle check || bundle install
# carthage bootstrap
# bundle exec pod install
test:
./scripts/cli.sh test
bootstrap:
./scripts/cli.sh bootstrap
prepare_keychain:
./scripts/cli.sh prepare-keychain
delete_keychain:
./scripts/cli.sh delete-keychain
deploy:
./scripts/cli.sh deploy
ci_pre: bootstrap prepare_keychain install
ci_test: test
ci_post: delete_keychain
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment