Setting Up a Jenkins CI Server

Why would you want to run your own CI server, with so many compelling cloud offerings that are very simple to start using, provide pretty much all the commonly needed functionality, and are generally free to use for at least small projects? Well, there are a few good reasons to go down that path..

  • Maybe you aren't in a position where you can comfortably host your code in a cloud environment
  • You might want to make use of more advanced features and / or have more fine-grained control
  • ..or perhaps you just like learning new things, and enjoy being in control of your full stack!

Whatever the reason, or even if you're not sure yet, I invite you to follow me down this particular rabbit hole to find out! I'll start off by showing one way to install a Jenkins server, and then follow up with examples of setting up an Elixir project, doing deployments, and working with some of the more advanced Jenkins features.

Installation

First, set up an Ubuntu 16.04 machine on eg. DigitalOcean, preferrably with root access using an SSH key.

Second, let's get some Java:

apt-get install -u openjdk-8-jdk

Now we can install Jenkins:

wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | apt-key add -
echo "deb http://pkg.jenkins.io/debian-stable binary/" > /etc/apt/sources.list.d/jenkins.list
apt-get update
apt-get install -y jenkins

NOTE: I tried first with "https://..." according to instructions, but it failed with a curious error:

Err:1 https://pkg.jenkins.io/debian-stable binary/ jenkins 2.19.1
  Protocol "http" not supported or disabled in libcurl
E: Failed to fetch https://pkg.jenkins.io/debian-stable/binary/jenkins_2.19.1_all.deb  Protocol "http" not supported or disabled in libcurl

That the error says "http" stumps me.. If it were "https" instead, I'd suspect that we were missing apt-transport-https, but it's there alright.. so who knows?

(If you do, please let me know in the comments!)

Configuration

Visiting your new server at http://<serverip>:8080_ should present you with an Unlock Jenkins screen. Follow the instructions, pasting in the password and then proceeding to click the Continue button:

Unlock Jenkins

The next screen asks you to customize; we'll just go with Install suggested plugins, as it'll have everything we need to get started:

Customize Jenkins

Setting up Jenkins shouldn't take too long:

Plugin Installation

Finally, you'll be asked to set up a user of your own with admin access. It's a good idea, if nothing else then because you get to set up an e-mail address for notification without fiddling around in the Jenkins settings:

Create User

You're all done:

Jenkins is ready

Creating your first project

When you log in to Jenkins for the first time, you'll be presented with an encouraging welcome screen urging you to create some jobs:

Welcome to Jenkins

So.. well.. let's do that! Click the link, then fill out a project name, select the Freestyle project option and click OK:

Creating a Job

Click the Build tab (or scroll down to the Build section) and select Add build step -> Execute shell:

Add build step

As the shell command, let's just start out with:

echo "Hello, World!"

Save, then click the Build Now button to the left. You should be rewarded with build #1 finishing successfully, and to see what happened, we can pick the Console Output option from the drop-down by the build number in the build history sidebar:

Build History

You should see something similar to this:

Console Output

Summary

Jenkins is fairly easy to set up these days, and includes a default set of popular plugins. We'll look closer at more advanced options later, but even so, just having a shell build step can get you really far.

For the next Jenkins blog post, I'm going to set up a basic Elixir project so that we get something fun to play around with.. and also, I'd like to showcase a convenient approach to deploying releases in this context. It will probably include working with Distillery, as I've been looking forward to try it out.