Configuring Jenkins on Ubuntu (for C++, GTest and Bitbucket)


wget -q -O - | sudo apt-key add -
sudo sh -c 'echo deb binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt-get update
$ sudo apt-get install jenkins

Once installed, Jenkins will run at port 8080 on your server.

  • Don’t forget to install a C/C++ compiler
  • Don’t forget to install dependencies like QT, libc++ and Boost



Before doing anything else, you must secure your installation.
Navigate to Jenkins, Manage Jenkins, Configure Global Security.

On this page you can specify all security settings. The most simple case is to use Jenkins’ own user database.
If you refresh the page after enabling this option, you will see a Register button in top right corner of the page. Create an account before proceeding.

Now you have an account, you can select Matrix-based security. This will show a list (matrix) of all security options. You can restrict the access for the anonymous user there.
You should add your account to the security matrix with administrator access before saving. Otherwise you will lock yourself out!
You can eventually add new users there with their own permissions.

Jenkins global security matrix

If you lock yourself out, you can manually disable security in /var/lib/jenkins/config.xml.
Change <useSecurity>true</useSecurity> to <useSecurity>false</useSecurity> and restart Jenkins: sudo service jenkins restart.

Adding build job

On some Jenkins installations you must install the Git plugin before you can use git as SCM.

SSH keys

We also need to configure SSH keys for Bitbucket so we don’t have to use plain text passwords.

$ ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/Mathias/.ssh/id_rsa): 
/Users/Mathias/.ssh/id_rsa already exists.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
passphrase too short: have 3 bytes, need > 4
Saving the key failed: /Users/Mathias/.ssh/id_rsa.
The key fingerprint is:
4c:72:6c:3a:7f:a6:45:80:92:e7:39:b7:73:7b:c6:51 Mathias@Mac-Pro-van-Mathias.local
The key's randomart image is:
+--[ RSA 2048]----+
|       . 0..E    |
|      o.o...     |
|      .+B. o.    |
|       *+oo.+    |
|      C So.=+    |
|          o=.    |
|         ..o     |
|                 |
|                 |

On Bitbucket go to Manage account, click SSH keys.
The SSH Keys page displays. If you have any existing keys, those appear on this page.
Back in your terminal, copy the contents of your public key file.

$ cat ~/.ssh/

More information on working with SSH keys (and Bitbucket) can be found here.

Add credentials to Jenkins

Now we have generated keys for our Git authentication, we can add them to the credentials in Jenkins.
On the built configuration screen select SSH username with private key for credentials.

Select your username, and Enter directly for the private key.
Again, you can get the key from the console, but this time we need the private key:

$ cat ~/.ssh/id_rsa

You also need to specify the passphrase (if you set one before).

Jenkins Credentials SSH key git

Configuring build triggers

Choose for Trigger builds remotely and specify a really random authentication token.
You can for example generate a random token using OpenSSL: $ openssl rand -base64 16.

You also need the API key from your Jenkins account. Go to your profile page and click on Configure. On the configuration page click on Show API Token.

Finally you can add trigger on Bitbucket. Go the repository’s settings page, and navigate to Services. Add a new Jenkins service with the following details:

  • Endpoint: Jenkins URL in the following format —
  • Project name: the project name that you’ve set in Jenkins
  • Token: the authentication token of the project (that we generated earlier).

Schermafbeelding 2015-10-18 om 19.06.31

If you now do a push to your Bitbucket remote repo, it will automatically trigger a Jenkins build.

Configuring build

If you want to build the project with CMake, you might have to install the CMake plugin (and you need CMake on your server).
Maybe you also need the Build Environment plugin.

Once installed, you click on Add build step and you choose CMake Build. You can also specify a Shell invocation if you want to use your own make file or build commands.

I won’t dig deeper into specific build settings, since that’s irrelevant for you particular project…

Configuring unit test (gtest)

If you use GoogleTest, you can create a test output report with Jenkins. Execute gTest with this flag: --gtest_output=xml:gtestresults.xml.

You also need to replace notun by skipped since Jenkins doesn’t like the first one.

awk '{ if ($1 == "<testcase" && match($0, "notrun")) print substr($0,0,length($0)-2) "><skipped/></testcase>"; else print $0;}' gtestresults.xml > gtestresults-skipped.xml
mv gtestresults.xml

Now you have the xml report file, you must add the Publish JUnit test result report Post-build action with the gtestresults-skipped.xml file.