3. DevOps – Jenkins[2.9]: How to create and build the job ?

jenkins

In continuation of my previous blog on Jenkins 2.9 installation [https://vskumar.blog/2017/11/25/1-devops-jenkins2-9-installation-with-java-9-on-windows-10], We have installed and retested it. In this blog we can see a simple job creation and running it in a build.

Now, Let us do some exercise:

Let us create a new job using Jenkins.

As you are aware of; with Jenkins any kind of manual tasks can be done.

For example:

  1. I want to compile a java program and run it.
  2. To do this 1st let us identify the manual steps.
  3. I need to go to the below directory: D:\JavaSamples\Javatest , where my java programs are available.
  4. I need to use the below command:

CD [to know the current directory.]

Assume, I am in the below dir:

D:\Jenkins\jenkins-2.90>cd

D:\Jenkins\jenkins-2.90

Then , I need to use cd \JavaSamples\Javatest

Now I need to check my current path using cd

Then see the hello*.java using

dir command.

Then I need to compile it using ‘javac HellowWorld.java’ command.

Then I need to check the files on hello*.* name

There should be one file as ‘HellowWorld.class’

It means my program has been compiled correctly without errors.

Now, I need to run the program using java command.

Now, I need to run this file as java HellowWorld

It should display the output.

I have executed all the steps in a command window to compile and test the program.

You can see the executed commands and the  output also from the below screen:

HelloWorld-compile&execute-CMD

As we know these are the manual tasks we need to do repetetively to select a program, compile it and run it. Why don’t we use Jenkins to create a job which has these set of tasks.

Now, let us learn how to create a job in Jenkins?:

Now assuming you are on the below screen:

Jenkins-New Job Creation

Click on “create new jobs”

You will get the below screen:

Jenkins job creation-enter an item name scrn.png

I want to create a job with the name of “vskumar2017test1”.

Jenkins-Freestyle project-creation

I want to create a Freestyle project, as I do not work with any of the plugins for now.
Hence I need to select on the 1st option “ Freestyle project”. When I click on “OK” button, we can see the below screen:

Jenkins-Freestyle project-creation2

I have entered the project description as below, as per our activity plan:

Jenkins-Freestyle project-description

As you are aware we are using Jenkins for simple task in this exercise,

Now click on build options. You will get the below screen:

Jenkins-Freestyle project-Build.png

In this screen we need to use build option. So click on add build steps. Using this option, we will get the below features to use the command window and the commands:

Jenkins-Freestyle project-Build-Add build step

There are two different options we can see to use the commands execution. 1. Execute Windows batch command , 2. Execute shell. Currently e are working with windows only. Hence 1st option need to be selected.

You can see the below screen:

Jenkins-New Job-Build-window commands1

Now, whatever commands we tried using command prompt we need to enter those.

For example I used as below:

cd

cd \JavaSamples\Javatest

dir hellow*.java

javac HellowWorld.java

dir HellowWorld.*

java HellowWorld

Now, I am copying these commands into the window. You can see the partially displayed commands in the window. In reality it has all the commands.

Jenkins-Build-Windows-batch commands-entry1

Now, let us save this job.
We will get the below screen on the created job name:

Jenkins-Project-vskumar2017test1.png

How to run the created project ?:

We need to run the created project using the option “Build now”.

You can see the build history as the job running, Under build#1.

Now, How to see the executed job output ?:
To see the output we need to click on the down arrow mark ate the job#.
It displays the below:

We need to select the console output, It displays the output as below:

Jenkins-Project-vskumar2017test1-running-build1-consoleOuputIf we scroll down we can see the job status message:

Jenkins-Project-vskumar2017test1-running-build1-consoleOuput

It shows all the output for our commands along with the job status.

Now let us review and analyze the display messages and commands as below.

Started by user Vskumar2017
Building in workspace D:\Jenkins\Jenkins 2.9\workspace\vskumar2017test1
[vskumar2017test1] $ cmd /c call C:\WINDOWS\TEMP\jenkins5234154176626743505.bat

The above commands shows, Jenkins started the job with user id vskumar2017.N

And it displays the current path of the job where it is created.

And it invokes a .bat file to execute the command prompt commands what we entered. It means it stored the batch commands into a file and it is opened by a cmd command from a shell prompt.

Now, let us see the below commands:

D:\Jenkins\Jenkins 2.9\workspace\vskumar2017test1>cd
D:\Jenkins\Jenkins 2.9\workspace\vskumar2017test1

It denotes its current job/project directory. And executed the cd to show the path.

Through the below:

D:\Jenkins\Jenkins 2.9\workspace\vskumar2017test1>cd  \JavaSamples\Javatest 

D:\JavaSamples\Javatest>dir hellow*.java 
 Volume in drive D is New Volume
 Volume Serial Number is 5C67-6F04

 Directory of D:\JavaSamples\Javatest

04/16/2017  03:52 PM               234 HellowWorld.java
04/16/2017  03:53 PM               570 HellowWorld10.java
               2 File(s)            804 bytes
               0 Dir(s)  22,762,598,400 bytes free

It changed the directory where the java program is there.

And displayed the files.

Let us see the next output:

D:\JavaSamples\Javatest>javac HellowWorld.java 
'javac' is not recognized as an internal or external command,
operable program or batch file.

It shows error for java path. Jenkins is not recognizing the path. The javac [compiler application is in the below path: D:\Java\jdk-9.0.1\bin ]

Javac-path.png

Now, this need to be corrected in the project.
To correct this we need to goto option: “Configure”.

Open it into project window to update some more commands.

I have updated the command window with the below commands:
cd \JavaSamples\Javatest
dir hellow*.java
del HellowWorld.class
dir hellow*.java
D:\Java\jdk-9.0.1\bin\javac HellowWorld.java
dir HellowWorld.*
java HellowWorld

Now, let me run the job by using “Build now option”.

For debugging purpose, I have executed this job some more times. Hence history shows multiple builds on it.

Our current build is #5. And let us open it and see the console output:

Jenkins-Project-vskumar2017test1-buildsNow#5

The console output shows as below:

Jenkins-Project-vskumar2017test1-console#5

Now you can see the whole console output as below in text format:

Console Output
Started by user Vskumar2017
Building in workspace D:\Jenkins\Jenkins 2.9\workspace\vskumar2017test1
[vskumar2017test1] $ cmd /c call C:\WINDOWS\TEMP\jenkins1398066858541735603.bat

D:\Jenkins\Jenkins 2.9\workspace\vskumar2017test1>cd
D:\Jenkins\Jenkins 2.9\workspace\vskumar2017test1

D:\Jenkins\Jenkins 2.9\workspace\vskumar2017test1>cd \JavaSamples\Javatest

D:\JavaSamples\Javatest>dir hellow*.java
Volume in drive D is New Volume
Volume Serial Number is 5C67-6F04

Directory of D:\JavaSamples\Javatest

04/16/2017 03:52 PM 234 HellowWorld.java
04/16/2017 03:53 PM 570 HellowWorld10.java
2 File(s) 804 bytes
0 Dir(s) 22,761,955,328 bytes free

D:\JavaSamples\Javatest>del HellowWorld.class

D:\JavaSamples\Javatest>dir hellow*.java
Volume in drive D is New Volume
Volume Serial Number is 5C67-6F04

Directory of D:\JavaSamples\Javatest

04/16/2017 03:52 PM 234 HellowWorld.java
04/16/2017 03:53 PM 570 HellowWorld10.java
2 File(s) 804 bytes
0 Dir(s) 22,761,955,328 bytes free

D:\JavaSamples\Javatest>D:\Java\jdk-9.0.1\bin\javac HellowWorld.java

D:\JavaSamples\Javatest>dir HellowWorld.*
Volume in drive D is New Volume
Volume Serial Number is 5C67-6F04

Directory of D:\JavaSamples\Javatest

11/17/2017 12:3

9 PM 427 HellowWorld.class
04/16/2017 03:52 PM 234 HellowWorld.java
2 File(s) 661 bytes
0 Dir(s) 22,761,955,328 bytes free

D:\JavaSamples\Javatest>java HellowWorld
Hello World

D:\JavaSamples\Javatest>exit 0
Finished: SUCCESS

=======================>
Observe there was no error displayed as we have given the correct javac application path.

Now I have updated the commands as below to remove the existing HellowWorld.class file.
cd
cd \JavaSamples\Javatest
dir hellow*.*
echo ‘Assuming .class file is already there..”
del HellowWorld.class
dir hellow*.java
D:\Java\jdk-9.0.1\bin\javac HellowWorld.java
dir HellowWorld.*
java HellowWorld
=======================>
You can see the output under build#7:
========================>

Console Output
Started by user Vskumar2017
Building in workspace D:\Jenkins\Jenkins 2.9\workspace\vskumar2017test1
[vskumar2017test1] $ cmd /c call C:\WINDOWS\TEMP\jenkins7100481173282587024.bat

D:\Jenkins\Jenkins 2.9\workspace\vskumar2017test1>cd
D:\Jenkins\Jenkins 2.9\workspace\vskumar2017test1

D:\Jenkins\Jenkins 2.9\workspace\vskumar2017test1>cd \JavaSamples\Javatest

D:\JavaSamples\Javatest>dir hellow*.*
Volume in drive D is New Volume
Volume Serial Number is 5C67-6F04

Directory of D:\JavaSamples\Javatest

11/17/2017 12:45 PM 427 HellowWorld.class
04/16/2017 03:52 PM 234 HellowWorld.java
04/16/2017 03:53 PM 570 HellowWorld10.java
3 File(s) 1,231 bytes
0 Dir(s) 22,760,976,384 bytes free

D:\JavaSamples\Javatest>echo ‘Assuming .class file is already there..”
‘Assuming .class file is already there..”

D:\JavaSamples\Javatest>del HellowWorld.class

D:\JavaSamples\Javatest>dir hellow*.java
Volume in drive D is New Volume
Volume Serial Number is 5C67-6F04

Directory of D:\JavaSamples\Javatest

04/16/2017 03:52 PM 234 HellowWorld.java
04/16/2017 03:53 PM 570 HellowWorld10.java
2 File(s) 804 bytes
0 Dir(s) 22,760,976,384 bytes free

D:\JavaSamples\Javatest>D:\Java\jdk-9.0.1\bin\javac HellowWorld.java

D:\JavaSamples\Javatest>dir HellowWorld.*
Volume in drive D is New Volume
Volume Serial Number is 5C67-6F04

Directory of D:\JavaSamples\Javatest

11/17/2017 12:47 PM 427 HellowWorld.class
04/16/2017 03:52 PM 234 HellowWorld.java
2 File(s) 661 bytes
0 Dir(s) 22,760,976,384 bytes free

D:\JavaSamples\Javatest>java HellowWorld
Hello World

D:\JavaSamples\Javatest>exit 0
Finished: SUCCESS
=================================>
Now, you can see message display from echo command.
And the old class file is removed and the new file time stamp can be seen differently.

Now, how to make failure a job ?:

Please see the screen display with a failed job:

Jenkins-vskumar2017-success1

Now, let us see the console output:

Console-output-vskumar2017-3.png

In this example I have given a wrong file name to execute. Hence it is failed.
It checks the last commands results. You can change them and cross check as an exercise.
The failure error flag also it shows as “1”.
When the job was success the flag showed as “0”.

Hope you understand the difference between failure and success of Jenkins build.
So, we need to make sure the commands/script mentioned in the command window should be a debugged one. Then the jobs success can be seen.

Please note;
If it is not a recognized command as an internal or external command,
operable program or batch file. Jenkins will not count it as a failure.

Exercise:
Take a new Java program and create a job to compile and run it.

How to use My views:
You can see the build history in graphical format as below with My views option:

Jenkins-Job-Views

 

At this point with this blog I want to close now, with the above scenarios.

Feel free to contact for any support:

Vcard-Shanthi Kumar V-v3

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s