SSH RaspberryPi and activate Jenkins server

Need to have your Jenkins server in RaspberryPi? here it is…

  • Install Raspbian OS and log into the device
  • Open Preferences > Raspberry Pi Configuration > Interfacesand enable SSH
  • Let us SSH into this RaspberryPi device from another machine
ssh pi@raspberrypi.local
  • The same can be achieved by giving the IP address instead of raspberrypi.local
ssh pi@192.168.0.105
  • Now, enter the default password, which is,
raspberry
  • Type passwd to change the current password

 

Jenkins Installation

  • Download Jenkins key and binary; it usually downloads the latest versions
wget -q -O - https://jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -

sudo sh -c 'echo deb http://pkg.jenkins-ci.org/debian binary/ > /etc/apt/sources.list.d/jenkins.list'
  • Install Jenkins
sudo apt-get update

sudo apt-get install jenkins -y
  • Set Jenkins server to start automatically during machine boot
systemctl status jenkins.service
  • Open one of the following links to see the Jenkins interface

http://raspberrypi.local:8080/

http://192.168.0.105:8080/

  • Copy and paste the Jenkins default password
sudo cat /var/lib/jenkins/secrets/initialAdminPassword

 

Java Installation

  • The latest Jenkins versions fail to support Java 1.8 which comes by default on Raspbian OS.
  • Let’s see the cmd lines to uninstall JDK 1.8 in RaspberryPi OS
sudo update-alternatives --display java

sudo update-alternatives --remove "java" "/usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/jre/bin/java"
sudo update-alternatives --remove "javac" "/usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/jre/bin/javac"
sudo update-alternatives --remove "javas" "/usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/jre/bin/javas"

sudo rm -rf jdk-8-oracle-arm32-vfp-hflt/

sudo update-alternatives --config java
sudo update-alternatives --config javac
sudo update-alternatives --config javas
  • Now, let us install Java 11 which is a compatible version for Jenkins 2.0+ versions
wget https://github.com/bell-sw/Liberica/releases/download/11.0.2/bellsoft-jdk11.0.2-linux-arm32-vfp-hflt.deb

sudo apt-get install ./bellsoft-jdk11.0.2-linux-arm32-vfp-hflt.deb

sudo update-alternatives --config javac
sudo update-alternatives --config java

List of available Jenkins Environment variables

Listed below is a complete set of Jenkins environment variables:

Variable Description
BRANCH_NAME For a multibranch project, this will be set to the name of the branch being built, for example in case you wish to deploy to production from master but not from feature branches; if corresponding to some kind of change request, the name is generally arbitrary (refer to CHANGE_ID and CHANGE_TARGET).
CHANGE_ID For a multibranch project corresponding to some kind of change request, this will be set to the change ID, such as a pull request number, if supported; else unset
CHANGE_URL For a multibranch project corresponding to some kind of change request, this will be set to the change URL, if supported; else unset
CHANGE_TITLE For a multibranch project corresponding to some kind of change request, this will be set to the title of the change, if supported; else unset
CHANGE_AUTHOR For a multibranch project corresponding to some kind of change request, this will be set to the username of the author of the proposed change, if supported; else unset
CHANGE_AUTHOR_DISPLAY_NAME For a multibranch project corresponding to some kind of change request, this will be set to the human name of the author, if supported; else unset
CHANGE_AUTHOR_EMAIL For a multibranch project corresponding to some kind of change request, this will be set to the email address of the author, if supported; else unset
CHANGE_TARGET For a multibranch project corresponding to some kind of change request, this will be set to the target or base branch to which the change could be merged, if supported; else unset
BUILD_NUMBER The current build number, such as “153”
BUILD_ID The current build ID, identical to BUILD_NUMBER for builds created in 1.597+, but a YYYY-MM-DD_hh-mm-ss timestamp for older 
BUILD_DISPLAY_NAME The display name of the current build, which is something like “#153” by default
JOB_NAME Name of the project of this build, such as “foo” or “foo/bar”
JOB_BASE_NAME Short Name of the project of this build stripping off folder paths, such as “foo” for “bar/foo”
BUILD_TAG String of “jenkins-${JOB_NAME}${BUILD_NUMBER}” All forward slashes (“/”) in the JOB_NAME are replaced with dashes (“-“). Convenient to put into a resource file, a jar file, etc for easier identification
EXECUTOR_NUMBER The unique number that identifies the current executor (among executors of the same machine) that’s carrying out this build. This is the number you see in the “build executor status”, except that the number starts from 0, not 1
NODE_NAME Name of the agent if the build is on an agent, or “master” if run on master
NODE_LABELS Whitespace-separated list of labels that the node is assigned
WORKSPACE The absolute path of the directory assigned to the build as a workspace
JENKINS_HOME The absolute path of the directory assigned on the master node for Jenkins to store data
JENKINS_URL Full URL of Jenkins, like http://server:port/jenkins/ (note: only available if Jenkins URL set in system configuration)
BUILD_URL Full URL of this build, like http://server:port/jenkins/job/foo/15/ (Jenkins URL must be set)
JOB_URL Full URL of this job, like http://server:port/jenkins/job/foo/ (Jenkins URL must be set)
GIT_COMMIT The commit hash being checked out
GIT_PREVIOUS_COMMIT The hash of the commit last built on this branch, if any.
GIT_PREVIOUS_SUCCESSFUL_COMMIT The hash of the commit last successfully built on this branch, if any
GIT_BRANCH The remote branch name, if any
GIT_LOCAL_BRANCH The local branch name being checked out, if applicable
GIT_URL The remote URL. If there are multiple, will be GIT_URL_1GIT_URL_2, etc
GIT_COMMITTER_NAME The configured Git committer name, if any
GIT_AUTHOR_NAME The configured Git author name, if any
GIT_COMMITTER_EMAIL The configured Git committer email, if any
GIT_AUTHOR_EMAIL The configured Git author email, if any
SVN_REVISION Subversion revision number that’s currently checked out to the workspace, such as “12345”
SVN_URL Subversion URL that’s currently checked out to the workspace

 

