Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Sanity Checking Your Feature Branches With a Personal CI Server

DZone's Guide to

Sanity Checking Your Feature Branches With a Personal CI Server

You can use a personal Jenkins CI server to provide a sanity check of your feature branches before a pull request.

· DevOps Zone
Free Resource

The Nexus Suite is uniquely architected for a DevOps native world and creates value early in the development pipeline, provides precise contextual controls at every phase, and accelerates DevOps innovation with automation you can trust. Read how in this ebook.

In the previous article, I stepped you through the process of building a basic personal CI server with Jenkins. We got to a point where we could build a Java Web application with Gradle and deploy it to WildFly. For those that are impatient, the exported VirtualBox image with Jenkins configured using these steps is available here. The username and password combination for Linux, Jenkins, and WildFly is myci and password.

I frequently find myself working on legacy code bases that have not had any code standards applied to them. It is unlikely that anyone will ever invest the time to clean up the code, but as a professional, I’d like to know that at least the bits that I touch are brought up-to-date with my own standards.

In these situations, it is not feasible to break the build because the code hasn’t met the requirements of a tool like Checkstyle, simply because the code will never meet the requirements. However, with a personal CI server, it is trivial to configure automated checks that give us, as individual developers, the confidence that our work meets our own standards.

Preparing the Tools

We’ll need to install cloc with the following command:

sudo apt-get install cloc

We also need to download the checkstyle-7.3-all.jar file from here and save it to the /opt directory.

Creating a Check Project

We’ll start by cloning the existing Sample_Project_Build item. Open up http://localhost:9080/ and click the New Item link. Enter a project name of Sample_Project_Check, and down the bottom of the screen, enter Sample_Project_Build into the Copy from field.

The resulting item will clone all the settings from Sample_Project_Build.

Remove the Maven and Gradle sections. We will not be building anything as part of the check, so these sections are not necessary.

We will reuse the Execute shell section and replace it with the following script:

files=`git diff --name-only origin/master origin/${BRANCH_NAME}`
filesArg=''
for line in $files ; do
    if [[ "${line}" == *.java ]]
    then
            filesArg="${filesArg} ${line}"
    fi  
done

java -jar /opt/checkstyle-7.3-all.jar -c "https://raw.githubusercontent.com/mcasperson/iridium-gradle-plugin/master/src/main/resources/checkstyle.xml" ${filesArg}
cloc --by-percent c --by-file ${filesArg}

This script does the following:

  1. Finds the *.java files that were changed in the current branch against the master.
  2. Passes those files to Checkstyle, which will check them for violations against a configuration file available from a web server (I’ve used the Checkstyle rules from a project called Iridium that I maintain, but obviously, this can be modified to reference your own ruleset).
  3. Passes those files to cloc, which counts the lines of code.

Since our checks are always going to be from a feature branch to master, it doesn’t make sense for Jenkins to run these checks against master because there would be no changed files and nothing to check. So, we need to change the Included branches that we want Jenkins to monitor.

Find the Branch Sources > Git section and click the Advanced button. This will display the Included branches field.

In my case, I’m checking branches that match the pattern casper/*, but you would change the string casper to the name that you are using in your own feature branches.

Now, whenever changes are committed to our feature branch, Jenkins will generate a report like this:

Starting audit... 
[WARN] /var/lib/jenkins/workspace/_Check_casper_new-feature-1-AO6JCLLY2BCFOHEIC2XGHAPN36AXP5MXYUVYSCYHBWS5TP2Z63TA/src/main/java/org/gradle/sample/Greeter.java:8: Line has leading space characters; indentation should be performed with tabs only. [RegexpSinglelineJava] 
[WARN] /var/lib/jenkins/workspace/_Check_casper_new-feature-1-AO6JCLLY2BCFOHEIC2XGHAPN36AXP5MXYUVYSCYHBWS5TP2Z63TA/src/main/java/org/gradle/sample/Greeter.java:9: Line has leading space characters; indentation should be performed with tabs only. [RegexpSinglelineJava] 
[WARN] /var/lib/jenkins/workspace/_Check_casper_new-feature-1-AO6JCLLY2BCFOHEIC2XGHAPN36AXP5MXYUVYSCYHBWS5TP2Z63TA/src/main/java/org/gradle/sample/Greeter.java:10: Line has leading space characters; indentation should be performed with tabs only. [RegexpSinglelineJava] 
[WARN] /var/lib/jenkins/workspace/_Check_casper_new-feature-1-AO6JCLLY2BCFOHEIC2XGHAPN36AXP5MXYUVYSCYHBWS5TP2Z63TA/src/main/java/org/gradle/sample/Greeter.java:11: Line has leading space characters; indentation should be performed with tabs only. [RegexpSinglelineJava] 
[WARN] /var/lib/jenkins/workspace/_Check_casper_new-feature-1-AO6JCLLY2BCFOHEIC2XGHAPN36AXP5MXYUVYSCYHBWS5TP2Z63TA/src/main/java/org/gradle/sample/Greeter.java:12: Line has leading space characters; indentation should be performed with tabs only. [RegexpSinglelineJava] 
[WARN] /var/lib/jenkins/workspace/_Check_casper_new-feature-1-AO6JCLLY2BCFOHEIC2XGHAPN36AXP5MXYUVYSCYHBWS5TP2Z63TA/src/main/java/org/gradle/sample/Greeter.java:13: Line has leading space characters; indentation should be performed with tabs only. [RegexpSinglelineJava] 
[WARN] /var/lib/jenkins/workspace/_Check_casper_new-feature-1-AO6JCLLY2BCFOHEIC2XGHAPN36AXP5MXYUVYSCYHBWS5TP2Z63TA/src/main/java/org/gradle/sample/Greeter.java:14: Line has leading space characters; indentation should be performed with tabs only. [RegexpSinglelineJava] 
[WARN] /var/lib/jenkins/workspace/_Check_casper_new-feature-1-AO6JCLLY2BCFOHEIC2XGHAPN36AXP5MXYUVYSCYHBWS5TP2Z63TA/src/main/java/org/gradle/sample/Greeter.java:15: Line has leading space characters; indentation should be performed with tabs only. [RegexpSinglelineJava] 
[WARN] /var/lib/jenkins/workspace/_Check_casper_new-feature-1-AO6JCLLY2BCFOHEIC2XGHAPN36AXP5MXYUVYSCYHBWS5TP2Z63TA/src/main/java/org/gradle/sample/Greeter.java:15:9: '}' at column 9 should be on the same line as the next part of a multi-block statement (one that directly contains multiple blocks: if/else-if/else, do/while or try/catch/finally). [RightCurly] 
[WARN] /var/lib/jenkins/workspace/_Check_casper_new-feature-1-AO6JCLLY2BCFOHEIC2XGHAPN36AXP5MXYUVYSCYHBWS5TP2Z63TA/src/main/java/org/gradle/sample/Greeter.java:16: Line has leading space characters; indentation should be performed with tabs only. [RegexpSinglelineJava] 
[WARN] /var/lib/jenkins/workspace/_Check_casper_new-feature-1-AO6JCLLY2BCFOHEIC2XGHAPN36AXP5MXYUVYSCYHBWS5TP2Z63TA/src/main/java/org/gradle/sample/Greeter.java:17: Line has leading space characters; indentation should be performed with tabs only. [RegexpSinglelineJava] 
[WARN] /var/lib/jenkins/workspace/_Check_casper_new-feature-1-AO6JCLLY2BCFOHEIC2XGHAPN36AXP5MXYUVYSCYHBWS5TP2Z63TA/src/main/java/org/gradle/sample/Greeter.java:18: Line has leading space characters; indentation should be performed with tabs only. [RegexpSinglelineJava] 
[WARN] /var/lib/jenkins/workspace/_Check_casper_new-feature-1-AO6JCLLY2BCFOHEIC2XGHAPN36AXP5MXYUVYSCYHBWS5TP2Z63TA/src/main/java/org/gradle/sample/Greeter.java:19: Line has leading space characters; indentation should be performed with tabs only. [RegexpSinglelineJava] 
Audit done. 
+ cloc --by-percent c --by-file src/main/java/org/gradle/sample/Greeter.java 
      1 text file. 
      1 unique file.                               
      0 files ignored.
github.com/AlDanial/cloc v 1.68  T=0.01 s (167.0 files/s, 3340.1 lines/s)
-----------------------------------------------------------------------------------------------
File                                                      blank %      comment %           code
-----------------------------------------------------------------------------------------------
src/main/java/org/gradle/sample/Greeter.java                11.11           0.00             18
-----------------------------------------------------------------------------------------------
Finished: SUCCESS

This gives us a quick sanity check of the issues that will affect files that this feature branch has touched. Right off the bat, we can see that Checkstyle has found a bunch of violations and cloc is reporting that a file has 0% code comments. Both are issues that we should be aware of, but that shouldn’t necessarily break the development flow and that certainly shouldn't be breaking the development build or deployment.

The DevOps Zone is brought to you in partnership with Sonatype Nexus.  See how the Nexus platform infuses precise open source component intelligence into the DevOps pipeline early, everywhere, and at scale. Read how in this ebook

Topics:
devops ,continuous integration ,feature branches

Published at DZone with permission of Matthew Casperson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}