Saturday, January 13, 2018

CI/CD with Baby Steps (Jenkins - Build Periodically)

This is a first in a series of blog posts about CI/CD practices.

Build process before Continuous Integration had many flaws. Software delivery was slow and
Developers had to work hard in locating and fixing bugs due to run lot of stuff manually.

Continuous Integration is a development practice in which the developers are required to commit changes to the source code in a shared repository several times a day or more frequently. Every commit made in the repository is then built. the results of which will let you know if the code works or breaks. Apart from this, depending on the Continuous Integration tool, there are several other functions like deploying the build application on the test server, providing the concerned teams with the build and test results etc. If the build result shows that there is a bug in the code, then the developers only need to check that particular commit.
Now a days Companies are shifting from Nightly build to Continuous Integration.

Continuous Delivery involves the steps required to deploy to non-production environments. This
means that on top of having automated your testing(CI), you also have automated your release
process and you can deploy your releases at any point of time.

Continuous deployment is continuous delivery that extends to automatically pushing the
deployment to production once some predetermined criteria is met.

We will use Jenkin to automate these process.

Jenkins is an automation tool built for Continuous Integration purpose. Jenkins is used to build and
test your software projects continuously making it easier for developers to integrate changes to the project.
Jenkins achieves Continuous Integration with the help of plugins that allow it to integrate
with most of the development, testing and deployment tools. Its no more just a Continuous
Integration tool. It is a Continuous Integration and Continuous delivery tools.

  • You have an IDE with Git support, such as Eclipse or a standalone Git client
  • Create a java program and run it through command line.
  • Download Jenkins Generic Java Package(.war)Jenkins
  • Create a Jenkins job to run the java program.

After developing any Java Project in Eclipse or Standalone, download Jenkin and start like below.
You can run Jenkins by executing the following from the Command Prompt:

java -jar jenkins.war --httpPort=9192
During first execution you would find following lines in command prompt
Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:
This may also be found at: C:\Users\imran\.jenkins\secrets\initialAdminPassword
Access Jenkins with  http://localhost:9192 and use admin/ to login and change password
Setting up Jenkins build server with Build periodically option
I already developed sample Springboot+ApacheCamel based application, I used Maven for
dependency management and build automation.
On local system i run following commands to compile, test and run
  • To Compile   `mvn install`
  • To Run `mvn spring-boot:run`
  • Run `mvn package` to create a JAR file in the `target` folder which can be deploy on other servers.
Now we will automate this for continuous integration by using Jenkins. We will create different Jenkins Jobs to automate this build process.

Step 1 : Jenkins - New Item - add details. it will show multi Tab windows

Step 2 : In General Tab provide description of Build Job.

Step 3 : Source Code Management Tab have different option if we want to integrate with SCM like GIT,
For current option we will select none.(In coming blogs we will integrate with GIT).

Step 4 : "Build Triggers" Tab provide different options to start/trigger build process, we will select
"Build periodically" which support Cron like job schedule like after 15 min or once a day.
As of now we will start with after every 5 minutes and put "H/5 * * * *" in schedule Text Field.

Step 5 : In Build section we can provide CommandPrompt/Shell commands, as we want to generate
JAR file we will enter below commands in command textfield

cd C:\Users\workspace\GreenFarm\
mvn clean package

Step 5 : "Post Build Actions" we will execute some scripts so we need "PostBuildScript" plugin.
"PostBuildScript" plugin makes it possible to execute a set of scripts at the end of the build.
Provide path for scripts file and it will get execute.

You can put contents like
java -jar greenfarm-1.0-SNAPSHOT.jar
curl -i http://localhost:8181/orders/1

Main purpose of this blog was to get introduced jenkin to old fashioned java developer,

coming blogs will cover more advanced topics.

No comments: