Set global environment variables inside Xcode build phase run script Set global environment variables inside Xcode build phase run script xcode xcode

Set global environment variables inside Xcode build phase run script


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:

# Note the dot in the beginning of the next line. It is not a typo.. set_environment.shxcodebuild myawesomeapp.xcodeproj

The script could look like this:

export VARIABLE1=value1export 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.


From Apple's Technical Q&A QA1067 it 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 article also 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.

From 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).