Jenkins installation and configuration

Install Jenkins with Java WAR

Start Jenkins

java -jar jenkins.war

Run as background on Linux

nohup java -jar jenkins.war &

Jenkins will run at http://localhost:8080/

Ubutnu startup script for Jenkins

sudo vi /etc/init.d/jenkins

NAME=jenkins
RUN_AS=jenkins
JAVA_HOME="/usr/lib/jvm/java-8-oracle"
JAVA_OPTS="-server -Xmn1g -Xms2g -Xmx2g -Djava.net.preferIPv4Stack=true -Dfile.encoding=UTF8 -Dhudson.model.DirectoryBrowserSupport.CSP="""
JENKINS_HOME="/opt/jenkins_home"
JENKINS_WARS="/opt/jenkins_wars"
LOGFILE= $JENKINS_HOME/jenkins.log
COMMAND="nohup $JAVA_HOME/bin/java $JAVA_OPTS -jar $JENKINS_WARS/jenkins.war > $LOGFILE 2>&1 &"

. /lib/init/vars.sh
. /lib/lsb/init-functions

do_start()
{
su $RUN_AS -s /bin/bash -c "JENKINS_HOME=$JENKINS_HOME exec $COMMAND"
return 0
}

do_stop() {
# pkill -u $RUN_AS
kill -9 $(ps aux | grep java | grep jenkins.war | awk '{print $2}')
return 0
}

case $1 in
'start')
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
'stop')
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
'restart')
echo -n "Restarting $DESC: $NAME"
do_stop
sleep 1
do_start
echo "."
;;
*)
echo "usage: $NAME {start|stop|restart}"
exit 1
;;
esac

exit 0

Make jenkins service starts automatically on Ubuntu

sudo chmod 755 /etc/init.d/jenkins
sudo chown root:root /etc/init.d/jenkins
sudo update-rc.d jenkins defaults
sudo update-rc.d jenkins enable

# Start your jenkins service
sudo service jenkins start


Configure Admin account

Manage Jenkins > Configure Global Security

Check: Enable security
Check: Project-based Matrix Authorization Strategy
Create a user "admin" in: User/group to add:
Check All rules for the user
Save

Sign up a user with username "admin" above.
Log in with "admin".

Jenkins LDAP set up
- Install Jenkins LDAP Plugin by search for "ldap" in Manage Plugins. More https://plugins.jenkins.io/ldap/

Reset Jenkins password when forget admin password or LDAP master password is expired or changed

- Stop Jenkins service
- Make a backup of $JENKINS_HOME/config.xml
- Open $JENKINS_HOME/config.xml and search for section

<managerPasswordSecret>{ABCefghIKLM=}</managerPasswordSecret>

- Change 'ABCefghIKLM=' by your new plain text password, example 'Hello2World'

<managerPasswordSecret>{Hello2World}</managerPasswordSecret>

- Start Jenkins service and try to log in
- If above steps don't work, then
- Make suare to backup $JENKINS_HOME/config.xml
- Remove all of these sections

<useSecurity>true</useSecurity>

<authorizationStrategy class="hudson.security.GlobalMatrixAuthorizationStrategy">
...
...
</authorizationStrategy>

<securityRealm class="hudson.security.LDAPSecurityRealm" plugin="ldap@1.24">
...
...
</securityRealm>

- Start Jenkins service and set up LDAP again
- Make sure LDAP works and you can log in by LDAP accounts
- Stop Jenkins service
- Merge old config.xml with new config.xml
- Start Jenkins

Get TOKEN to use REST API

Get token
- Login into Jenkins
- Go to http://JENKINS_URL:8080/user/YOUR_USER_ID/configure
- Show and get the API Token

Get the build status with curl

# Ubuntu - install the jq command-line JSON processor
sudo apt-get install jq

export USER=YOUR_USER_ID
export TOKEN=YOUR_API_TOKEN
export JENKINS_URL="http://JENKINS_URL:8080/job/YOUR_JOB_NAME/lastCompletedBuild/api/json"
BUILD_STATUS=$(curl --silent --user ${USER}:${TOKEN} ${JENKINS_URL} | jq -r '.result')
echo $BUILD_STATUS

Get build time with format
Example: Use the curl to get a job info or trigger it via HTTP. Get the recently build time to check for code change: 11-Jul-2015.16:30:54

Last Successful Build:

http://localhost:8080/job/My_Job/lastSuccessfulBuild/buildTimestamp?format=dd-MMM-yyyy.HH:mm:ss

Last Stable Build:

http://localhost:8080/job/My_Job/lastStableBuild/buildTimestamp?format=dd-MMM-yyyy.HH:mm:ss

Useful plugins

anything-goes-formatter
- This plugin adds a markup formatter that's unsafe but allows more powerful HTML manipulation.

conditional-buildstep
- A buildstep wrapping any number of other buildsteps, controlling their execution based on a defined condition (e.g. BuildParameter).

Email Extension Plugin
- This plugin is a replacement for Jenkins's email publisher

Environment Injector Plugin
- This plugin makes it possible to set an environment for the builds.

Hudson Post build task
- This plugin allows to execute a batch/shell task depending on the build log output.

JUnit Plugin
- Allows JUnit-format test results to be published.

Multijob plugin
- This plugin is a MultiJob plugin.

Parameterized Trigger plugin
- This plugin lets you trigger new builds when your build has completed, with various ways of specifying parameters for the new build.

Post-Build Script Plug-in
- PostBuildScript makes it possible to execute a set of scripts at the end of the build.

Run Condition Plugin
- Core conditions to select whether to execute a build step or publisher. Used by the [Flexible Publish Plugin] and the [Conditional BuildStep Plugin].

Test Results Analyzer Plugin
- A plugin that shows history of test execution results in a tabular format.

TextFinder plugin
- This plugin is used to search for strings in workspace files. The outcome of this search can be used to mark the build as normal or failed.

Token Macro Plugin
- This plug-in adds reusable macro expansion capability for other plug-ins to use.

Ref:
- https://www.jenkins.io/doc/book/installing/#war-file

Loading