Search code examples
iosxcodeunity-game-enginexcodebuild

How to build an Xcode project through command line in 2019 with the right provisioning profile?


I'd like to automatize ios builds for Unity. The first part is not a problem. It generated a whole folder for xcode with an xcodeproj file.

My goal is to write a single script which builds my project and uploads it to appstore's test flight. I've managed to get an .ipa file using these commands:

xcodebuild -project Unity-iPhone.xcodeproj -scheme "Unity-iPhone" -sdk iphoneos -configuration Release archive -archivePath export/build/ts2.xcarchive
xcodebuild -exportArchive -archivePath export/build/ts2.xcarchive -exportOptionsPlist info.plist -exportPath export/build -allowProvisioningUpdates

However when I tried to upload it with altool, it threw an error about using the wrong provisioning profile. True that it had the development profile and not the distribution one. How can I specify which one to use at signing?

Unfortunately xcode changes their ways to do this often enough to render all online resources about this useless


Solution

  • You can create a .plist which contains the provisioning profile used for distribution. Then point to it in the -exportOptionsPlist parameter.

    If you want to do this dynamically in a script, you can generate the .plist like this

        cat > "$BUILD_DIRECTORY/exportOptions.plist" <<EOF
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
        <key>method</key>
        <string>$EXPORT_METHOD</string>
        <key>provisioningProfiles</key>
        <dict>
            <key>$BUNDLE_IDENTIFIER</key>
            <string>$PATH_TO_PROVISION_PROFILE</string>
        </dict>
    </dict>
    </plist>
    EOF
    

    Then on the -exportArchive step point to -exportOptionsPlist "$BUILD_DIRECTORY/exportOptions.plist"

    For the App Store the $EXPORT_METHOD would be app-store, but you can expand this to enterprise, ad-hoc if need be and replace with the respective provisioning paths.

    (P.S. I don't use -allowProvisioningUpdates so you may try removing it.)