From time to time, you need to build a project with a specific version of Xcode. Sometimes, you need to do so with automated builds in Jenkins. Unfortunately, Jenkins doesn’t provide a clear cut way to do this. It turns out it’s not too difficult to do, but I searched around quite a bit and couldn’t find anywhere anyone had addressed it. So I am documenting the process here in hopes that it may help someone out there, including potential future versions of me who have forgotten it.
My first thought was to invoke xcode-select before running the build. I wasn’t very happy about the idea, and it turns out to be a nonstarter anyway as only root can run xcode-select. But if you take a look at the man page for xcode-select, you’ll discover that an alternative means of selecting which version of the xcode tools to use is to set the DEVELOPER_DIR environmental variable.
I believe you can set global environmental variables in Jenkins, but what if you need to set this one way for some projects, and another for others? To do this, you’ll need to use the Environment Injector Plugin. You can install it from the Jenkins Manage Plugins page.
After the Environment Injector Plugin is installed, a new section will appear in your project configuration called Build Environment. Check the “Inject environment variables to the build process” checkbox in that section. This will add a few more text boxes. In the box labeled “Properties Content”, add your environmental variable:
Note that the DEVELOPER_DIR variable must be more specific than what you might pass in to xcode-select. It must point to the actual Developer folder in the Xcode app bundle, rather than just the bundle itself.
I have added this step to every project I have set up in Jenkins, so that it doesn’t matter which version I may have set up with xcode-select. I always rename old versions of Xcode with their version number (as seen above) and leave the current version named Xcode.app. For projects which can always be built with the Latest & Greatest, this means I don’t have to do anything more. If I ever need to build them with an older version, I can go in and change it.
I feel that this is something that probably ought to be built into the Jenkins Xcode plugin, so I went to try to file a bug report/feature request about it. Unfortunately, this requires you to sign up for an account with their JIRA system, which I felt was too much friction for me to make this minor request. I think it should always be possible for users to report bugs and request features without having to sign in to any system. Otherwise, who knows how many issues you are missing because users find the process of reporting the issue too daunting? But, that’s a different story…