Xcode Server Hacks: 3. Install CLI tools for _xcsbuildd

This article is Part 3 of a whole series called Xcode Server Hacks. Check out Part 2 first if you haven't already. New posts are always tweeted by me (@czechboy0) on Twitter.


In Tutorial 3: Prebuild & Postbuild Scripts we discussed running many useful tools like CocoaPods and fastlane with each Integration of our code. However, this became a bit more complicated with the introduction of the new rootless mode in OS X El Cap. Also, if you prefer to use version managers like rvm, nvm for your Ruby and Node versions, this will help you keep using them instead of having to install your CLI tools globally.

TL;DR? Login as _xcsbuildd, install your tools and then add source ~/.bash_profile to your bot scripts.

The goal is to get a prebuild script like this working again (where PROJECT_NAME is your project’s name), which breaks if you’re using rvm or are using El Cap.

cd PROJECT_NAME
fastlane prebuild

1. Installing rvm for _xcsbuildd

Many of us like to keep around multiple versions of Ruby and Node with version managers like rvm, nvm and other *vm tools. These tend to install the tool in question into your home directory, e.g. ~/.rvm/... for Ruby.

However, as we found in the last tutorial, environment config files like ~/.profile, ~/.bash_profile and ~/.zshrc don’t get loaded for the _xcsbuildd user before Xcode Server performs a build.

The way to fix that is to install rvm (or any other version manager) when you’re logged-in as _xcsbuildd. So let’s do that, log in as _xcsbuildd with:

sudo su - _xcsbuildd

and install rvm and the latest stable version of Ruby with

\curl -sSL https://get.rvm.io | bash -s stable
source ~/.profile
rvm autolibs read-only
rvm install ruby

(the second line will fixes a Homebrew permissions issue)

2. Force-load ~/.*profile in your scripts

There, _xcsbuildd (Xcode Server’s build user) now has the same Ruby version that you need to run fastlane (in our example). However, this alone won’t work, because _xcsbuildd doesn’t load its environment config files like ~/.bash_profile. We have to do that in our script manually, by adding source ~/.bash_profile (if you’re using bash, otherwise replace with your shell’s config file) to the prebuild script itself. The new prebuild script then looks like:

cd PROJECT_NAME
source ~/.bash_profile
fastlane prebuild

Help me help you

Don’t you absolutely hate this? I do. And I would greatly appreciate if you dupped my open radar requesting that _xcsbuildd loads its config files properly. Let’s hope that this gets fixed ASAP!


I hope you found this useful or interesting. For criticism, praise and future articles, I’m @czechboy0 on Twitter.