Jenkins on a Mac.

Getting a Jenkins build server installed on a Mac is pretty easy, just download and install the package, then accept the defaults. Getting it working is a bit trickier.

We use Mercurial source control. It was the system I use in my day job, so it seemed like a good enough choice for our mobile game code. Jenkins has a Mercurial plugin so it seemed like an easy job to have a Jenkins server and build agent on the Mac to build and upload our app whenever there’s a check-in.

Installing Jenkins went well enough. Just download and install it from¬†jenkins.io where there’s an installer for Mac. I accepted the defaults as everything seemed fine. Then it opened a browser and pointed it to localhost:8080. So far so good. It invents a default password and logs it to a file. You need this password to get into the browser interface the first time. Even as an admin, I couldn’t open the file it told me to look in, but it’s also logged to¬†/var/logs/jenkins/jenkins.log which I found to be a lot easier to access.

Next step is to set up a Jenkins user. Which again, went smoothly.

Then go to the plugins page. Don’t click the Mercurial plugin or you’ll be on a page telling you everything about it except how to install it. What you actually need to do is to click the box to the left of it, then there’s an install button to make it happen. Turns out it’s pointless though.

I decided that I would prefer all builds to be done via remote build agents, even though the only build agent I planned to have was on the Mac its self. This seemed like a good idea so that I could move things around at a later date without having to figure out what is done in what way.

There doesn’t seem to be a lot of information on setting up a build agent. It turns out that for a Mac, with it’s build in ssh server, all you need to do is create a user for Jenkins to connect as and make sure it has SSH access enabled. The in Jenkins you go to the configure build agents and tell it to use SSH as it’s launch method. I gave the node a label of MacNode so I can tell jenkins to run jobs on that one specifically.

Next, I configured the Mercurial parameters in the Manage Jenkins part of the Jenkins interface as I thought they should be but when I tried to create a job I got an instant red build saying it couldn’t find the hg command. hg is the command for Mercurial and it’s definitely on the Mac as I can run it quite happily, even if logged in as the Jenkins build agent user I created earlier.

Hours passed with me trying just about every mercurial setting I could think of, of find on the internet. Then inspiration struck me. I’ll just ignore the mercurial plugin that clearly doesn’t work, and do it with a script. So I logged in with the Jenkins user and proceeded to setup SSH access keys for the Mercurial server as follows.

ssh-keyget -t rsa -b 4096
less ~/.ssh/id_rsa.pub
{COPY} the key to the clipboard then
ssh to the Mercurial server
echo {PASTE} >> ~/.ssh/authorized_keys

Where {COPY} and {PASTE} just mean highlight it, right click and use the copy and paste in the OS.

Now, I needed to setup the system to poll the mercurial server periodically and do the build if there’s anything new to build. This can be done by chaining two Jenkins jobs. One to check for anything new with the hg incoming command in a script. The script exits with a non zero code if there’s nothing new so Jenkins won’t move onto the next build int he chain as it thinks that one failed. Here’s my polling script.

if hg incoming
then
  echo some changes
  exit 0
else
  echo no changes
  exit 1
fi

The next job is just another script to pull from Mercurial, update the working source and build it. It then checks the changes back in, though only the version number file should have changed.

This triggers a third script to upload the final result to the Android Play Store with Fastlane as described in an earlier blog entry. All three jobs had to have their working directory set to the same place and none of them are set to clear it.

This should have been a simple click to install and setup one or two simple jobs. However, this was a Mac. On a Mac, nothing is ever as simple as it should be.

 

Leave a Reply

Your email address will not be published. Required fields are marked *