Skip to content

Instantly share code, notes, and snippets.

@praveenApk
Created November 30, 2022 08:01
Show Gist options
  • Save praveenApk/b798b6c21637a5b21a4812f47c60026a to your computer and use it in GitHub Desktop.
Save praveenApk/b798b6c21637a5b21a4812f47c60026a to your computer and use it in GitHub Desktop.
A simple shell script to build ipa file and upload it to SFTP server using scp command.
#!/bin/sh
# Purpose: A simple shell script to build ipa file and upload it to SFTP server using scp command.
# Usage: sh iOS_SFTP.sh
# Make note of start time in epoch format. This will be later used to calculate script ran time.
startTime=$(date +"%s")
# Using bash color codes to print colored output in terminal.
# https://stackoverflow.com/a/28938235
GREEN='\033[0;32m'
RED='\033[0;31m'
COLOR_OFF='\033[0m'
PURPLE='\033[1;35m'
# Local build path
BUILD_PATH='~/Desktop/Build'
# Utility methods
# method 1: print function
# This will print the message in green colour.
print_func() {
echo "\n${GREEN} $1 ${COLOR_OFF}"
}
# method 2: print error function
# This will print the message in red colour.
print_error_func() {
echo "\n${RED} $1 ${COLOR_OFF}"
exit_func 1
}
# method 3: exit function
# This function will do cleanup actions on both build success and failure.
# For success case, it will exit with status code 0 and for failure case, it will exit with status code 1.
# https://askubuntu.com/a/892605
exit_func() {
print_func "Cleaning repo changes"
cd ~/Desktop/SFTP_Shell/CaratLane_iOS/ios-retail/ios-retail/CaratLane_Retail/
git checkout .
git reset HEAD --hard
git clean -fd
print_func "Exiting..."
if [ $1 == 1 ]
then
exit 1
else
exit 0
fi
}
# method 4: upload function
# Uploading IPA to SFTP server
upload_func() {
sshpass -f ~/Desktop/SFTP_Shell/pass_file scp -v ~/Desktop/Build/CaratLane.ipa {username}@{ip_address}:{server_path}/retail-f2-sh/CaratLane.ipa
print_func "** UPLOAD COMPLETED **"
}
# method 5: run time calculation function
# Calculate script ran time. Method takes two arguments.
run_time_func() {
scriptStartTime=$1
scriptEndTime=$2
timeDiff=$(($scriptEndTime - $scriptStartTime))
minutes=$(($timeDiff / 60))
seconds=$(($timeDiff % 60))
echo "\n${PURPLE}Took ${minutes} minutes and ${seconds} seconds to build and upload ipa to server.${COLOR_OFF}"
}
# 1. Change directory to iOS project repo
print_func "1. Navigating to iOS project repository."
cd ~/Desktop/SFTP_Shell/CaratLane_iOS/ios-retail/ios-retail/CaratLane_Retail/
pwd
# 2. Pull latest code from qe branch
print_func "2. Pulling latest code from qe branch."
git checkout qe
git pull origin qe
# 3. Pod install
print_func "3. Installing pod dependencies."
pod install
# 4. Build using CaratLane scheme
print_func "4. Building..."
BUILD_SUCCESS=$(xcodebuild -workspace CaratLane.xcworkspace -scheme CaratLane -sdk iphoneos -destination 'generic/platform=iOS' build | grep -w "\*\* BUILD SUCCEEDED \*\*")
# Check if build succeeded or not.
if [ "$BUILD_SUCCESS" == "** BUILD SUCCEEDED **" ]
then
# Archiving
print_func "$BUILD_SUCCESS"
print_func "5. Archiving..."
ARCHIVE_SUCCESS=$(xcodebuild -workspace CaratLane.xcworkspace -scheme CaratLane -archivePath $BUILD_PATH/CaratLane.xcarchive -destination 'generic/platform=iOS' archive | grep -w "\*\* ARCHIVE SUCCEEDED \*\*")
# Check if archive succeeded or not.
if [ "$ARCHIVE_SUCCESS" != "** ARCHIVE SUCCEEDED **" ]
then
print_error_func "\nError in xcode archive"
fi
# Exporting
print_func "$ARCHIVE_SUCCESS"
print_func "6. IPA Exporting..."
EXPORT_SUCCESS=$(xcodebuild -exportArchive -archivePath $BUILD_PATH/CaratLane.xcarchive -exportPath $BUILD_PATH -exportOptionsPlist ~/Desktop/SFTP_Shell/ExportOptions.plist | grep -w "\*\* EXPORT SUCCEEDED \*\*")
# Check if archive succeeded or not.
if [ "$EXPORT_SUCCESS" == "** EXPORT SUCCEEDED **" ]
then
print_func "$EXPORT_SUCCESS"
print_func "7. Uploading IPA to SFTP server."
upload_func
# Make note of end time in epoch
endTime=$(date +"%s")
# Calculate script run time
run_time_func $startTime $endTime
exit_func 0
else
print_error_func "Error in IPA export"
fi
else
print_error_func "Error in xcode build"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment