NM_DevOps
NM_DevOps
0
Assignments for DevOps Continous Integration and Continous Delivery
CONTENTS
CONTENTS .................................................................................................................................................... 1
Assignments for DevOps Continuous Integration and Continuous Delivery .................................................... 2
Context ............................................................................................................................................................ 2
Exercise 1: Testing lab set up.......................................................................................................................... 3
Exercise 2: Git operations .............................................................................................................................. 3
Exercise 3: Creating a project in SonarQube ................................................................................................. 12
Exercise 4: Using Sonarqube with Sonar-Runner ..........................................................................................13
Exercise 5: Creating a local repository in Artifactory .................................................................................... 16
Build Automation: Maven ............................................................................................................................. 17
Exercise 6: Build automation using Maven....................................................................................................17
Continuous Integration: Jenkins .................................................................................................................... 21
Exercise 7: Jenkins Installation & System Configuration ............................................................................. 21
Exercise 8: Download the plugins in Jenkins ................................................................................................. 23
Exercise 9: Creating Central CI pipeline ....................................................................................................... 23
Exercise 10: Copying and Moving Jobs ........................................................................................................ 40
Exercise 11: Creating pipeline view in Jenkins .............................................................................................. 41
Exercise 12: Configuring Gating Conditions .................................................................................................43
Additional Exercises ..................................................................................................................................... 47
1. Adding custom rules to SonarQube ........................................................................................................ 47
2. Static program analysis using SonarLint ................................................................................................ 48
3. Binding SonarQube rules to SonarLint ................................................................................................. 52
1
Assignments for DevOps Continous Integration and Continous Delivery
2
Assignments for DevOps Continous Integration and Continous Delivery
Step 2: Start all the installed tools by executing the appropriate batch file.
Step 3: you can ensure all are working by accessing tools user interface with below
mentioned URL’s and credentials
Summary: You have tested lab set up for the forthcoming exercises for Jenkins.
Objective: Perform the basic operations on Git repositories using EGit (Eclipse
plugin)
3
Assignments for DevOps Continous Integration and Continous Delivery
4
Assignments for DevOps Continous Integration and Continous Delivery
5
Assignments for DevOps Continous Integration and Continous Delivery
6
Assignments for DevOps Continous Integration and Continous Delivery
->Click Next -> click Finish->you can observe project explorer view with project cloned as shown below->.
The name of the project is JNTU_Calc_Application in your case.
Step 1: Expand the project cicd-calculator-> go to src\main\webapp\index.jsp ->make some changes to the
code and save the changes->you can observe “>” symbol on project and edited source file as shown below.
7
Assignments for DevOps Continous Integration and Continous Delivery
Step2: Right click on project->team->commit->drag the files from Unstaged Changes to Staged Changes-
>Enter appropriate comments for the commit as shown below->
Note: similar way, we can commit multiple individual changes to local repository
using commit option.
Push Changes from Local Git Repository (associated with Eclipse) to Remote
Git Repository
8
Assignments for DevOps Continous Integration and Continous Delivery
Step 1: Right click on project->team-> click on Push to upstream -> you can see changes successfully
pushed from local repository to central repository as shown below.
Note: Your repository name and number may be different, this is an illustration.
Note:if push results to “non fast forward” rejection, perform below mentioned operations in sequence.(This
is dueto the fact that there has been more changes made possibly by other developers to the central Git
repository and hence those changes need to be merged before committing the changes)
Fetch from upstream->merge with local branch ->resolve if there are any merge conflicts->commit-> push.
9
Assignments for DevOps Continous Integration and Continous Delivery
Pull Changes from Remote Git Repository to Local Git Repository (associated
with Eclipse)
Assumption: A team member has committed three changes to remote repository.
Note: Pull perform two actions in sequence, Fetch and Merge. So if there are any
merge conflicts after pull operation, you have to resolve and commit again.
10
Assignments for DevOps Continous Integration and Continous Delivery
Pull the project from remote Git repository to local Eclipse work space using EGit.
Practice the following commands-
a. Commit and push operations
b. Fetch operation
c. Merge operation with and without conflicts. When there is a conflict, resolve
the conflict and push the code back to the branch provided.
Note: Perform commit, and push operations on remote Git repository by making
some changes to source code. You can use the repository provided in the earlier
demo for completing this exercise.
Step 2: The parent branch will by default be that branch that is currently open in
Eclipse, you can also change it by clicking on the Select option.
Step 3: Name the New Branch and check on the Check out as new branch if you
would like to switch to the new branch as well. Click on Finish.
11
Assignments for DevOps Continous Integration and Continous Delivery
Summary: You have learnt to do basic operations with Git related to version control in this exercise.
Exercise 3: Creating a project in SonarQube
Objective: Understand creation of project in Sonarqube
Step 1: Go to SonarQube URL and login with credentials: admin: password
Step 2: Click on the manually option on the SonarQube dashboard as shown in the
screenshot given below.
Step 3: Enter the project display name and project key as calculator as shown in the
screenshot given below and click Set Up.
12
Assignments for DevOps Continous Integration and Continous Delivery
Step 5: Click on the Generate option as shown in the screenshot given below.
13
Assignments for DevOps Continous Integration and Continous Delivery
Step 6: You can see the token generated as shown in the screenshot given below. Click on continue.
Note: Copy the token and save it somewhere on your system. It cannot be retrieved
later.
Step 7: Select Maven under the run analysis on your project as shown in the
screenshot given below.
Step 8: Paste the copied token in the pom.xml within the <sonar.login> tag as shown
in the screenshot given below.
14
Assignments for DevOps Continous Integration and Continous Delivery
Step 2: Go to conf folder of Sonar Runner. Make the following changes based on
the name of the Project provided to you and the path where it exists in your system:
15
Assignments for DevOps Continous Integration and Continous Delivery
Open command prompt inside the project src folder in File Explorer and run the following command:
16
Assignments for DevOps Continous Integration and Continous Delivery
<distributionManagement>
<repository>
<id>artifactory</id>
<name>Calc Dev Snapshot</name>
<url>http://localhost: 8081/artifactory/Calc Dev Snapshot</url>
</repository>
</distributionManagement>
Step 5: Click on Create Local Repository.
Step 6: You can view the binaries stored in the artifactory under Application ->
Artifactory -> Packages.
Summary: You have learnt to create a local repository in artifactory in this exercise.
17
Assignments for DevOps Continous Integration and Continous Delivery
18
Assignments for DevOps Continous Integration and Continous Delivery
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.2</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
<executions>
<execution>
<id>default-prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>default-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<!-- implementation is
needed only for Maven 2 -->
<rule
implementation="org.jacoco.maven.RuleConfiguration">
<element>BUNDLE</element>
<limits>
<!--
implementation is needed only for Maven 2 -->
<limit
implementation="org.jacoco.report.check.Limit">
<counter>COMPLEXITY</counter>
<value>COVEREDRATIO</value>
<minimum>0.10</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
19
Assignments for DevOps Continous Integration and Continous Delivery
</plugins>
</build>
<profiles>
<profile>
<id>ut</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-
plugin</artifactId>
<configuration>
<includes>
<include>**/Calculaterut.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>it</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-
plugin</artifactId>
<configuration>
<includes>
<include>**/CalculatorIT.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>pt</id>
<build>
<plugins>
<plugin>
20
Assignments for DevOps Continous Integration and Continous Delivery
<groupId>com.lazerycode.jmeter</groupId>
<artifactId>jmeter-maven-
plugin</artifactId>
<version>2.4.0</version>
<executions>
<execution>
<id>jmeter-tests</id>
<phase>test</phase>
<goals>
<goal>jmeter</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
<!-- -->
Once you run the pom.xml in the order “clean compile test jacoco:report sonar:sonar
war:war”, you can see the output on console as shown below.
[INFO]
[INFO] BUILD SUCCESS
[INFO]
[INFO]Total time: 48.720
[INFO]Finished el:
[INFO]Final Memory: 40M/351M
[INFO]
You can also find the results of the maven goals executed in the target folder of your
project:
1. JUnit test reports in xml and txt: surefire-reports folder
2. Jacoco code coverage reports in html and xml: Under site/jacoco/
3. War file
21
Assignments for DevOps Continous Integration and Continous Delivery
Summary of this Exercise: You have learnt to use Maven tool for build automation.
Note:
You can install Jenkins in two ways on Windows –
a. Install Jenkins as a Windows service
b. Use webserver with a servlet container like GlassFish or Tomcat, and then
deploy Jenkins.war to it.
We have used the first approach in this exercise.
Configuring Jenkins
Step 1: Start the Jenkins server and enter URL http://localhost:8064/jenkins in
browser
22
Assignments for DevOps Continous Integration and Continous Delivery
Step 2: Go to -> Manage Jenkins -> Configure System(Configure global settings and paths)
Step 3: You can observe Jenkins Maven integration as shown below:
Additional configurations
1. Provide the tool configuration (JDK, Maven) in the Manage Jenkins ->Global
Tool configuration tab
23
Assignments for DevOps Continous Integration and Continous Delivery
Step 4: Repeat the step – 3 and select the below mentioned plugins and click on
install without restart.
a. JaCoCo
b. Artifactory
c. Build pipeline
d. Deploy to container
Create a new Folder item with name “Central_CI” using “New Item” option as shown
below. Add jobs of type Freestyle Project for each of tasks needed in the the
continuous integration pipeline.
24
Assignments for DevOps Continous Integration and Continous Delivery
25
Assignments for DevOps Continous Integration and Continous Delivery
26
Assignments for DevOps Continous Integration and Continous Delivery
27
Assignments for DevOps Continous Integration and Continous Delivery
28
Assignments for DevOps Continous Integration and Continous Delivery
29
Assignments for DevOps Continous Integration and Continous Delivery
30
Assignments for DevOps Continous Integration and Continous Delivery
31
Assignments for DevOps Continous Integration and Continous Delivery
32
Assignments for DevOps Continous Integration and Continous Delivery
33
Assignments for DevOps Continous Integration and Continous Delivery
34
Assignments for DevOps Continous Integration and Continous Delivery
35
Assignments for DevOps Continous Integration and Continous Delivery
36
Assignments for DevOps Continous Integration and Continous Delivery
37
Assignments for DevOps Continous Integration and Continous Delivery
38
Assignments for DevOps Continous Integration and Continous Delivery
39
Assignments for DevOps Continous Integration and Continous Delivery
40
Assignments for DevOps Continous Integration and Continous Delivery
41
Assignments for DevOps Continous Integration and Continous Delivery
Step 4: Click on Move and observe the newly moved jobs in that location.
Estimated Time: 20 mins
Summary: You learned how to copy and move jobs on Jenkins.
Exercise 11: Creating pipeline view in Jenkins
Objective: Understand creation of pipeline view in Jenkins
Step 1: Click on the ‘+’ symbol under the Jenkins project folder as shown in the
screenshot given below
Step 2: Provide the name for the pipeline in the viewname field and select the
Build Pipeline View and click create as shown in the screenshot given below.
42
Assignments for DevOps Continous Integration and Continous Delivery
Step 4: Execute the job from setup and you can see the pipeline flow as shown in
the screenshot given below.
43
Assignments for DevOps Continous Integration and Continous Delivery
Step 3: To link the “CALQG” with project, use below shown option available in the
same page (check the project name in without section, then that gate applied)
44
Assignments for DevOps Continous Integration and Continous Delivery
Once static analysis done (by running Maven Target) using SonarQube, you can
observe the current analysis results on SonarQube dashboard as shown below.
Once static analysis done (by running Maven Target) using SonarQube, you can
observe the current analysis results on SonarQube dashboard as shown below.
45
Assignments for DevOps Continous Integration and Continous Delivery
Step 2: Choose the first option as shown below in Jenkins job configuration to link
with downstream job in the pipeline.
Step 3: Go to the project in Eclipse and edit the junit test class
(CalculatorTest.java file under src/java/test/ut)
Step 4: Modify any one of the testcases to get fail as shown in the screenshot
given below.
Step 5: Commit the code and check the Jenkins pipeline for the failure.
Step 6: You can also fail few more test cases and observe the result in the Jenkins
pipeline.
46
Assignments for DevOps Continous Integration and Continous Delivery
Step 1: You can use code coverage results to decide the stability of build as shown
below.
47
Assignments for DevOps Continous Integration and Continous Delivery
Step 2: Choose the first option as shown below in Jenkins job configuration to link
with downstream job in the pipeline.
48
Assignments for DevOps Continous Integration and Continous Delivery
failing in Jenkins. Correct them and observe that the build becomes stable.
(when changes are being made, commit the test code from Eclipse).
Summary of this Exercise:
You have learnt apply gating conditions to ensure code quality and tests pass in
every build.
Additional Exercises
1. Adding custom rules to SonarQube
Objective: Add custom rules to Sonarqube
Requirements: SonarQube 5.6 and above
Step 1: Run the StartSonar.bat file as admin.
Creating the custom rule
Rule to be created:
Avoid single parameter in methods. Here are the parameters:
1. name: Avoid usage of single parameter
2. Description: This rule expects methods to have at least two parameters
3. This is a coding guideline
4. Priority is MAJOR
Add this rule to Sonarqube and check the same using the calculator workspace
provided
Hint: Use method.parameters().size() to check the number of parameters in a
method
Estimated time: 30 mins
Summary: You have learnt to customize the rules in SonarQube according to your
requirement for quality analysis.
49
Assignments for DevOps Continous Integration and Continous Delivery
50
Assignments for DevOps Continous Integration and Continous Delivery
51
Assignments for DevOps Continous Integration and Continous Delivery
A new tab opens below which shows the issues found in the current java file
selected.
52
Assignments for DevOps Continous Integration and Continous Delivery
A new empty SonarLint Rule Description tab opens. Now select one of the issue in
the SonarLint On-the-fly tab and switch to SonarLint Rule Description tab. This
suggests the necessary changes to be made in the code to retain the quality of the
code.
In the Goals tab, use clean, compile and sonar: sonar and execute the pom.xml file
54
Assignments for DevOps Continous Integration and Continous Delivery
Once the build is successful, we need to bind the current java project with the
SonarQube project.
Step 3: Go to Window->Show View->Other.
55
Assignments for DevOps Continous Integration and Continous Delivery
56
Assignments for DevOps Continous Integration and Continous Delivery
This opens a SonarQube Servers tab below. Click on the Connect to a SonarQube
Server option showing up in the tab.
57
Assignments for DevOps Continous Integration and Continous Delivery
Step 5: Go back to Eclipse, right click on the project under SonarLint click on ‘Bind
to a SonarQube Project’.
58
Assignments for DevOps Continous Integration and Continous Delivery
Once you click on Auto bind, you will see ‘SonarQubeTestKey/ on server localhost’
under the title SonarQubeProject.
Step 7: Now open a browser and open http://localhost:9000 to view the project on
the SonarQube server.
59
Assignments for DevOps Continous Integration and Continous Delivery
60