Installation

Download the jenkins.war file

_$: wget http://mirrors.jenkins-ci.org/war/latest/jenkins.war

Extract the jenk.7z. Be aware that creates a hidden .jenkins directory. Move the .jenkins directory to your home: /home/<user>

Run

_$: java -jar jenkins.war

Go to http://localhost:8080 and check that is running. If you want to check the port it is running in, use the httpPort flag:

_$: java -jar jenkins.war --httpPort=8000   # Run in the 8000 port

Command line interface installation

Go to http://localhost:8080/cli and follow the instructions to download the jenkins-cli.jar file.

_$: java -jar jenkins-cli.jar -s http://localhost:8080 list-jobs
Git polling
...
selenium_tests
Total_build

Directory structure

_$: pwd
/home/jenkins/tests

_$: tree
.
├── firefox
│   ├── firefox
│   │   ├── ...
│   │   ├── firefox
│   │   ├── firefox-bin
│   └── firefox-24.6.0esr.tar.bz2
├── requirements.txt
├── selenium
│   ├── nosetest.xml
│   ├── test_admin.py
│   └── test_admin.pyc
├── selenium-server-standalone-2.42.2.jar
├── unit
│   ├── nosetest.xml
│   ├── tests.py
│   └── tests.pyc
└── venv
    ├── ...
/home/jenkins/tests/requirements.txt:
-------------------------------------
coverage==3.7.1
nose==1.3.3
selenium==2.42.1

Examples

1) Create a Python virtual environment

#!/bin/bash -

VENV=$WORKSPACE/venv
PIP_CACHE=/tmp/pip-cache

printf "venv: %s\n" $VENV

if [ ! $(ls -A $VENV 2> /dev/null) ]
then
    mkdir $VENV
    virtualenv $VENV
fi

source ${VENV}/bin/activate || exit 1

mkdir -p ${PIP_CACHE} || exit 1
pip install -r requirements.txt --download-cache ${PIP_CACHE}
pip install -e .

2) Unit tests

#!/bin/bash -

TEST_PATH="/home/jenkins/tests/unit"
ENV_PATH="/home/jenkins/tests/venv"

source ${ENV_PATH}/bin/activate || exit 1

nosetests --with-xunit --xunit-file=${TEST_PATH}/nosetest.xml ${TEST_PATH}

3) Functional tests with selenium

#!/bin/bash -

TEST_PATH=$WORKSPACE/taloki/tests/selenium
VENV=$WORKSPACE/venv
FIREFOX_PATH=/srv/jenkins/firefox/firefox
PATH=$PATH:$FIREFOX_PATH

source ${VENV}/bin/activate || exit 1

# Prepare X server
Xvfb :99 -ac 2> /dev/null &
export DISPLAY=:99

# Run tests
PATH=$PATH nosetests --with-xunit --xunit-file=${TEST_PATH}/nosetest.xml ${TEST_PATH}
status=$?

# Terminate X server
pkill Xvfb
exit $status

Running Jenkins as a service

/etc/init.d/jenkins:
--------------------
#!/bin/sh

DESC="Jenkins CI Server"
NAME=jenkins
PIDFILE=/var/run/$NAME.pid
RUN_AS=jenkins
COMMAND="/usr/bin/java -- -jar /home/jenkins/jenkins.war"

d_start() {
    start-stop-daemon --start --quiet --background --make-pidfile --pidfile $PIDFILE --chuid $RUN_AS --exec $COMMAND
}

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

d_status() {
    start-stop-daemon --status --pidfile $PIDFILE
    status=$?
    case $status in
        0)
            echo -n "running"
            ;;
        1)
            echo -n "not running (pid file exists)"
            ;;
        3)
            echo -n "not running"
            ;;
        4)
            echo -n "unable to determine"
            ;;
    esac
}

