Set global environment variables inside Xcode build phase run script

Bryan picture Bryan · Apr 6, 2012 · Viewed 15.5k times · Source

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.

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.

Does anyone know if this is possible?

Answer

sti picture sti · Apr 7, 2012

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