Thursday, February 16, 2017

Setup a private Git server on Linux (Mint / Ubuntu / Debian)

Git is a distributed version control system (VCS) created by Linus Torvalds for development of the Linux kernel, with other kernel developers contributing to its initial development.  Git development began in April 2005, after many developers of the Linux kernel gave up access to BitKeeper because his creator had withdrawn free use of the product after claiming that BitKeeper protocols had been reverse-engineered.

The Eclipse Foundation reported that as of May 2014, Git was the most widely used source code management tool; with 42.9% of professional software developers reporting that they use Git as their primary source control system.
Companies like GitHub offer code hosting services based on Git, but free plans usually are limited to open source projects and you are not allowed to host private projects unless you pay for it. The alternative you have for a free service is to set up your own personal Git server.

The following guide will explain how to set up a private Git server for hosting your personal projects. Although you don’t need to set up a Git server if you write your programs in a single computer, since you can use Git in local mode, having a server will allow you to keep and manage all your projects in a common place. You can conveniently access to your code from every computer, even though Internet. Additionally you can invite other friends or colleges to pull your code and collaborate simultaneously in different projects.

Proposed scenario 

 

 

 

The steps involved in the setup are:

#1. Install and configure OpenSSH to allow access through SSH protocol (On server machine)
#2. Create a developers group and add each developer to it (On server machine)
#3. Create a directory for the git repository and configure its permissions (On server machine)
#4. Install Git and create a a new project (On server machine)
#5. Install Git and clone the project (On client machine)


 

# 1. Install and configure OpenSSH

On the server machine open a new console and install OpenSSH

admin@server-pc $ sudo apt install openssh-server


After installation we have to make some changes in the configuration file, so it’s recommended to make a backup of the file before introducing any of the changes.

admin@server-pc $ sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.original


Open the configuration file with a text editor like vi, gedit or xed.

admin@server-pc $ sudo vi /etc/ssh/sshd_config

Find and uncomment the following lines removing the character “#” located at the beginning of each line. This will enable SSH server listen on all adapters thus it can be reached from the network.

ListenAddress ::
ListenAddress 0.0.0.0


Finally, reboot the system or start OpenSSH service from the console.

admin@server-pc $ sudo systemctl restart sshd.service


In case you need a more detailed explanation about the installation process you can visit the Ubuntu documentation site.

# 2. Create a developers group and add each developer to it

In a new console or in the previous one, create the “developers” group. This group will be used for managing permissions all members will have over the repository directory created in step 3.

admin@server-pc $ sudo groupadd developers


Then add all Developer users to the group. For instance, for the user “john” the command would be:

admin@server-pc $ sudo usermod -a -G developers john


# 3. Create a git repository and configure its permissions

Create a new directory for the git repository. Possible target paths could be /srv/git-repo, /var/lib/git-repo or any other file system location of your choice. In my case I have a dedicated volume mounted on /mnt/data/git-repo so I can keep my important data away of the operative system volume.

admin@server-pc $ sudo mkdir /srv/git-repo


Set the group “developers” as owner of the repository directory.

admin@server-pc $ sudo chown -R root:developers /srv/git-repo


Set to the group permissions to read, write and execute.

admin@server-pc $ sudo chmod -R g+rwx /srv/git-repo/


# 4. Install Git and create a new project

Install Git on the server machine using the following command.

admin@server-pc $ sudo apt install git

Create an example project named “myproject”. This step must be repeated every time you want to create a new project on the server, as convention remember to end your project name directory in .git.

admin@server-pc $ git init --bare /srv/git-repo/myproject.git


# 5. Install Git and clone the project

Install Git on the client machine.

user@client-pc $ sudo apt install git

Clone the project using “git clone”.

user@client-pc $ git clone ssh://john@192.168.0.1/srv/git-repo/myproject.git myproject

The above command clones the project “myproject” located on the server path /srv/git-repo/myproject.git. We assume the server is listening on the IP address “192.168.0.1” and the user for the SSH connection is “john”, same user that we previously added on the server to the group “developers”.
After executing the command a directory myproject will be created on the client machine. Every time you want to clone a project you have to execute this command, off course changing all variables according your configurations.
The final step to check everything is working as expected is to create an empty file readme.txt and push that file into the server repository. You can achieve this test by running the following commands:

