CircleCI build fails because of CODE_SIGN_IDENTITY in xcodebuild command - ios

I have inherited a project that uses Fastlane and CircleCI to generate builds. It has been broken for a while but I managed to get most of it working again. However the actual build keeps failing on the build server, locally all of my lanes already finish successfully.
I've been comparing what happens locally versus on the CI server and the difference seems to be in the way Circle archives the project. I've changed it so it will run locally and the errors remain the same:
xcodebuild -workspace ./App.xcworkspace -scheme App -configuration AdHoc -destination generic/platform=iOS -archivePath "/Users/me/Library/Developer/Xcode/Archives/2020-08-14/App 2020-08-14 13.08.37.xcarchive" archive "CODE_SIGN_IDENTITY=iPhone Distribution: ACME"
Results in:
error: No certificate for team '123ABC4DEF' matching 'iPhone Distribution: ACME' found: Select a different signing certificate for CODE_SIGN_IDENTITY, a team that matches your selected certificate, or switch to automatic provisioning. (in target '***********-SomeLibrary' from project '***********')
It looks like it wants to sign not only the main application using the CODE_SIGN_IDENTITY, expects 'iPhone Distribution: ACME' to be explicitly there and fails when it doesn't.
As soon as I remove the CODE_SIGN_IDENTITY part at the end it starts working locally:
xcodebuild -workspace ./App.xcworkspace -scheme App -configuration AdHoc -destination generic/platform=iOS -archivePath "/Users/me/Library/Developer/Xcode/Archives/2020-08-14/App 2020-08-14 13.08.37.xcarchive" archive"
I've been combing over all of the settings where this might be specified hardcoded but I couldn't find anything. GYM_CODE_SIGN_IDENTITY hasn't been added for example. So how is it possible that it doesn't add that CODE_SIGN_IDENTITY flag locally using Fastlane but yes remote on the server?

I've found out that it was a variable set up in the configuration of CircleCI:
https://app.circleci.com/settings/project/github/[your_org]/[your_app]/environment-variables

Related

how to properly archive an ios app xcode 13

Well there are lots of articles/blog on archiving an iOS app, i've tried several of them, but i end up getting one error or the other, I'm setting up CI/CD, and I'm at the stage of archiving my app.
These are the most promising errors i've gotten so far, i say promising cause other errors encountered seem to be dead ends, but the below commands seems to be a step in the right direction.
1st Command
xcodebuild -workspace MyApp.xcworkspace -scheme MyApp -sdk iphoneos -configuration AppStoreDistribution -archivePath $PWD/build/MyApp.xcarchive clean archive
Output
error: Signing for "MyApp" requires a development team. Select a development team in the Signing & Capabilities editor. (in target 'Decred Wallet' from project 'MyApp')
2nd Command
xcodebuild -workspace MyApp.xcworkspace -scheme MyApp -sdk iphoneos -configuration MyApp\ Release -archivePath $PWD/build/MyApp.xcarchive clean archive
Output
error: No profiles for 'com.mydomian.myapp.test' were found: Xcode couldn't find any iOS App Development provisioning profiles matching 'com.mydomian.myapp.test'. Automatic signing is disabled and unable to generate a profile. To enable automatic signing, pass -allowProvisioningUpdates to xcodebuild. (in target 'MyApp' from project 'MyApp')
has anyone done this recently, I need some insight please.
also a few steps to this
i already created a direcory mkdir -p ~/Library/MobileDevice/Provisioning\ Profiles and copied my mobile provisioning file there, i also created a keychain and imported my .p12 file into it

xcode 8 xcodebuild manual code signing from command line for multiple provision profiles

In Xcode 7 I was building project through command line for multiple provision profiles by using following commands. I have multiple provision profiles and multiple code signing certificate linked to those profiles so I need to sign the IPA file with appropriate provision profile.
PROVISION_PROFILE="My Provision profile name"
xcodebuild -workspace ../ProjectName.xcworkspace -scheme "${PRODUCT_NAME}" -sdk iphoneos -configuration "${CONFIGURATION}" archive -archivePath "${TARGET_BUILD_DIR}/${PRODUCT_NAME}.xcarchive"
xcodebuild -exportArchive -archivePath "${TARGET_BUILD_DIR}/${PRODUCT_NAME}.xcarchive" -exportPath "${BINDIR}/${PACKAGE_NAME}-${CURVERSION}.ipa" -exportProvisioningProfile "${PROVISION_PROFILE}"
Now these commands not working for Xcode 8. I uncheck the automatically manage signing after that it gives below error
"someProjectName" requires a provisioning profile. Select a provisioning profile for the "Debug" build configuration in the project editor.
Code signing is required for product type 'Application' in SDK 'iOS 10.1'
Anybody please help me in this.
You are only specifying the provisioning profile in your -exportArchive command. In the -workspace command, Xcodebuild is reaching into the -scheme you specify, which looks at the project.pbxproj file and grabs the provisioning profiles you placed from the project editor for the configurations specified by the scheme being built.
If you open your project.pbxproj file in a text editor, you'll see that the Debug configuration has no provisioning profile listed, which is why Xcodebuild is throwing this error. To fix this, add your provisioning profiles in the project editor (or in project.pbxproj) for the configurations being built by your scheme.
Edit: Now that I better understand the requirements, editing my answer with another option. You can also do this to manually specify the provisioning profile in your build step:
xcodebuild -workspace ../ProjectName.xcworkspace -scheme "${PRODUCT_NAME}" -sdk iphoneos -configuration "${CONFIGURATION}" archive -archivePath "${TARGET_BUILD_DIR}/${PRODUCT_NAME}.xcarchive" PROVISIONING_PROFILE="${PROVISION_PROFILE}"