Stop/kill ‘docker run’ on aborting Jenkins job

A small post that helps you to stop & remove docker containers on Jenkins job abortion.

  • Shell snippet that run in Jenkins Build > Execute shell
# function to trigger on abort condition
getAbort()
{
 docker rm $(docker stop $(docker ps -aq --filter="name=Test$BUILD_ID"))
}

# declare on abort condition
trap 'getAbort; exit' SIGHUP SIGINT SIGTERM

# docker pull and run
docker pull httpd
docker run --name Test$BUILD_ID httpd

Block current Jenkins Job until the other job is completed

  • Install Build Blocker Jenkins plugin

Build Blocker plugin

  • Go to Manage Jenkins > Manage Plugins and install Build Blocker plugin

  • Open a Jenkins job, click configure , and observe the below block

  • You can add multiple jobs as below; string prefix or suffix is allowed too

  • Simple Jenkins Job DSL for Build Blocker
blockOn("Job_name_to_wait")
  • Jenkins DSL for Build Blocker with options (single job)
blockOn("Job_name_to_wait", {
blockLevel("GLOBAL")
scanQueueFor("DISABLED")
})
  • Jenkins DSL for Build Blocker with multiple jobs
blockOn(['job_name_1', 'job_name_2']) {
blockLevel('GLOBAL')
scanQueueFor('ALL')
}

Auto-trigger Jenkins job on GitHub commit

  • Go to Jenkins job > Build Triggers, select ​Github hook trigger for GITScm polling and save Job (make sure the github plugin is installed)

  • Similar action can be performed using the following Jenkins Job-DSL snippet
triggers {
  githubPush()
}
  • Go to GitHub project Repository > Settings > Webhooks
  • Click Add webhook and provide the WebHook URL
  • The below payload URL is something the Jenkins uses to receive request from remote GitHub repository whenever there is a commit (push) is made

  • If your Jenkins server has a public IP, use it. For local and testing purpose, use ngrok tool or just port forward your local machine IP
  • ngrok exposes local servers behind NATs and firewalls to the public internet over secure tunnels
  • Download ngrok from the below link
https://ngrok.com/download
  • Unzip and open terminal to run the given cmd
./ngrok http 8080
  • Copy the pseudo url generated for 127.0.0.1

  • Copy & Paste it as a payload url in GitHub Webhooks to post APIs once the git push action is applied

  • Go to Jenkins > Manage Jenkins > Configure System
  • You will see GitHub section if the github plugin is installed
  • Click on Advanced options

  • Copy & Paste the same hook url for github configuration

  • Observe the client when a push is made in the github (GitHub sends API calls through the given payload url)

  • Eventually the Jenkins recognizes the commit made in the remote and triggers respective Jenkins job

Dockerize and integrate SonarQube with Jenkins

This post needs a basic knowledge from the previous post. Let’s see how to make SonarQube integration with Jenkins for code quality analysis in a live docker container

Dockerize SonarQube

  • Create a docker-compose.yml file with sonarqube and postgres latest images
  • Make sure you’ve sonar and sonar-scanner libs pre-installed in your local machine
  • Set login username and password as admin while executing the runner
sonar-scanner -Dsonar.projectKey=project_key -Dsonar.sources=. -Dsonar.host.url=http://localhost:9000 -Dsonar.login=admin -Dsonar.password=admin

Jenkins Integration

  • Install SonarQube Scanner Jenkins plugin

  • Go to Manage Jenkins > Configure System and update the SonarQube servers section

  • Go to Manage Jenkins > Global Tool Configuration and update SonarQube Scanner as in the below image

  • Now, create a jenkins job and setup SCM (say, git)
  • Choose Build > Execute SonarQube Scanner from job configure

  • Now, provide the required sonar properties in Analysis properties field. [Mention the path to test source directories in the following key, sonar.sources]

  • These sonar properties can be also be served from a file inside the project, named sonar-project.properties (see github for more details)
  • Now, update Path to project properties field in the project execute build

  • Observe the results in docker container’s host url

Jenkins environment variable setup

Manual Feed

# set custom job name
jobName="your_job_name"
(note: by default, you can use $JOB_NAME which doesn't require any env setup)

# create a shell file and add the below statement
echo 'export VN_NAME="$jobName"' > ~/load_env.sh

# set user privilege
chmod 750 ~/load_env.sh

# temp env variable call
source ~/load_env.sh
echo $VN_NAME

Jenkins Plugin to inject ENV variable

  • By default you will have the Jenkins plugin EnvInject API Plugin
  • Go to Job > Build Environment  and select Inject environment variables to the build process
  • Set variable key and value as in below

Default Jenkins Configure

The environment variables can also be set through Global properties

  • Go to Manage Jenkins > Configure System > Global properties
  • Add the variable key and value