Xcode "Build and Archive" from command line


Xcode 3.2 provides an awesome new feature under the Build menu, "Build and Archive" which generates an .ipa file suitable for Ad Hoc distribution. You can also open the Organizer, go to "Archived Applications," and "Submit Application to iTunesConnect."

Is there a way to use "Build and Archive" from the command line (as part of a build script)? I'd assume that xcodebuild would be involved somehow, but the man page doesn't seem to say anything about this.

UPDATE Michael Grinich requested clarification; here's what exactly you can't do with command-line builds, features you can ONLY do with Xcode's Organizer after you "Build and Archive."

  1. You can click "Share Application..." to share your IPA with beta testers. As Guillaume points out below, due to some Xcode magic, this IPA file does not require a separately distributed .mobileprovision file that beta testers need to install; that's magical. No command-line script can do it. For example, Arrix's script (submitted May 1) does not meet that requirement.
  2. More importantly, after you've beta tested a build, you can click "Submit Application to iTunes Connect" to submit that EXACT same build to Apple, the very binary you tested, without rebuilding it. That's impossible from the command line, because signing the app is part of the build process; you can sign bits for Ad Hoc beta testing OR you can sign them for submission to the App Store, but not both. No IPA built on the command-line can be beta tested on phones and then submitted directly to Apple.

I'd love for someone to come along and prove me wrong: both of these features work great in the Xcode GUI and cannot be replicated from the command line.

7/21/2010 5:46:19 AM

Accepted Answer

I found how to automate the build and archive process from the comand line, I just wrote a blog article explaining how you can achieve that.

The command you have to use is xcrun:

/usr/bin/xcrun -sdk iphoneos PackageApplication \
--sign "${DEVELOPER_NAME}" \

You will find all the details in the article. If you have any questions dont hesitate to ask.

8/27/2018 9:29:43 PM

Updating my answer with Xcode 9 and Swift


xcodebuild -workspace <ProjectName>/<ProjectName>.xcworkspace \
  -scheme <schemeName> clean archive -configuration release \
  -sdk iphoneos -archivePath <ProjectName>.xcarchive

IPA Export (please note the export option plist)

xcodebuild -exportArchive -archivePath  <ProjectName>.xcarchive \
  -exportOptionsPlist  <ProjectName>/exportOptions.plist \
  -exportPath  <ProjectName>.ipa

For those who don't know about exportOptions.plist,

Those who were using this for building project in CI/CD tools like teamcity/jenkins, please make sure you are using the right xcode installed in the build agent for for both archive and export.

You can use either of below 2 options for this.

  1. Use the full path to xcodebuild,
  1. Use xcode-select,
xcode-select -switch /Applications/Xcode

Below is my old answer

Here is command line script for creating archive and IPA example. I have an iPhone xcode project , which is located in Desktop/MyiOSApp folder.

Execute following commands one by one:

cd /Users/username/Desktop/MyiOSApp/

xcodebuild -scheme MyiOSApp archive \
  -archivePath /Users/username/Desktop/MyiOSApp.xcarchive

xcodebuild -exportArchive -exportFormat ipa \
  -archivePath "/Users/username/Desktop/MyiOSApp.xcarchive" \
  -exportPath "/Users/username/Desktop/MyiOSApp.ipa" \
  -exportProvisioningProfile "MyCompany Distribution Profile"

This is tested with Xcode 5 and working fine for me.


I've been using my own build script to generate the ipa package for ad hoc distribution.

