简体   繁体   English

你能将环境变量传递给xcodebuild吗?

[英]Can you pass environment variables into xcodebuild?

I have two Jenkins jobs that run our functional tests. 我有两个运行功能测试的Jenkins工作。 One job is for whenever something is submitted for code review and the other job runs whenever something is pushed to master. 一个工作是每当提交代码审查的东西时,另一个工作是在任何东西被推送到掌握时运行。

Because these are functional tests they test entire flows of the application which end up modifiying the user state. 因为这些是功能测试,所以它们测试应用程序的整个流程,最终修改用户状态。 The issue we are having right is that every job uses the same account so whenever two Jenkins jobs are running in parallel they modify the same account which can put them in an unexpected state and fail the test. 我们遇到的问题是每个作业都使用相同的帐户,因此每当两个Jenkins作业并行运行时,他们会修改同一个帐户,这会使他们处于意外状态并且无法通过测试。

My plan was to use Jenkins' BUILD_NUMBER environment variable and by applying a bit of arthimetic to it I could have a guaranteed unique number for the job. 我的计划是使用Jenkins的BUILD_NUMBER环境变量,并通过应用一些关节,我可以得到一个保证唯一的工作号码。 This unique number could then be passed into xcodebuild as an environment variable and the tests could use this number to ensure that every Jenkins' is working on a unique account. 然后可以将此唯一编号作为环境变量传递到xcodebuild,测试可以使用此编号来确保每个Jenkins都在使用唯一帐户。