Using xcodebuild to build different platform

I have a framework project and I'm trying to build a fat file (iphoneos + iphonesimulator) using a script build phase, but it's not working. The script is simple; it checks the platform being currently built, like to:
if [[ "$SF_SDK_PLATFORM" = "iphoneos" ]]
then
SF_OTHER_PLATFORM=iphonesimulator
else
SF_OTHER_PLATFORM=iphoneos
fi
And then uses xcodebuild to build it:
xcrun xcodebuild -project "${PROJECT_FILE_PATH}" -target "${TARGET_NAME}" -sdk ${SF_OTHER_PLATFORM} -configuration "${CONFIGURATION}" BUILD_DIR="${BUILD_DIR}" OBJROOT="${OBJROOT}" BUILD_ROOT="${BUILD_ROOT}" SYMROOT="${SYMROOT}" $ACTION
Other details on the script have been omited for simplicity.
If I chose the initial target as the simulator, this works fine, and both the simulator and device binaries are generated and I use lipo to get the fat file. The problem happens when I do it the other way around, and build the device file, and as such xcodebuild is called for the iphonesimulator SDK. The build fails with the following error:
CodeSign error: entitlements are required for product type 'Framework' in SDK 'Simulator - iOS 8.4'. Your Xcode installation may be damaged.
If I change the -sdk option I get the build, but not the simulator build, which is what I need. This would be (kinda) fine, but in order to build for release (Archive) I need to set the device as the primary target, or otherwise xcode doesn't give me the option.
What should I do?
It looks like you need to put your script in an aggregate target type. It was designed for exactly such cases: two different targets in one build.
What I do is create a new target (Other->Aggregate type) and add a script to it and use that target to create a fat release product.
Here's the script I'm using:
xcodebuild -workspace MyApp.xcworkspace -scheme MyApp -sdk iphonesimulator -configuration Release
xcodebuild -workspace MyApp.xcworkspace -scheme MyApp -sdk iphoneos -configuration Release
mkdir -p ${TARGET_BUILD_DIR}/../MyApp${CURRENT_PROJECT_VERSION}
cp -r ${TARGET_BUILD_DIR}/../Release-iphoneos/ ${TARGET_BUILD_DIR}/../MyApp${CURRENT_PROJECT_VERSION}
lipo -create "${TARGET_BUILD_DIR}/../Release-iphoneos/MyApp.framework/MyApp" "${TARGET_BUILD_DIR}/../Release-iphonesimulator/MyApp.framework/MyApp" -output "${TARGET_BUILD_DIR}/../MyApp${CURRENT_PROJECT_VERSION}/MyApp.framework/MyApp"
Works like a charm. It creates a folder with the version number (you set it in your new target) and a fat framework inside.
EDIT:
Why this didn't work for you.
Developer is free to distribute iOS framework without codesigning it
as Consumer will re-codesign it anyway, but Developer is forced by
Xcode to codesign his framework when he builds for iOS device.
Creating iOS/OSX Frameworks: is it necessary to codesign them before distributing to other developers?
When you're using the device target you're forced to code sign even if you don't have to. That's why it works with the aggregate target -> it's not expected of you to codesign and you don't need to codesign to release a framework.
Selecting the simulator, the process works fine because you are building using the Debug configuration I think.
In the Project navigator, select your project. Now select your target and under the Build Settings tab, check Code Signing Identity settings.
If you expand that, you should see a row for every configuration you set in your project (if you didn't, you should see the default Debug and Release rows).
Now check under the Release row (that is the default used when the Archive command is called) that is set the correct identity (this is based on what you selected under Provisioning Profile).
If you want to know more about provisioning profiles, signing identity and so on, check this link from Apple

xctool build with today extension

we have an app integrated with today extension, we use xctool and Jenkins to do continuous build and in-house distribution.
In command line, before we use
xctool -workspace our_workspace.xcworkspace -scheme app_schme -xcconfig path_to_xcconfig -configuration Release build archive -archivePath path_to_archive
to generate archive and then export to .ipa, it works fine.
But right now with today extension, we have to build it with another scheme and xcconfig, we put certificate and provisioning profile in xcconfig, as today extension is a new target and should built with its own certificate and provisioning profile, I'm wondering how to achieve using xctool.
Any help is appreciated.
I finally managed to export ipa files via xcodebuild. Since the xctool is built upon xcodebuild, this answer might help.
First of all, when you create an extension, the extension's target will be embedded into your main app's scheme.
So, there is no need to use two schemes.
Then, in your project settings page, create a new configuration, say AdHoc. And then you can set a new Provisioning Profile in both of your target's build settings.
(project settings)
(the build settings of one target)
Then set the right provisioning profiles for your targets (And you'd better set the code sign identity to automatic, so that Xcode can determine which code sign identity is to be used).
Next step, you can archive your app using xcodebuild with the new configuration you just created above:
xcodebuild -project Extension\ Demo.xcodeproj -scheme Extension\ Demo -sdk iphoneos -archivePath ./Build/extension-demo.xcarchive -configuration AdHoc archive
In this step, the codesign will sign two of your targets separately by the provisioning profiles you specified.
Finally, export the .xcarchive file to ipa, again using xcodebuild;
xcodebuild -exportArchive -archivePath ./Build/extension-demo.xcarchive -exportPath ./Build/extension-demo.ipa -exportWithOriginalSigningIdentity
Notice that -exportWithOriginalSigningIdentity is set, so that xcodebuild will not re-sign your ipa, and the code signature in the xcarchive file is preserved.

Why won't my build phase scripts be executed when creating an IPA from command line?

Question - short version:
Why won't my build phase scripts be executed when creating an IPA from the command line?
When I'm running xcodebuild to create an IPA the predefined build phase scripts does not get executed. Why is that?
Question - lengthy version:
I have a workspace with a scheme I want to create an IPA out of from command line.
This works fine except for one thing; I have two scripts in the build phases of the target that is used to put the correct app version (CFBundleShortVersionString) and the correct svn revision number (CFBundleVersion). These to scripts works fine when archiving from xcode but for some reason they do not get run when archiving from command line.
First of all why is that?
Here are the scripts that are working (if archiving form xCode)
When archiving and creating the IPA from the command line I do (the essentials)
# Building
xcodebuild ARCHS="armv7 armv7s" ONLY_ACTIVE_ARCH=NO -workspace MyWorkspace.xcworkspace/ -scheme MyScheme CONFIGURATION_BUILD_DIR=${PROJECT_BUILD_DIR} -configuration Release clean build
# Creating IPA
/usr/bin/xcrun -sdk iphoneos PackageApplication -v "${PROJECT_BUILD_DIR}/${APPLICATION_NAME}.app" -o "${IPA_OUTPUT_PATH}/${APPLICATION_NAME}.ipa"
It works and creates an IPA but none of the build phase scripts gets executed leaving both the revision number and version number untouched.
All suggestions are appreciated!
UPDATE DUE TO BDASH's ANSWER
Instead of making a clean build I make an install as
xcodebuild install ARCHS="armv7 armv7s" ONLY_ACTIVE_ARCH=NO -workspace MyWorkspace.xcworkspace/ -scheme MyScheme CONFIGURATION_BUILD_DIR=${PROJECT_BUILD_DIR} -configuration Release
The predefined script will IN FACT be executed (can be seen in the project version number) with no errors during the install. However the created IPA will have a size of ~300 bytes (instead of ~10MB) and cannot be installed on a device.
Building the app before installing it, i.e.
# Building
xcodebuild clean build ARCHS="armv7 armv7s" ONLY_ACTIVE_ARCH=NO -workspace MyWorkspace.xcworkspace/ -scheme MyScheme CONFIGURATION_BUILD_DIR=${PROJECT_BUILD_DIR} -configuration Release
# Installing
xcodebuild install ARCHS="armv7 armv7s" ONLY_ACTIVE_ARCH=NO -workspace MyWorkspace.xcworkspace/ -scheme MyScheme CONFIGURATION_BUILD_DIR=${PROJECT_BUILD_DIR} -configuration Release
and then creating the IPA will result in an IPA with executed version script and of correct size BUT it is not possible installing it on a device. Trying to put it on a device will give an error message saying
"The program "MyApp" was not installed on you iPhone device "My Device" because an unknown error has occurred."
You have "Run script only when installing" checked for at least one of the script phases. That phase won't be run when using the build action to xcodebuild, only if using the install action.

Resources