在 CI (Travis/Jenkins) 环境中使用 xcodebuild (Xcode 8) 和自动签名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39500634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:21:36  来源:igfitidea点击:

Use xcodebuild (Xcode 8) and automatic signing in CI (Travis/Jenkins) environments

xcodejenkinscontinuous-integrationtravis-cixcodebuild

提问by pablobart

With the release of Xcode 8, Apple introduced a new way of managing the signing configuration. Now you have two options Manualand Automatic.

随着 Xcode 8 的发布,Apple 引入了一种管理签名配置的新方法。现在您有两个选项ManualAutomatic

According to the WWDC 2016 Session about Code signing (WWDC 2016 - 401 - What's new in Xcode app signing), when you select Automaticsigning, Xcode is going to:

根据关于代码签名的 WWDC 2016 Session (WWDC 2016 - 401 - What's new in Xcode app signature),当您选择Automatic签名时,Xcode 将:

  • Create signing certificates
  • Create and update App IDs
  • Create and update provisioning profiles
  • 创建签名证书
  • 创建和更新 App ID
  • 创建和更新配置文件

But according to what Apple says in that session, the Automatic Signingis going to use Development signingand will be limited to Xcode-created provisioning profiles.

但根据 Apple 在该会议中所说的,Automatic Signing将使用Development signing并将仅限于 Xcode 创建的配置文件。

The issue comes when you try to use Automatic Signingon a CI environment (like Travis CI or Jenkins). I'm not able to figure out an easy way to keep using Automatic and sign for Distribution (as Xcode forces you to use Development and Xcode-created provisioning profiles).

当您尝试Automatic Signing在 CI 环境(如 Travis CI 或 Jenkins)上使用时,问题就会出现。我无法找到一种简单的方法来继续使用 Automatic 和 Sign for Distribution(因为 Xcode 强制您使用 Development 和 Xcode 创建的配置文件)。

The new "Xcode-created provisioning profiles" do not show up in the developer portal, although I can find then in my machine... should I move those profiles to the CI machine, build for Developmentand export for Distribution? Is there a way to override the Automatic Signingusing xcodebuild?

新的“Xcode 创建的配置文件”没有显示在开发人员门户中,尽管我可以在我的机器中找到......我应该将这些配置文件移动到 CI 机器,构建Development和导出Distribution吗?有没有办法覆盖Automatic Signingusing xcodebuild

采纳答案by pablobart

After trying a few options, these are the solutions that I was able to use on my CI server:

在尝试了几个选项后,这些是我能够在我的 CI 服务器上使用的解决方案:

  • Include the Developer certificate and private key as well as the auto generated provisioning profiles in the CI environment:

  • 在 CI 环境中包括开发人员证书和私钥以及自动生成的配置文件:

Using Automatic signingforces you to use a Developercertificate and auto-generated provisioning profiles. One option is to export your development certificate and private key (Application -> Utilities -> Keychain Access) and the auto-generated provisioning profiles to the CI machine. A way to locate the auto-generated provisioning profiles is to navigate to ~/Library/MobileDevice/Provisioning\ Profiles/, move all files to a backup folder, open Xcode and archive the project. Xcode will create auto-generated development provisioning profiles and will copy them to the Provisioning Profilesfolder.

UsingAutomatic signing强制您使用Developer证书和auto-generated provisioning profiles. 一种选择是将您的开发证书和私钥(应用程序 -> 实用程序 -> 钥匙串访问)以及自动生成的配置文件导出到 CI 机器。找到自动生成的配置文件的一种方法是导航到~/Library/MobileDevice/Provisioning\ Profiles/,将所有文件移动到备份文件夹,打开 Xcode 并存档项目。Xcode 将创建自动生成的开发配置文件并将它们复制到Provisioning Profiles文件夹中。

xcodebuild archive ...will create a .xcarchivesigned for Development. xcodebuild -exportArchive ...can then resign the build for Distribution

xcodebuild archive ...将创建一个已.xcarchive签名的Development. xcodebuild -exportArchive ...然后可以放弃构建Distribution

  • Replace 'Automatic' with 'Manual' when building on a CI environment

  • 在 CI 环境中构建时将“自动”替换为“手动”

Before calling xcodebuilda workaround is to replace all instances of ProvisioningStyle = Automaticwith ProvisioningStyle = Manualin the project file. sedcan be used for a simple find an replace in the pbxprojfile:

拨打电话之前xcodebuild解决方法是更换的所有实例ProvisioningStyle = AutomaticProvisioningStyle = Manual在项目文件。sed可用于在pbxproj文件中简单查找替换:

sed -i '' 's/ProvisioningStyle = Automatic;/ProvisioningStyle = Manual;/' <ProjectName>.xcodeproj/project.pbxproj

sed -i '' 's/ProvisioningStyle = Automatic;/ProvisioningStyle = Manual;/' <ProjectName>.xcodeproj/project.pbxproj

@thelvis also created a Ruby scriptto do this using the xcodeprojgem. The script gives you a better control over what is changed.

@thelvis 还创建了一个Ruby 脚本来使用xcodeprojgem执行此操作。该脚本使您可以更好地控制更改的内容。

xcodebuildwill then use the code signing identity (CODE_SIGN_IDENTITY) set in the project, as well as the provisioning profiles (PROVISIONING_PROFILE_SPECIFIER). Those settings can also be provided as parameters to xcodebuildand they will override the code signing identity and/or provisioning profile set in the project.

xcodebuild然后将使用CODE_SIGN_IDENTITY项目中设置的代码签名标识 ( ) 以及配置文件 ( PROVISIONING_PROFILE_SPECIFIER)。这些设置也可以作为参数提供xcodebuild,它们将覆盖项目中设置的代码签名标识和/或配置文件。

EDIT: with Xcode 9, xcodebuildhas a new build settings parameter CODE_SIGN_STYLEto select between Automaticand Manualso there's no need to find and replace instances of automatic with manual in the project file, more info in WWDC 2017 Session 403 What's New in Signing for Xcode and Xcode Server

编辑:使用 Xcode 9,xcodebuild有一个新的构建设置参数CODE_SIGN_STYLE可供选择AutomaticManual因此无需在项目文件中使用手动查找和替换自动实例,更多信息请参见WWDC 2017 Session 403 Xcode 和 Xcode Server 签名新功能

  • Switch to manual signing

  • 切换到手动签名

Manual signing will provide total control over the code signing identities and provisioning profiles being used. It's probably the cleanest solution, but with the downside of losing all the benefits of Automatic signing.

手动签名将提供对正在使用的代码签名身份和配置文件的完全控制。这可能是最干净的解决方案,但缺点是失去了自动签名的所有好处。

To learn more about code signing with Xcode 8 I really recommend this articleas well as the WWDC2016 session 401 - What's new in Xcode app signing

要了解有关使用 Xcode 8 进行代码签名的更多信息,我强烈推荐这篇文章以及 WWDC2016 session 401 - What's new in Xcode appsignature

回答by d4Rk

I basically run into the same issue using Jenkins CI and the Xcode Plugin. I ended up doing the build and codesigning stuff myself using xcodebuild.

我基本上使用 Jenkins CI 和 Xcode 插件遇到了同样的问题。我最终使用xcodebuild.

0. Prerequisites

0. 先决条件

In order to get the following steps done successfully, you need to have installed the necessary provisioning profiles and certificates. That means your code signing should already be working in general.

为了成功完成以下步骤,您需要安装必要的配置文件和证书。这意味着您的代码签名应该已经可以正常工作了。

1. Building an .xcarchive

1. 建立一个 .xcarchive

xcodebuild -project <path/to/project.xcproj> -scheme <scheme-name> -configuration <config-name> clean archive -archivePath <output-path> DEVELOPMENT_TEAM=<dev-team-id>
  • DEVELOPMENT_TEAM: your 10 digit developer team id (something like A1B2C3D4E5)
  • DEVELOPMENT_TEAM:您的 10 位开发者团队 ID(类似于 A1B2C3D4E5)

2. Exporting to .ipa

2. 导出到.ipa

xcodebuild -exportArchive -archivePath <path/to/your.xcarchive> -exportOptionsPlist <path/to/exportOptions.plist> -exportPath <output-path>

Example of an exportOptions.plist:

示例exportOptions.plist

<!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>development</string>
    <key>teamID</key>
    <string> A1B2C3D4E5 </string>
</dict>
</plist>
  • method: is one of development, app-store, ad-hoc, enterprise
  • teamID: your 10 digit developer team id (something like A1B2C3D4E5)
  • method: 是development, app-store, ad-hoc, 之一enterprise
  • teamID:您的 10 位开发者团队 ID(类似于 A1B2C3D4E5)

This process is anyway closer to what you would do with Xcode manually, than what for example the Jenkins Xcode Plugin does.

无论如何,此过程更接近您手动使用 Xcode 执行的操作,而不是 Jenkins Xcode 插件所做的操作。

Note: The .xcarchive file will always be develpment signed, but selecting "app-store" as method in the 2nd step will do the correct distribution signing and also include the distribution profile as "embedded.mobileprovision".

注意:.xcarchive 文件将始终进行开发签名,但在第二步中选择“app-store”作为方法将进行正确的分发签名,并且还将分发配置文件包含为“embedded.mobileprovision”。

Hope this helps.

希望这可以帮助。

回答by Paul Buchanan

I'm considering another option I've not seen mentioned here yet. Setup two identical targets, that only differ in their signing settings.

我正在考虑这里还没有提到的另一种选择。设置两个相同的目标,仅在签名设置上有所不同。

  • Development Targetuses automatic signing to get all of those benefits when new devices / developers are added
  • CI Targetuses manual signing
  • 当添加新设备/开发人员时,Development Target使用自动签名来获得所有这些好处
  • CI Target使用手动签名

Downside is that you would have to manage two identical targets. Upside is that get the benefits of automatic signing for development, and don't have to maintain potentially brittle scripts that modify your project just before build time.

缺点是您必须管理两个相同的目标。好处是可以获得自动签名开发的好处,并且不必维护在构建时间之前修改项目的潜在脆弱脚本。

回答by Ajeet Sharma

If you are using Xcode 8.x and Jenkins for CI. Then probably you would face issue with "Signing for “YourProjectName" requires a development team. Select a development team in the project editor.

如果您使用 Xcode 8.x 和 Jenkins for CI。那么您可能会遇到“签署“YourProjectName”需要开发团队的问题。在项目编辑器中选择一个开发团队。

Code signing is required for product type 'Application' in SDK 'iOS 10.1'”.** BUILD FAILED ** when running the job.

SDK“iOS 10.1”中的产品类型“应用程序”需要代码签名。**运行作业时构建失败**。

What is the solution?.

解决办法是什么?。

Solution is:

解决办法是:

  1. set Provisioning profile to None in Xcode project build settings.

  2. In jenkins, Create a execute shell before the Xcode setting and write the below command

    sed -i '' 's/ProvisioningStyle = Automatic;/ProvisioningStyle = Manual;/' ProjectName.xcodeproj/project.pbxproj 
    

    Remember: keep that execute shell before Xcode settings in Build section of jenkins.

  1. 在 Xcode 项目构建设置中将 Provisioning profile 设置为 None。

  2. 在 jenkins 中,在 Xcode 设置之前创建一个执行 shell 并编写以下命令

    sed -i '' 's/ProvisioningStyle = Automatic;/ProvisioningStyle = Manual;/' ProjectName.xcodeproj/project.pbxproj 
    

    请记住:在 jenkins 的 Build 部分中的 Xcode 设置之前保留执行 shell。

This works.

这有效。

回答by Marcelo dos Santos

For me, nothing worked. I solved my problem by changing a file in Xcode app installed on your Mac Mini (CI server with Jenkins), as shown in this link:
https://www.jayway.com/2015/05/21/fixing-your-ios-build-scripts/
Additionally I turned off automatic signing from Xcode.

对我来说,没有任何效果。我通过更改 Mac Mini(带有 Jenkins 的 CI 服务器)上安装的 Xcode 应用程序中的文件解决了我的问题,如下链接所示:
https: //www.jayway.com/2015/05/21/fixing-your-ios -build-scripts/
另外,我关闭了 Xcode 的自动签名。

All done! Finally works!

全部完成!终于工作了!

回答by UberGeoff

I noticed my Unity build was never adding a ProvisioningStyle key to my XCode project. I then found a way to manually add the ProvisioningStyle by using a "PostProcessBuild" build script. i.e. a unit of code that is called after the IOS XCode project has been built by Unity.

我注意到我的 Unity 构建从来没有向我的 XCode 项目添加 ProvisioningStyle 键。然后我找到了一种使用“PostProcessBuild”构建脚本手动添加 ProvisioningStyle 的方法。即在 IOS XCode 项目由 Unity 构建后调用的代码单元。

First I had a look at what the project.pbxproj file should look like - when it is set to Manual Provisioning:

首先,我查看了 project.pbxproj 文件的外观 - 当它设置为手动配置时:

/* Begin PBXDictionary section */
    29B97313FDCFA39411CA2CEA /* Project object */ = {
        isa = PBXProject;
        attributes = {
            TargetAttributes = {
                1D6058900D05DD3D006BFB54 /* Unity-iPhone */ = {
                    ProvisioningStyle = Manual;
                };
                5623C57217FDCB0800090B9E /* Unity-iPhone Tests */ = {
                    TestTargetID = 1D6058900D05DD3D006BFB54 /* Unity-iPhone     */;
                };
            };
        };

Then I created my code to replicate the "structure" of the file seen above. (using the XCodeEditor project found here: XCodeEditor)

然后我创建了我的代码来复制上面看到的文件的“结构”。(使用此处找到的 XCodeEditor 项目:XCodeEditor

[PostProcessBuild]
public static void OnPostProcessBuild(BuildTarget target, string path)
{
    // Create a new project object from build target
    XCProject project = new XCProject(path);

    if (target == BuildTarget.iOS)
    {
        //Add Manual ProvisioningStyle - this is to force manual signing of the XCode project
        bool provisioningSuccess = AddProvisioningStyle(project, "Manual");

        if (provisioningSuccess)
            project.Save();
    }
}

private static bool AddProvisioningStyle(XCProject project, string style)
{
    var pbxProject = project.project;

    var attr = pbxProject.data["attributes"] as PBXDictionary;
    var targetAttributes = attr["TargetAttributes"] as PBXDictionary;

    var testTargetIDGuid = FindValue(targetAttributes, "TestTargetID");

    if (!string.IsNullOrEmpty(testTargetIDGuid))
    {
        var settings = new PBXDictionary();
        //here we set the ProvisioningStyle value
        settings.Add("ProvisioningStyle", style);

        targetAttributes.Add(testTargetIDGuid, settings);

        var masterTest = FindValue(targetAttributes, "ProvisioningStyle");

        if (masterTest == style)
        {
            return true;
        }
    }

    return false;
}

private static string FindValue(PBXDictionary targetAttributes, string key)
{
    foreach (var item in targetAttributes)
    {
        var ma = item.Value as PBXDictionary;

        foreach (var di in ma)
        {
            var lookKey = di.Key;

            if (lookKey == key)
            {
                return di.Value.ToString();
            }
        }
    }

    return "";
}

回答by BadmintonCat

What fixed it for me was this: http://code-dojo.blogspot.jp/2012/09/fix-ios-code-signing-issue-when-using.html

为我修复的是:http: //code-dojo.blogspot.jp/2012/09/fix-ios-code-signing-issue-when-using.html

... copying certificates from Login keychain to System keychain. You might also want to set all dev certificates to 'Allow all applications to access this item' (Right-click/Get Info/Access Control).

... 将证书从登录钥匙串复制到系统钥匙串。您可能还想将所有开发证书设置为“允许所有应用程序访问此项目”(右键单击/获取信息/访问控制)。

回答by lastlink

There is a tool called fastlanewhich makes using xcodebuild much easier and it is maintained meaning new updates will continue to provide support for changes to xcode. It makes it much easier to create scripts and config for building and codesigning your app among many other xcode automation tools it supports. I'd recommend giving it a look into.

有一个名为fastlane的工具,它使 xcodebuild 的使用变得更加容易,并且它的维护意味着新的更新将继续为 xcode 的更改提供支持。在它支持的许多其他 xcode 自动化工具中,它可以更轻松地创建脚本和配置以构建和共同设计您的应用程序。我建议去看看。