如何使用Jenkins为iOS项目多Target构建IPA安装包?
Hey there! I’ve dealt with this exact multi-target iOS build scenario in Jenkins multiple times, so let me break down the step-by-step configs you need to get both target1 and target2 generating their own .ipa files smoothly.
First, making your job parameterized lets you choose which target to build (or both) on demand. Here’s how:
- Go to your Jenkins job → Configure → General tab.
- Check This project is parameterized.
- Add a Choice Parameter named
TARGET_SELECTIONwith options:target1target2Both
This gives you flexibility instead of hardcoding a single target.
Skip the Jenkins Xcode plugin for this (it’s less flexible for multi-target setups) and use an Execute shell build step. Write a script that handles each target based on your parameter:
# Create build directories to keep things organized mkdir -p ./build/ipa/target1 mkdir -p ./build/ipa/target2 # Build and export target1 if selected if [ "${TARGET_SELECTION}" = "target1" ] || [ "${TARGET_SELECTION}" = "Both" ]; then # Clean and archive target1 xcodebuild clean archive \ -workspace YourProject.xcworkspace \ -scheme target1 \ -configuration Release \ -archivePath ./build/target1.xcarchive # Export IPA for target1 (use your specific ExportOptions.plist) xcodebuild -exportArchive \ -archivePath ./build/target1.xcarchive \ -exportPath ./build/ipa/target1 \ -exportOptionsPlist ./ExportOptions-target1.plist fi # Build and export target2 if selected if [ "${TARGET_SELECTION}" = "target2" ] || [ "${TARGET_SELECTION}" = "Both" ]; then # Clean and archive target2 xcodebuild clean archive \ -workspace YourProject.xcworkspace \ -scheme target2 \ -configuration Release \ -archivePath ./build/target2.xcarchive # Export IPA for target2 (use your specific ExportOptions.plist) xcodebuild -exportArchive \ -archivePath ./build/target2.xcarchive \ -exportPath ./build/ipa/target2 \ -exportOptionsPlist ./ExportOptions-target2.plist fi
Each target might have different code signing or export settings (like ad-hoc vs app store). Generate these plists locally first:
- Open Xcode, archive one target manually, then export the IPA.
- Xcode will generate an
ExportOptions.plistduring export. Copy this, rename it toExportOptions-target1.plist, and adjust settings if needed (repeat for target2). - Commit these plists to your repo so Jenkins can access them.
If your targets use different certificates/provisioning profiles:
- Ensure both are installed in the Jenkins agent’s keychain. You can add a build step to import them using
security importif needed. - Alternatively, add code signing flags to your
xcodebuildcommands:xcodebuild clean archive \ -workspace YourProject.xcworkspace \ -scheme target1 \ -configuration Release \ -archivePath ./build/target1.xcarchive \ CODE_SIGN_IDENTITY="iPhone Distribution: Your Team ID" \ PROVISIONING_PROFILE="Your Target1 Provisioning Profile UUID"
Finally, add a Post-build Actions step to save your IPAs:
- Select Archive the artifacts.
- In Files to archive, enter:
build/ipa/**/*.ipa
This will store both IPAs (or the selected one) in Jenkins for easy download.
内容的提问来源于stack exchange,提问作者Jenish

