Getting Started With Jenkins

Abdul Hakeem Mahmood
5 min readDec 14, 2020

It’s a 3 parts long article.

  1. Understanding CICD.
  2. Understanding Jenkins.(you are here)
  3. Jenkins Android integration. (coming soon)

Jenkins is an open-source automation tool written in Java with rich set on plugs-in which makes using Jenkins pretty easy and fast to integrate. With Jenkins we can accelerate our software development process by automating continuous delivery process

Lets Gets Started With Jenkins

Go to https://www.jenkins.io/download/ and get the generic jenkins .war package.

Jenkins is a java project so make sure you have your JDK installed in your system.

To start Jenkins, Execute the following command in your Jenkins.war directory.

java -jar jenkins.war

By default Jenkins run on port 8080, If you want to run Jenkins in a different port than 8080 then run the following command to start Jenkins.

java -jar jenkins.war --httpPort={YOUR_PORT_NUMBER_GOES_HERE}

If you have a latest version of java and Jenkins throws you an version mismatch exception then run the following command to start Jenkins.

java -jar jenkins.war --enable-future-java

And boom!! now you have your Jenkins up and running on your local host

Running the Jenkins for the first time will prompt you a initial admin password in the console logs, you will need it to login to Jenkins for the first time.

Goal

Our goal is to clone simple hellworld java project from GitHub and execute the java class. After execution we will send an email with the build execution status.

Lets get started

Open localhost:{PORT_NUMBER}

Install all the suggested Plug-Ins

First thing first, to execute our java project we surly need JDK, lets give Jenkins path to our installed JDK.

Go to: Manage Jenkins > Configure (http://localhost:8080/configure) > Environment variables

Create a new Jenkins Job.

Enter Job name and Choose Freestyle project then Click OK.

Then we will be navigated to our job configuration screen where we will be adding configuration to our job which will mainly consist of 4 parts.

  1. Tell Jenkins where your Source Code is. (Source Code Management)
  2. Trigger Jenkins to execute some task on your Source Code.(Build Triggers)
  3. Execute tasks on your Source Code. (Build Execution)
  4. Post execution response/triggers. (Post-Build Action)

1)Jenkins Source Code Management

Jenkins source code management will clone your source code from a version control each time Jenkins job is executed.

Here we choose a simple java project to clone from GitHub.

Clone your source code

2) Jenkins Build Triggers

Jenkins build triggers is a way to tell Jenkins when do you want Jenkins to execute your Jenkins job.

Say each time a new commit on specific branch on git only then you want your Jenkins to execute.

For the sake of simplicity, I have set the periodic execution, meaning Jenkins job will execute periodically after every 15 minutes

(H/15 * * * *)

Periodic executions will start a cron job on Jenkins server.

Set a build trigger

3) Jenkins Build Execution

Now that Jenkins knows where to get the source code, Jenkins knows when to execute, Now lets tell Jenkins what to execute.

Build > Add build step > if (windows) Execute Windows batch command else Execute Shell.

Build Script

Cloned Project Path : {JENKINS_HOME} / workspace / {YOUR PROJECT}

JENKINS_HOME : Manage Jenkins > Configure System (http://localhost:{PORT_NUMBER}/configure) > Home directory

Navigate to the project directory where you would like to execute the command

cd /home/abdulhakeem/.jenkins/workspace/JavaJenkinsExample/srcjavac JavaJenkins.java
java JavaJenkins

4) Jenkins Post-Build Action

By this step Jenkins have our source code, Jenkins knows when to execute and Jenkins knows what to execute. Now we have to do some post build action.

Send an email or Send a Slack notification or upload your artifacts.

Lets send an successful build notification.

First setup email configuration from :

Manage Jenkins > Configure System > Extended E-mail Notification and add the following settings.

SMTP Server : smpt.gmail.com

SMPT PORT : 464

SMPT USERNAME : username@gmail.com

SMPT PASSWORD : *****

USE SSL : CHECKED

Add Post Build Action to your project : Extended Notification

Add your desired email addresses to Project Recipient List and leave rest of the settings as it is and scroll down where you see advance settings. Add Triggers from advance settings and choose Always and Send to Recipient List

Recipient List
Email Triggers

Now we are all done. Hit save and our Jenkins job will execute after 15 minutes each time until Jenkins is running.

If everything goes well, you should see a success message on your console log

Build Logs

To view your build logs, Go to your project and find build history on the bottom left of the screen.

Jenkins mark blue dots to the successful build and if anything goes wrong during build process and your job execution fails for what so ever reason, Jenkins will mark it as red dot for indication.

Go to the build history and click on any job# and click on console to view the logs for each specific Jenkins Job.

Console
Successful build email notification

Whats next?

Understanding basic on how Jenkins work is a very good start. There is a vast amount of task we can automate using Jenkins, imagination is the limit.

Next, we can integrate Jenkins on our actual project and automate the build process of within our sprint cycle. Processes are exactly the same which we have covered already in this article which is Clone -> Trigger -> Execute -> Post-Execution-Actions

--

--