case $1 in
    start)
        echo -n "Starting $DESC: $NAME"
        d_start
        echo "."
        ;;
    stop)
        echo -n "Stopping $DESC: $NAME"
        d_stop
        echo "."
        ;;
    restart)
        echo -n "Restarting $DESC: $NAME"
        d_stop
        sleep 1
        d_start
        echo "."
        ;;
    status)
        echo -n "Status $DESC: "
        d_status
        echo "."
        ;;
    *)
        echo "usage: $NAME {start|stop|restart}"
        exit 1
        ;;
esac

exit 0
_$: service jenkins start
_$: update-rc.d jenkins defaults

Plugin installation

You can download the plugins from http://updates.jenkins-ci.org/download/plugins/. It is recommended to install the plugins from Jenkins, but we will install one of them manually to show how it is done.

Example: Install the Sonar plugin

_$: wget http://updates.jenkins-ci.org/latest/sonar.hpi
_$: mv sonar.hpi /home/jenkins/.jenkins/plugins
_$: service jenkins restart

Go to http://localhost:8080/pluginManager/installed and check that it appears as installed.

Pipeline creation

Go to the main page of your Jenkins server and click on the + besides the All view. Or go to http://localhost:8080/newView. Create the new view and put the project_git process as the first process.

Example

Our example pipeline:

project_git -> project_prepare_venv -> project_test_unit
                                    -> project_test_selenium

project_git

Project name: project_git
Advanced Project Options:
    Use custom workspace
        Directory: /srv/jenkins/project-workspace
Source Code Management
    Git
        Repositories
            URL: ssh://user@server.domain.com/srv/git/project.git
            Credentials project/*****
        Branches to build: */master
Build Triggers
    Build periodically
        Schedule H 0 * * *
Post-build Actions
    Archive for Clone Workspace SCM
        Files to include in cloned workspace: */
        Criteria for build to be archived: Most Recent Completed Build
        Archive method: Gzipped tar

project_prepare_venv

Project name: project_prepare_venv
Advanced Project Options:
    Use custom workspace
        Directory: /srv/jenkins/project-workspace
Source Code Management
    None
Build Triggers
    Build after other projects are built
        Projects to watch: project_git,
        Trigger only if build is stable
Build
    Execute shell
        ...
Post-build Actions
    Archive for Clone Workspace SCM
        Files to include in cloned workspace: */
        Criteria for build to be archived: Most Recent Succesful Build
        Archive method: Gzipped tar

project_test_unit

Project name: project_test_unit
Advanced Project Options:
    Use custom workspace
        Directory: /srv/jenkins/project-workspace
Source Code Management
    None
Build Triggers
    Build after other projects are built
        Projects to watch: project_prepare_venv,
        Trigger only if build is stable
Build
    Execute shell
        ...

project_test_selenium

Project name: project_test_selenium
Advanced Project Options:
    Use custom workspace
        Directory: /srv/jenkins/project-workspace
Source Code Management
    None
Build Triggers
    Build after other projects are built
        Projects to watch: project_prepare_venv,
        Trigger only if build is stable
Build
    Execute shell
        ...

Jenkins and SonarQube

1) Configure SonarQube

Go to Manage Jenkins -> Configure System http://localhost:8080/configure

Sonar Runner    (Already installed, we just need to configure it)
    Name: sonar-runner
    SONAR_RUNER_HOME: /etc/sonar-runner
Sonar           (Already installed, we just need to configure it)
    Name: sonarqube

2) Create a Jenkins project for Sonar

Project name: project_sonar
Source Code Management
    Clone Workspace
        Parent project: project_git
Build Triggers
    Build after other projects are built
        Projects to watch: project_git,
        Trigger only if build is stable
Build
    Invoke Standalone Sonar Analysis
        Path to project properties: $WORKSPACE      (Jenkins' environment variables, but you can
                                                    also leave it empty.)

When it has finished, go to http://localhost:9000/ and see the results of the analysis.

Jenkins plugins

  • Build Pipeline Plugin
  • disk-usage plugin
  • Git plugin
  • Sonar Plugin
  • thinBackup