user@client-pc $ touch readme.txt
user@client-pc $ git add .
user@client-pc $ git commit -m 'Initial commit'
user@client-pc $ git push

It’s important to mention that for commands requiring a connection to the server like clone, fetch, pull, push, etc. Git will prompt you to enter a password, as every SSH connection and operation must be authorized by the server. Notice that the entered password should be the one belonging to the user specified in the “git clone” command, and not the one belonging to the user logged in the client machine.
If your password is complex and/or you find annoying entering a password so often and for so many commands, you can rather generate “SSH keys” so the client machine can automatically authenticate to the server.

 

Conclusion

Having your own Git server will expand your options at managing your code in several aspects: You will get better understanding of advanced Git commands and take advantage of their use, you can share projects with your colleagues or friends, projects are centralized and reachable from every computer from the network, backups are more convenient (specially if you have more than one computer), and many other factors o needs you might have.

Friday, February 10, 2017

Installing Jenkins on Linux (Mint / Ubuntu)


Jenkins is an open source automation server written in Java that supports continuous integration (CI) and continuous delivery (CD) practices. Nowadays it’s one of the most popular solution between its competitors (TeamCity, Bamboo, CruiseControl, etc..).

The server has an impressive web interface, supports non-Java projects and through plugins the built-in functionality can be conveniently extended. Plugins are vital as they support the integration with several version control systems, databases, build automation software, reporting tools, test frameworks, and general purpose solutions.

Jenkins can be installed as a standalone component or in a servlet container such as Tomcat. The following guide describes how to install Jenkins as a standalone component on Linux Mint/Ubuntu distributions, with some changes or no changes at all installation process might also work in other distributions.

The first step is download the .war release of your preference from the Jenkins official site, I recommend you downloading the LTS (Long-Term Support) Release. Once downloaded place the .war file in a directory of your choice, for instance /opt/jenkins. In my case I have the file in /mnt/data/apps/jenkins as I have all my apps and data mounted in another volume, which is very convenient for me when I have to update or reinstall my Linux distribution.

In the same directory you placed the .war, create a second file named jenkins.sh with the following content. In the script pay attention to the variable “JENKINS_ROOT” as it must point to the directory in which you are placing your files.

### BEGIN INIT INFO
# Provides:        jenkins
# Required-Start:  $network
# Required-Stop:   $network
# Default-Start:   2 3 4 5
# Default-Stop:    0 1 6
# Short-Description: Jenkins CI Server
### END INIT INFO

DESC="Jenkins CI Server"
NAME=jenkins
PIDFILE=/var/run/$NAME.pid
RUN_AS=root
PATH=/sbin:/bin:/usr/sbin:/usr/bin

JENKINS_ROOT=/mnt/data/apps/jenkins
export JENKINS_HOME=$JENKINS_ROOT/data

start() {
    start-stop-daemon --start --background --make-pidfile --pidfile $PIDFILE --chuid $RUN_AS --exec /usr/bin/java -- -jar $JENKINS_ROOT/jenkins.war
}

stop() {
    start-stop-daemon --stop --pidfile $PIDFILE
    if [ -e $PIDFILE ]
        then rm $PIDFILE
    fi
}

case $1 in
  start|stop) $1;;
  restart) stop; start;;
  *) echo "Run as $0 "; exit 1;;
esac

Additionally, in the above script, pay attention to the variable “JENKINS_HOME” as it’s redirecting the Jenkins home directory. This directory is the storage location where Jenkins saves all data. By default, home directory is set to ~/.jenkins but you can change the “JENKINS_HOME” variable to point to another location or just comment/delete this line to set the home directory to its default.

$ sudo cp ./jenkins.sh /etc/init.d/jenkins
$ sudo chmod 755 /etc/init.d/jenkins
$ sudo update-rc.d jenkins defaults

If everything goes as planned, after restarting your computer Jenkins will be automatically executed during the system initialization without any user intervention or login operation. You can also manage the service state from a console by running the command.

$ service jenkins <stop|start|restart>


Conclusion


As you can see installing Jenkins as a standalone component using this manual method is a transparent and a simple task. Furthermore, by manually editing the loading script you can customize the location of both Jenkins .war file and the home directory.

Hope you find this guide helpful. If you have any question or suggestion, please don’t hesitate to contact me; I will be willing to answer you.