xcodebuild - set certificate through commandline - ios

I have an Xcode project for an iOS application.
I plan building it through commandline using xcodebuild.
I will be adding minor customizations from build to build, producing slightly different versions of the application (for branding purpose).
What I need is a way to automatically set a specific certificate for each build. Is it possible to somehow set a certificate for the build through command line?
To clarify - every time I build the project, I want to use a new certificate.

before the build, set environment variable CODE_SIGNING_IDENTITY="name of certificate"

Related

How to remove scripts (in a framework) when the ios app is archived?

I'm working on a cocoa pod framework which has a couple of shell scripts in it. After installing the pod, the user should add the following in app's build phase -> run script :
“${PODS_ROOT}/MyPod/MyPod/framework/MyPod.framework/scripts/build.sh”
After this the build works fine and app runs. But archiving creates problems. XCode gives an error saying:
iTunes Store Operation Failed
Invalid Signature. Code object is not signed at all. The file at path [SampleApp.app/Frameworks/MyPod.framework/scripts/build.sh] is not properly signed. Make sure you have signed your application with a distribution certificate, not an ad hoc certificate or a development certificate. Verify that the code signing settings in Xcode are correct at the target level (which override any values at the project level). Additionally, make sure the bundle you are uploading was built using a Release target in Xcode, not a Simulator target. If you are certain your code signing settings are correct, choose "Clean All" in Xcode, delete the "build" directory in the Finder, and rebuild your release target. For more information, please consult https://developer.apple.com/library/ios/documentation/Security/Conceptual/CodeSigningGuide/Introduction/Introduction.html
I've two concerns now:
Xcode doens't allow scripts to be a part of archive?
If thats the case, then how do I remove these scripts from archive? Is there a build setting like ${ARCHIVE_DIR} from where I can search for the latest archive and then remove the script. Or, is there some build setting where I can mention what files to ignore while building or archiving?
Also, I've already tried adding these files to 'exclude source file names' to the app's build settings. It didn't work since the script to be executed isn't a part of the app, but part of the framework integrated in app.
Also, my framework is a fat framework generated by using lipo on simulator build and device build. Which means that framework won't be code signed.
Apple does not allow simulation code inside framework when push to store, to overcome this, either ship the framework without simulator arch, or use external script to remove it during app build.
Example script can be found in some framework like Realm, basically you have to add the script file in your framework, and tell user/dev to call this on their build script (it's the strip-frameworks.sh)
This issue is stated in this archiving universal framework bug

Jenkins - command line build iOS project

I have an iOS project cloned from repo into my jenkins account. Xcode is not yet installed. I know command line scripts to build an iOS project including targets/configurations/profiles etc. Certificate and provisioning profile is installed in jenkins account. I am wondering if there is a way to build and create ipa without installing Xcode in jankins account. Does apple provide any developer tools for building and creating IPA through command line only?
Advance thanks
Every building option for iOS apps requires Xcode, but apple does provide tools for building via CLI. A ton has changed in the most recent iOS OS upgrade and there are incredible tools out there for CI/CD app delivery. What you end up using should really depend on what you want to maintain over time and how advanced your system needs to be.
To get Builds working in Jenkins
Step 1 : Install Xcode on the Jenkins node that will be doing the builds. You wont be using the Xcode UI but you will need it installed, there is currently no way to build IPAs without Xcode.
Step 2: Choose your build tool
You could write your own build scipts and manage provisioning profiles on the machine. xcodebuild is the CLI tool you are looking for. If you end up going down this route make sure to use xcpretty or you will loose your mind with giant build logs.
Fastlane is an amazing toolset for building mobile apps, it might change your life. Check out these examples and how to get started.
Apple has an xcode build server that Jenkins could possibly talk to.
The first two in this list do a good job of managing provisioning profiles out of the box, and that can save you major headaches down the road if you need to scale

Can Xcode (command-line) build an archive without a scheme?

I'm primarily a .NET developer but am trying to compile some applications for iOS using Xcode command-line tools. My goal is to use the command-line to write a script to clone a repository, compile the code, and sign it with my iOS developer certificate.
However, when I clone a Git repository for an iOS/OSX application, it usually only contains a Xcode project file (and sometimes a workspace file). When I try to use 'xcodebuild archive' it requires a scheme to be specified but 'xcodebuild -list' says that none exist.
What is the correct way to automatically generate these schemes from the command-line?

How can I use terminal to assign the cert, and build an iOS application?

I would like to use terminal to automatic my application build working flow, how can I change the cert and build the application from terminal only? Thanks.
Yes, you may use terminal to build the app.
There is an xcodebuild tool which allows to do that ($ man xcodebuild would explain a lot).
As for your question – as I understand you're trying to make a build with a different mobileprovision profile. It's possible by adding PROVISIONING_PROFILE parameter to xcodebuild command. And you need to provide profile's UUID.
In two words, each mobileprovision profile has an identifier and it should be "installed" (installation via Xcode is OK). So, you should follow these steps to make a build from the terminal:
Check if the mobileprovision profile is installed (it could be done from Xcode).
Get the mobileprovisioning UUID. The simplest way to do that is the next:
Find the profile in Xcode's organizer
Reveal it in Finder (profile's file name differs from the profile name)
Open the profile in vim or any other editor (it's a signed plist so don't be afraid of the binary part)
Find the <key>UUID</key> string and the corresponding identifier (it should look like AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE), BTW this UUID is used by Xcode as a file name.
Go to your project's folder in terminal (this one which contains .xcodeproj) and run the next command:
xcodebuild PROVISIONING_PROFILE=<YOUR-PROFILE-IDENTIFIER>
That would make a build and store its results in build directory.
You may also specify configuration which should be used, base sdk, target and a lot of different parameters.

Xcode: Can I set Code Signing Profile from the command line

I have a batch build script that I run to build a common codebase for iOS apps into 100+ unique IPAs. I use Xcode 4.2 on OS X Lion.
The build settings for each app are set using PlistBuddy and the build works by running
PlistBuddy, installing app icons, running xcodebuild, then xcrun. It has been tested and
works correctly when targeting a single app.
Each app has a unique distribution provisioning profile that currently must be manually assigned via Xcode's Project settings -> Code Signing section (for release builds).
My problem is that the build script will not be able to match the App to its profile during the xcodebuild phase. It will simply try to use the most recent app's profile, then generate the error:
[BEROR]Code Sign error: Provisioning profile 'MOST RECENT APP'S PROVISIONING PROFILE ID HERE' can't be found.
Desperately looking for a way to either set this via command line (security, plistbuddy, or xcodebuild?). My batch script is quite useless without it.
I found the answer - it was simple. The problem was that in my Xcode target Code Signing settings, I had set a specific distribution profile for the build (out of 90 or so).
What fixed it was simply selecting the profile under "Automatic Profile selection".
Once that was done, xcodebuild was able to run successfully.

Resources