xcode 您可以将环境变量传递给 xcodebuild 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39478895/
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
Can you pass environment variables into xcodebuild?
提问by Reid Main
I have two Jenkins jobs that run our functional tests. One job is for whenever something is submitted for code review and the other job runs whenever something is pushed to master.
我有两个 Jenkins 工作来运行我们的功能测试。一项工作用于提交代码的内容,而另一项工作则在将某些内容推送到 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. 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.
我的计划是使用 Jenkins 的 BUILD_NUMBER 环境变量,通过对它应用一些算术,我可以获得一个有保证的工作唯一编号。然后可以将此唯一编号作为环境变量传递到 xcodebuild 中,测试可以使用此编号来确保每个 Jenkins 都在使用唯一帐户。
The problem is that I cannot find any way to pass environment variables into 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. 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. 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. It is essentially the same functionality and saves me from having to pass in build settings.
问题是我找不到任何方法将环境变量传递给 xcodebuild。我知道您可以通过 xcodebuild(或 xcargs,如果您使用 Fastlane)传递用户定义的构建设置,但这些值似乎无法作为环境变量访问。它们可由预处理器访问,因此您可以使用它将值导出到您的 Info.plist,然后从那里读取它。但是随后您已将这些值烘焙到您的二进制文件中,除非您重建它,否则它无法更改,这并不理想。同样在这个时间点,我可以让 Jenkins 写入磁盘上的文件并从中读取测试。它本质上是相同的功能,使我不必传递构建设置。
回答by andxyz
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:
在 ruby 中它看起来像这样:
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?
So, perhaps you could pull in your own environment variable using ruby inside of fastlane. by adding your var into the GCC_PREPROCESSOR_DEFINITIONS
section
因此,也许您可以在 fastlane 中使用 ruby 引入自己的环境变量。通过将您的 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
回答by cbowns
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 |
…