在 Xcode 构建阶段运行脚本中设置全局环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10046394/
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
Set global environment variables inside Xcode build phase run script
提问by Bryan
I'm using Jenkins to do continuous integration builds. I have quite a few jobs that have much of the same configuration code. I'm in the midst of pulling this all out into a common script file that I'd like to run pre and post build.
我正在使用 Jenkins 进行持续集成构建。我有很多工作有很多相同的配置代码。我正在将这些全部提取到一个通用脚本文件中,我想在构建前和构建后运行该文件。
I've been unable to figure out how to set some environment variables within that script, so that both the Xcode build command, and the Jenkins build can see them.
我一直无法弄清楚如何在该脚本中设置一些环境变量,以便 Xcode 构建命令和 Jenkins 构建都可以看到它们。
Does anyone know if this is possible?
有谁知道这是否可能?
回答by sti
It is not possible to do exactly what you ask. A process cannot change the environment variables of another process. The pre and post and actual build steps run in different processes.
不可能完全按照您的要求去做。一个进程不能改变另一个进程的环境变量。前后和实际构建步骤在不同的过程中运行。
But you can create a script that sets the common environment variables and share that script between all your builds.
但是您可以创建一个脚本来设置公共环境变量并在所有构建之间共享该脚本。
The would first call your shell to execute the commands in the script and then call xcodebuild:
将首先调用您的 shell 来执行脚本中的命令,然后调用 xcodebuild:
# Note the dot in the beginning of the next line. It is not a typo.
. set_environment.sh
xcodebuild myawesomeapp.xcodeproj
The script could look like this:
该脚本可能如下所示:
export VARIABLE1=value1
export VARIABLE2=value2
How exactly your jobs will share the script depends on your environment and use case. You can
您的作业将如何共享脚本取决于您的环境和用例。你可以
- place the script in some well-known location on the Jenkins host or
- place the script in the version controlled source tree if all your jobs share the same repository or
- place the script in a repository of its own and make a Jenkins build which archives the script as a build artifact. All the other jobs would then use Copy Artifact plugin to get a copy of the script from the artifacts of script job.
- 将脚本放在 Jenkins 主机上的某个众所周知的位置,或者
- 如果您的所有作业共享同一个存储库,则将脚本放在版本控制的源代码树中,或者
- 将脚本放在它自己的存储库中,并进行 Jenkins 构建,将脚本存档为构建工件。然后所有其他作业将使用 Copy Artifact 插件从脚本作业的工件中获取脚本的副本。
回答by chown
From Apple's Technical Q&A QA1067it appears that if you create the file /Users/YOU/.MacOSX/environment.plist
and populate it with your desired environment variables that all processes (launched by the user with the environment.plist file in their home dir) will pick up these environment variables. You may need to restart your computer (or just log out and back in) before a newly launched process will pick up the variables.
This articlealso claims that Xcode will also pass these variables to a build phase script. I have not tested it yet but next time I restart my MacBook I will let you know if it worked.
从Apple 的 Technical Q&A QA1067看来,如果您创建文件/Users/YOU/.MacOSX/environment.plist
并使用所需的环境变量填充它,则所有进程(由用户在其主目录中使用 environment.plist 文件启动)都将选择这些环境变量。在新启动的进程获取变量之前,您可能需要重新启动计算机(或只是注销并重新登录)。
这篇文章还声称 Xcode 也会将这些变量传递给构建阶段脚本。我还没有测试过,但下次我重新启动我的 MacBook 时,我会告诉你它是否有效。
From http://developer.apple.com/library/mac/#/legacy/mac/library/qa/qa1067/_index.html
来自http://developer.apple.com/library/mac/#/legacy/mac/library/qa/qa1067/_index.html
Q: How do I set environment for all processes launched by a specific user?
A: It is actually a fairly simple process to set environment variables for processes launched by a specific user.
There is a special environment file which loginwindow searches for each time a user logs in. The environment file is: ~/.MacOSX/environment.plist (be careful it's case sensitive). Where '~' is the home directory of the user we are interested in. You will have to create the .MacOSX directory yourself using terminal (by typing mkdir .MacOSX). You will also have to create the environment file yourself. The environment file is actually in XML/plist format (make sure to add the .plist extension to the end of the filename or this won't work).
Q: How do I set environment for all processes launched by a specific user?
A: It is actually a fairly simple process to set environment variables for processes launched by a specific user.
每次用户登录时,loginwindow 都会搜索一个特殊的环境文件。环境文件是:~/.MacOSX/environment.plist(注意区分大小写)。其中“~”是我们感兴趣的用户的主目录。您必须使用终端自己创建 .MacOSX 目录(通过键入 mkdir .MacOSX)。您还必须自己创建环境文件。环境文件实际上是 XML/plist 格式(确保将 .plist 扩展名添加到文件名的末尾,否则这将不起作用)。