die() {
    echo "$*" >&2
    exit 1

config='Ad Hoc Distribution'

echo using configuration $config

echo updating version number
agvtool bump -all
fullversion="$(agvtool mvers -terse1)($(agvtool vers -terse))"
echo building version $fullversion

xcodebuild -activetarget -configuration "$config" -sdk $sdk build || die "build failed"

echo making ipa...
# packaging
cd build/"$config"-iphoneos || die "no such directory"
rm -rf Payload
rm -f "$appname".*.ipa
mkdir Payload
cp -Rp "$" Payload/
if [ -f "$project_dir"/iTunesArtwork ] ; then
    cp -f "$project_dir"/iTunesArtwork Payload/iTunesArtwork

ipaname="$appname.$fullversion.$(date -u +%Y%m%d%H%M%S).ipa"
zip -r $ipaname Payload

echo finished making $ipaname

The script also increment the version number. You can remove that part if it's not needed. Hope it helps.


The xcodebuild tool can build and export archive products with the -exportArchive flag (as of Xcode 5). The export step was previously only possible via the Xcode Organizer UI.

First archive your app:

xcodebuild -scheme <scheme name> archive

Given $ARCHIVE_PATH (the path to the .xcarchive file), export the app from the archive with one of the following:

iOS .ipa file:

xcodebuild -exportArchive -exportFormat ipa -archivePath "$ARCHIVE_PATH" -exportPath "myApp.ipa" -exportProvisioningProfile "My App Provisioning profile"

Mac .app file:

xcodebuild -exportArchive -exportFormat app -archivePath "$ARCHIVE_PATH" -exportPath "" -exportSigningIdentity "Developer ID Application: My Software Company"

In both commands the -exportProvisioningProfile and -exportSigningIdentity arguments are optional. man xcodebuild for details on the semantics. In these examples, the provisioning profile for the iOS build specified an AdHoc distribution provisioning profile, and the signing identity for the Mac app specified a Developer ID for export as a 3rd party application (i.e. not distributed via the Mac App Store).


We developed an iPad app with XCode 4.2.1 and wanted to integrate the build into our continuous integration (Jenkins) for OTA distribution. Here's the solution I came up with:

# Unlock keychain
security unlock-keychain -p jenkins /Users/jenkins/Library/Keychains/login.keychain

# Build and sign app
xcodebuild -configuration Distribution clean build

# Set variables
VERSION=`defaults read $APP_PATH/Info CFBundleShortVersionString`
REVISION=`defaults read $APP_PATH/Info CFBundleVersion`
DATE=`date +"%Y%m%d-%H%M%S"`
ITUNES_LINK="<a href=\"itms-services:\/\/?action=download-manifest\&url=https:\/\/\/iPadApp-$VERSION.$REVISION-$DATE.plist\">Download iPad2-App v$VERSION.$REVISION-$DATE<\/a>"

# Package and verify app
xcrun -sdk iphoneos PackageApplication -v build/Distribution-iphoneos/ -o $PWD/iPadApp-$VERSION.$REVISION-$DATE.ipa

# Create plist
cat iPadApp.plist.template | sed -e "s/\${VERSION}/$VERSION/" -e "s/\${DATE}/$DATE/" -e "s/\${REVISION}/$REVISION/" > iPadApp-$VERSION.$REVISION-$DATE.plist

# Update index.html
curl -o index.html.$DATE
cat index.html.$DATE | sed -n '1h;1!H;${;g;s/\(<h3>Aktuelle Version<\/h3>\)\(.*\)\(<h3>&Auml;ltere Versionen<\/h3>.<ul>.<li>\)/\1\
<li>/g;p;}' | sed -e "s/\${ITUNES_LINK}/$ITUNES_LINK/" > index.html

Then Jenkins uploads the ipa, plist and html files to our webserver.

This is the plist template:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">

To set this up, you have to import the distribution certificate and provisioning profile into the designated user's keychain.


I found some of the other answers here hard to get going. This article did if for me. Some paths may need to be absolute, as mentioned in the other answers.

The Command:

xcrun -sdk iphoneos PackageApplication \
    "/path/to/build/" \
    -o "output/path/to/MyApp.ipa" \
    --sign "iPhone Distribution: My Company" \
    --embed "/path/to/something.mobileprovision"

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]