The problem is that I cannot find any way to pass environment variables into xcodebuild. 问题是我找不到任何方法将环境变量传递给xcodebuild。 I know it is possible for you to pass in user-defined build settings via xcodebuild (or xcargs if you're using Fastlane) but those values do not seem to be accessible as environment variables. 我知道您可以通过xcodebuild传递用户定义的构建设置(如果您使用的是Fastlane,则传递xcargs),但这些值似乎不能作为环境变量访问。 They are accessible by the preprocessor and so you could use it to export the value to your Info.plist and then read it from there. 它们可由预处理器访问,因此您可以使用它将值导出到Info.plist,然后从那里读取它。 But then you have baked these value into your binary and it cannot be changed unless you rebuild it which is not ideal. 但是你已经把这些值加到你的二进制文件中了,除非你重建它并不理想,否则不能更改它。 Also at this point in time I could just have Jenkins write to a file on disk and have the tests read from it. 此时此刻,我可以让Jenkins写入磁盘上的文件并从中读取测试。 It is essentially the same functionality and saves me from having to pass in build settings. 它本质上是相同的功能,使我免于传递构建设置。

I remember using something like GCC_PREPROCESSOR_DEFINITIONS to pass my own var 我记得使用像GCC_PREPROCESSOR_DEFINITIONS这样的东西传递我自己的var

I had to shell escape the quotes. 我不得不逃避报价。 I ended up coding it into my fastlane build file. 我最终把它编码到我的fastlane构建文件中。

in ruby it looked like this: 在红宝石中它看起来像这样:

tmp_other_flags = {
  GCC_PREPROCESSOR_DEFINITIONS: '"DISABLE_PUSH_NOTIFICATIONS=1"',
  TARGETED_DEVICE_FAMILY: '1',
  DEBUG: '1'
}
other_flags = tmp_other_flags.map do |k, v|
  "#{k.to_s.shellescape}=#{v.shellescape}"
end.join ' '
puts "___ Custom Flags also know as xcargs:"
puts other_flags

gym(
  clean: true,
  silent: false,
  project: proj_xcodeproj_file,
  archive_path: "build-ios-xcarchive",
  destination: 'generic/platform=iOS',
  use_legacy_build_api: true,
  output_directory: 'build-ios',
  output_name: "MyApp.ipa",
  export_method: 'ad-hoc',
  codesigning_identity: 'iPhone Distribution: company (12345)',
  provisioning_profile_path: './dl_profile_com.company.myapp.iphone.prod_ad_hoc.mobileprovision',
  scheme: 'MyApp',
  configuration: 'Debug',
  xcargs: other_flags
)

it ended up getting called in the shell something like this: 最终在shell中调用了这样的东西:

set -o pipefail && xcodebuild -scheme 'MyApp' -project 'platforms/ios/MyApp.xcodeproj' -configuration 'Debug' -destination 'generic/platform=iOS' -archivePath 'build-ios-xcarchive.xcarchive' GCC_PREPROCESSOR_DEFINITIONS=\"DISABLE_PUSH_NOTIFICATIONS\=1\" TARGETED_DEVICE_FAMILY=1 DEBUG=1 clean archive CODE_SIGN_IDENTITY='iPhone Distribution: My Company (Blah)' | tee '/Users/andxyz/Library/Logs/gym/MyApp-MyApp.log' | xcpretty

xcodebuild - how to define preprocessor macro? xcodebuild - 如何定义预处理器宏?

So, perhaps you could pull in your own environment variable using ruby inside of fastlane. 所以,也许你可以使用fastlane中的ruby来引入你自己的环境变量。 by adding your var into the GCC_PREPROCESSOR_DEFINITIONS section 将您的var添加到GCC_PREPROCESSOR_DEFINITIONS部分

ruby can access the environment, for example: ruby可以访问环境,例如:

ENV.fetch('TERM_PROGRAM') #returns "iTerm.app" on my machine

so following along with above: 所以跟着上面:

tmp_other_flags = {
  GCC_PREPROCESSOR_DEFINITIONS: "MY_VARIABLE=#{ENV.fetch('MY_VARIABLE')}" ,
  TARGETED_DEVICE_FAMILY: '1',
  DEBUG: '1'
}

HTH HTH

Via @alisoftware, you can use xcargs to pass additional variables in: 通过@alisoftware,您可以使用xcargs传递其他变量:

gym(
  scheme: scheme,
  xcargs: {
    :PROVISIONING_PROFILE => 'profile-uuid',
    :PROVISIONING_PROFILE_SPECIFIER => 'match AppStore com.bigco.App'
  },
  codesigning_identity: "iPhone Distribution: BigCo, Inc. ()",
)

emits this during the build: 在构建期间发出此信息:

+---------------------+-------------------------------------------------------------------------------------------------+
|                                             Summary for gym 2.53.1                                                    |
+---------------------+-------------------------------------------------------------------------------------------------+
| scheme              | Bespoke-iOS                                                                                     |
| xcargs              | PROVISIONING_PROFILE=profile-uuid PROVISIONING_PROFILE_SPECIFIER=match\ AppStore\ com.bigco.App |
…

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 xcodebuild build-for-testing 命令中传递 dart 环境变量 - Passing dart environment variables in xcodebuild build-for-testing command 我可以将参数从 Qt Creator 传递给 xcodebuild 吗? - Can I pass parameters to xcodebuild from Qt Creator? 您如何确定XCode使用的构建选项,以便可以将它们与xcodebuild一起使用? - How do you determine the build options that XCode uses so you can use them with xcodebuild? 如何将xcrun环境变量传递给Calabash启动器? - How to pass xcrun environment variables to Calabash launcher? “您无法打开该应用程序,因为这种类型的Mac不支持该应用程序。” xcodebuild - “You can’t open the application because it is not supported on this type of Mac.” xcodebuild xcodebuild可以管理自动签名吗? - Can xcodebuild manage automatic signing? xcodebuild无法连接到模拟器 - xcodebuild can't connect to the Simulator 如何在 xcodebuild 命令中传递配置文件名称? - How to pass the provisioning profile name in xcodebuild command? xcodebuild 如何将 WARNING_CFLAGS 传递给 buildsettings - how xcodebuild to pass WARNING_CFLAGS to buildsettings 如何将钥匙串传递给xcodebuild的“检查依赖项”步骤 - How to pass keychain to “Check dependencies” step of xcodebuild
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM