Jenkins Server

Jenkins is an open source automation server. Jenkins manages and controls development lifecycle processes of all kinds, including build, document, test, package, stage, deployment, static analysis and many more.

At its core, Jenkins does two things: automated integration and external build monitoring. This means that it can greatly simplify the process of keeping your code maintainable and keep a close and untiring eye on the quality of your builds, ensuring you don’t end up with nasty surprises when a few of your developers merge their code before it’s ready.

Maintaining any project, especially one developed by several team members concurrently and one that might incorporate many functions, components, languages, and environments, is a struggle at the best of times. Fundamentally, Jenkins provide a solution for continuous integration — i.e. is a development practice that requires developers to integrate code into a shared repository several times a day. Each check-in is then verified by an automated build, allowing teams to detect problems early.

Installing Jenkins on CentOS

Step 1: Before starting the installation, please install Java, using the following commands

yum install java-1.8.0-openjdk-devel –y

Step 2: Create Jenkins user and group

groupadd jenkins useradd –g jenkins jenkins

Step 3: Create JENKINS_HOME and download the latest version of the jenkins.war

mkdir -p /opt/jenkins
mkdir -p /opt/jenkins/logs
wget http://mirrors.jenkins-ci.org/war/latest/jenkins.war -O /opt/jenkins/jenkins.war

Step 4: Create the scripts to start and stop the Jenkins processes (/opt/jenkins/start-jenkins.sh and /opt/jenkins/stop-jenkins.sh respectively). Both of these files should be in the JENKINS_HOME.

vim /opt/jenkins/start-jenkins.sh

#!/bin/bash

# import sysconfig settings and set defaults
[ -f /etc/sysconfig/jenkins ] && . /etc/sysconfig/jenkins
[ "${JENKINS_HOME}" == "" ] &&
    JENKINS_HOME=/opt/jenkins
[ "${JENKINS_LOG}" == "" ] &&
    JENKINS_LOG=/opt/jenkins/jenkins.log
[ "${JENKINS_JAVA}" == "" ] &&
    JENKINS_JAVA=/usr/bin/java
[ "${JENKINS_JAVAOPTS}" == "" ] &&
    JENKINS_JAVAOPTS=""
[ "${JENKINS_IP}" == "" ] &&
    JENKINS_IP=0.0.0.0
[ "${JENKINS_PORT}" == "" ] &&
    JENKINS_PORT=8080
[ "${JENKINS_ARGS}" == "" ] &&
    JENKINS_ARGS=""

JENKINS_WAR=${JENKINS_HOME}/jenkins.war

# check for config errors
JENKINS_ERRORS=()
[ ! -f ${JENKINS_WAR} ] &&
    JENKINS_ERRORS[${#JENKINS_ERRORS[*]}]="JENKINS_HOME : The jenkins.war
could not be found at ${JENKINS_HOME}/jenkins.war"
[ ! -f $JENKINS_JAVA ] &&
    JENKINS_ERRORS[${#JENKINS_ERRORS[*]}]="JENKINS_JAVA : The java
executable could not be found at $JENKINS_JAVA"

# display errors if there are any, otherwise start the process
if [ ${#JENKINS_ERRORS[*]} != '0' ]
then
    echo "CONFIGURATION ERROR:"
    echo "    The following errors occurred when starting Jenkins."
    echo "    Please set the appropriate values at /etc/sysconfig/jenkins"
    echo ""
    for (( i=0; i<${#JENKINS_ERRORS[*]}; i++ ))
    do
        echo "${JENKINS_ERRORS[${i}]}"
    done
    echo ""
    exit 1
else
    nohup nice $JENKINS_JAVA $JENKINS_JAVAOPTS -jar $JENKINS_WAR
--httpListenAddress=$JENKINS_IP --httpPort=$JENKINS_PORT $> $JENKINS_LOG
2>&1 &
fi

vim /opt/jenkins/stop-jenkins.sh

#!/bin/bash
kill -9 `ps -ef | grep [j]enkins.war | awk '{ print $2 }'` > /dev/null

Step 5: Create a script called /etc/init.d/jenkins to allow the server to be started and stopped as a service.

vim /etc/init.d/jenkins

#! /bin/bash
# chkconfig: 2345 90 10
# description: Jenkins Continuous Integration server
# processname: /opt/jenkins/jenkins.war

# Source function library.
. /etc/rc.d/init.d/functions

# Get network sysconfig.
. /etc/sysconfig/network

# Check that networking is up, otherwise we can't start
[ "${NETWORKING}" = "no" ] && exit 0

# Get the Jenkins sysconfig
[ -f /etc/sysconfig/jenkins ] && . /etc/sysconfig/jenkins
[ "${JENKINS_HOME}" = "" ] &&
    JENKINS_HOME=/opt/jenkins
[ "${JENKINS_USER}" == "" ] &&
    JENKINS_USER=jenkins

startup=${JENKINS_HOME}/start-jenkins.sh
shutdown=${JENKINS_HOME}/stop-jenkins.sh
export JAVA_HOME=/usr/lib/jvm/jre-1.8.0-openjdk.x86_64

start(){
    action $"Starting Jenkins service: "
    ps -ef | grep [j]enkins.war > /dev/null
    status=$(echo $?)
    if [ $status -eq 0 ]; then
        echo "Jenkins is already running"
    else
        su - $JENKINS_USER -c $startup
        RETVAL=$?
        [ $RETVAL == 0 ] &&
            echo "Jenkins was started successfully." ||
            echo "There was an error starting Jenkins."
    fi
}

stop(){
    action $"Stopping Jenkins service: "
    ps -ef | grep [j]enkins.war > /dev/null
    status=$(echo $?)
    if [ $status -eq 1 ]; then
        echo "Jenkins is not running"
    else
        su - $JENKINS_USER -c $shutdown
        RETVAL=$?
        [ $RETVAL == 0 ] &&
            echo "Jenkins was stopped successfully." ||
            echo "There was an error stopping Jenkins."
    fi
 }

status(){
    ps -ef | grep [j]enkins.war > /dev/null
    status=$(echo $?)
    #if [ $pid -eq 2 ]; then
    if [ $status -eq 0 ]; then
        echo "Jenkins is running..."
    else
        echo "Jenkins is stopped..."
    fi
}

restart(){
    stop
    sleep 5
    start
}

# Call functions as determined by args.
case "$1" in
start)
    start;;
stop)
    stop;;
status)
    status;;
restart)
    restart;;
*)
    echo $"Usage: $0 {start|stop|status|restart}"
    exit 1
esac

exit 0

Step 6: Make sure that the JENKINS_USER is the owner of the JENKINS_HOME directory and scripts, and that the scripts have executable flags set.

chown -R jenkins:jenkins /opt/jenkins
chmod +x /opt/jenkins/start-jenkins.sh
chmod +x /opt/jenkins/stop-jenkins.sh
chmod +x /etc/init.d/jenkins

Step 7: You can then start, stop, restart, and check the status of the service via the following commands, or control if it is launched on boot using chkconfig:

service jenkins status
service jenkins start
service jenkins restart
service jenkins stop
chkconfig jenkins on

Step 8: The additional variables that can be set at /etc/sysconfig/jenkins and their defaults.

vim /etc/sysconfig/jenkins

# Jenkins system configuration
JENKINS_HOME=/opt/jenkins
JENKINS_USER=jenkins
JENKINS_LOG=/opt/jenkins/logs/jenkins.log
JENKINS_JAVA=/usr/bin/java
JENKINS_JAVAOPTS=""
JENKINS_IP=0.0.0.0
JENKINS_PORT=8080
JENKINS_ARGS=""

Step 9: Update the jenkins plugin update center json file to latest.

curl -L https://updates.jenkins-ci.org/update-center.json | sed '1d;$d' > /home/jenkins/.jenkins/updates/default.json
chown jenkins:jenkins /home/jenkins/.jenkins/updates/default.json

Step 10: Download Jenkins-cli jar file for CLI operations.

http://jenkins.local.in:8080/jnlpJars/jenkins-cli.jar -O /opt/jenkins/jenkins-cli.jar

Step 11: Enter following command which saves the current credential to allow future commands to run without explicit credential information.

java -jar jenkins-cli.jar -s http://jenkins.local.in:8080 login --username=admin --password=$(cat /home/jenkins/.jenkins/secrets/initialAdminPassword)

Step 12: Create Jenkins login user

echo 'hpsr=new hudson.security.HudsonPrivateSecurityRealm(false); hpsr.createAccount("jenkins", "jenkins@123")' | java -jar jenkins-cli.jar -s http://jenkins.local.in:8080 groovy =

Step 13: Access Jenkins master using following URL,
<IP-address>:8080

jenkins-login

Tags: , , , , , ,

Trackbacks/Pingbacks

  1. Configure Jenkins Slave and Connect to Master | Izel Technologies - July 26, 2016

    […] For understanding and installing refer previous blog of Jenkins Server. […]

  2. Install plugins on Jenkins | Izel Technologies - July 26, 2016

    […] understanding and installing jenkins server refer previous blog Jenkins Server and for connecting slave to the master node refer Configure Jenkins Slave and Connect to […]

Leave a Reply