15

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?

Bryan
  • 614
  • 1
  • 8
  • 18
  • Perhaps this answer could help - http://stackoverflow.com/a/32743908/67824. It doesn't let you define global environment variables, but it does allow you to change xcodeproj build settings which is presumably what you want the environment variables for (at least that's the case for me). – Ohad Schneider Nov 09 '16 at 13:20

2 Answers2

6

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.
sti
  • 11,047
  • 1
  • 27
  • 27
  • 1
    I do not see how this helps. The reason me (and so many others) are looking into this, is to have the ability of one build-step affect the next build-steps via setting Environment variables. In my case, even setting a predefined processor macro would suffice, but your answer does neither. Or maybe you mean a certain build-step should WRITE TO DISK the script that exports variables, and that is run by all other targets? – Motti Shneor Jan 06 '16 at 10:27
-3

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

chown
  • 51,908
  • 16
  • 134
  • 170