Setting Up Jenkins on Linux (Ubuntu 14.04)
|This document is part of a series of documents covering Continuous Integration setup for a UI5 application.|
The process of setting up a Jenkins CI server on a Linux machine is a little different compared to Windows. We set up Jenkins on a Windows server in the tutorial here. For this tutorial, we shall use a Linux server running the Ubuntu 14.04 distribution.
At the end of this document is a video in which all the required steps are carried out. You can watch that video for a quick setup.
Note: You will need to have root access on the Linux server to successfully set up Jenkins.
1. Install Git
If you do not already have Git installed, you can install it by executing the following commands:
sudo apt-get update
sudo apt-get install git
2. Install Jenkins
Follow the instructions here to install Jenkins on your system.
If Jenkins does not start automatically after installing, you can start it using the following command:
sudo service jenkins start
3. Install Node and NPM
We will be using node packages in the build of the UI5 application. So we need to install Node.js and NPM. This is done using the following commands:
sudo apt-get update
sudo apt-get install nodejs
sudo apt-get install npm
To run Node, we will have to use the command 'nodejs' which is different from 'node' that is used on Windows. If you would like to use the 'node' command on Linux, you can do so by creating a symbolic link from 'nodejs' to 'node' in the /usr/bin folder as follows:
sudo ln -s /usr/bin/nodejs /usr/bin/node
4. Set up SSH Authentication for 'jenkins' User
Since Jenkins will be checking out code from the on-premise installation of GitHub, it needs to have the relevant access permissions to the repository. We do this by using an SSH key. Since Jenkins will be running as a process owned by the 'jenkins' user, we need to generate and add an SSH key for the 'jenkins' user.
The 'jenkins' user is a service account and therefore does not have a default shell. To run commands as the 'jenkins' user, we change the owner of the current login session to 'jenkins' using the command:
sudo su -s /bin/bash jenkins
Now, generate an SSH key and add it to your GitHub account. You can find the instructions for doing so here. Ensure you perform all the steps as the 'jenkins' user.
5. Configure NPM to install global packages in a different directory
By default, NPM installs global packages in the /usr/local directory. However, the 'jenkins' user does not have write access to this directory and won't be able to install packages there. As a result, global npm install commands that we add in Jenkins jobs will not run. To solve this problem, we create a new directory in the Jenkins home directory (/var/lib/jenkins) called '.npm-packages' and configure NPM to install global packages in that directory. The 'jenkins' user has write access to that directory.
To create the directory, change the login session owner to jenkins by running
sudo su -s /bin/bash jenkins
Then go to the home directory (/var/lib/jenkins) and create the directory '.npm-packages'
Now, configure NPM to install global packages in this newly created directory using the command
npm config set prefix /var/lib/jenkins/.npm-packages
6. Install Required Plugins in Jenkins
The following plugins will need to be installed in Jenkins:
a) Git Plugin for Jenkins
b) GitHub Plugin for Jenkins
Go to Manage Jenkins > Manage Plugins > Available and look for the above plugins if they are not already installed, and install them. After installing the plugins, restart Jenkins.
Note: If you don't get a list of available plugins even after refreshing the list, you might first need to configure the proxy settings. To do this, go to Manage Jenkins > Manage Plugins > Advanced and enter the details as shown:
Click on 'Submit' and then click on 'Check Now' at the bottom-right corner of the page. You should now be able to see the list of all the available plugins in the 'Available' tab.
7. Set up Credentials in Jenkins
We now add our SSH credentials to Jenkins so that it can be authenticated when accessing GitHub.
Click on 'Credentials' on the Jenkins dashboard.
Select your domain. Select 'Global Credentials (unrestricted)' if you don't have domains set up yet.
Click on 'Add Credentials'
Enter the following details:
If you have a passphrase for the SSH key, then click on 'Advanced' and enter you passphrase there.
8. Add a Project in Jenkins
Select 'New Item' in the Jenkins dashboard, and create a new 'Freestyle project'. Enter your project details in the page that opens.
In the 'Source Code Management' section, select 'Git'.
Enter the URL of your repository in SSH format. It will be in the following format: email@example.com:<username>/<project_name>.git
You can get this SSH clone URL from the GitHub repository
For credentials, select 'root' from the dropdown box. These are the credentials that we set up in the previous step.
Click on 'Save' at the bottom of the page and then try to build your project. If everything was configured properly, Jenkins will clone your repository from GitHub and show that the build has passed.
Here's the video in which all the above steps are carried out:
You have now set up Jenkins on a Linux machine to communicate with your repository on GitHub. The next step is to configure your build to run unit tests and other tasks. You can see the article here for details on how to do this for a UI5 project. You can also configure Jenkins to run this build automatically when code is pushed to your repository on GitHub. Check out the tutorial on how to do this here.