Book Image

Jenkins Continuous Integration Cookbook - Second Edition

By :
Book Image

Jenkins Continuous Integration Cookbook - Second Edition


Overview of this book

<p>Jenkins is an award-wining and one of the most popular Continuous Integration servers in the market today. It was designed to maintain, secure, communicate, test, build, and improve the software development process.</p> <p>This book starts by examining the most common maintenance tasks. This is followed by steps that enable you to enhance the overall security of Jenkins. You will then explore the relationship between Jenkins builds and Maven pom.xml. Then, you will learn how to use plugins to display code metrics and fail builds to improve quality, followed by how to run performance and functional tests against a web application and web services. Finally, you will see what the available plugins are, concluding with best practices to improve quality.</p> <p>&nbsp;</p> <div class="book-toc-chapter">&nbsp;</div> <h2>Read an Extract from the book</h2> <h2><span lang="EN-GB" style="color: windowtext;">Analyzing project data with the R plugin</span></h2> <p class="NormalPACKT">This recipe describes how to use R to process metrics on each file in your project workspace. The recipe does this by traversing the workspace and collecting a list of files of a particular extension such as Java. The R script then analyzes each file individually and finally plots the results in a graphical format to a PDF file. The workflow is common to almost all quality-related analysis of software projects. This recipe is easily customized for tasks that are more complex.</p> <p class="NormalPACKT" style="margin-bottom: .0001pt;"><span style="letter-spacing: -.1pt;">In this example, we are looking at the size in words of the text files, printing to the console the names of large files and plotting the sizes of all files. From the visual representation, you can easily see which files are particularly large. If your property file is much larger than the other property files, it is probably corrupt. If a Java file is too large, it is difficult to read and understand.</span></p> <h2><span lang="EN-GB" style="color: windowtext;">How to do it...</span></h2> <ol> <li>Create a free-style job with the name <em></em>.</li> <li>In the <strong>Source Code Management </strong>section, select <strong>Subversion</strong>.</li> <li>Add the<strong> Repository URL</strong> as <a href=""></a>.</li> <li>In the <strong>build</strong> section, under <strong>Add build step</strong>, select <strong>Execute R script</strong>.</li> <li class="line-numbers">In the <strong>Script text</strong> area add the following code: <pre class="line-numbers"><code class="language-java">processFile &lt;- function(file){ text &lt;- readLines(file,encoding="UTF-8") if (length(text)&gt; 500) print(file) length(text) } javaFiles &lt;- list.files(Sys.getenv('WORKSPACE'), recursive = TRUE, full.names = TRUE, pattern = "\\.java$") propertiesFiles &lt;- list.files(Sys.getenv('WORKSPACE'), recursive = TRUE, full.names = TRUE, pattern = "\\.properties$") resultJava &lt;- sapply(javaFiles, processFile) resultProperties &lt;- sapply(propertiesFiles,processFile) warnings() filename &lt;-paste('Lengths_JAVA_',Sys.getenv('BUILD_NUMBER'),'.pdf',sep ="") pdf(file=filename) hist(resultJava,main="Frequency of length of JAVA files") filename &lt;- paste('Lengths_Properties_',Sys.getenv('BUILD_NUMBER'),'.pd f',sep="") pdf(file=filename) hist(resultProperties,main="Frequency of length of Property files")</code></pre> </li> <li>Click on the <strong>Save</strong> button.</li> <li>Click on the <strong>Build Now</strong> icon.</li> <li>Review the console output from the build. It should appear similar to the following: <pre class="line-numbers"><code class="language-java">At revision 313948 no change for since the previous build [] $ Rscript /tmp/hudson7641363251840585368.R [1] "/var/lib/jenkins/workspace/ rg/sakaiproject/profile2/logic/" [1] "/var/lib/jenkins/workspace/" [1] "/var/lib/jenkins/workspace/ org/sakaiproject/profile2/dao/impl/" 14: In readLines(file, encoding = "UTF-8") : incomplete final line found on '/var/lib/jenkins/workspace/ org/apache/wicket/markup/html/form/upload/MultiFileUploadFi' Finished: SUCCESS</code></pre> </li> <li>Visit the workspace and review the files <em>Lengths_Properties_1.pdf</em>, <em>Lengths_JAVA_1.pdf</em>.</li> <li><span style="letter-spacing: -.1pt;">Notice the straggler files with a large number of lines. Property files should be of roughly similar length, as they contain, in this case, the international translations for the GUI.</span></li> </ol> <p class="NormalPACKT" style="margin-bottom: .0001pt;"><span lang="EN-GB">&nbsp;</span>This feels like a well-balanced project, as there are only a few files that have a large number of lines of code.</p> <h2><span lang="EN-GB" style="color: windowtext;">How it works...</span></h2> <p class="NormalPACKT">You loaded in the profile2 tool from subversion <a href=""></a>. This code is used by millions of students around the world and represents mature, realistic production code.</p> <p class="NormalPACKT">Within your R script, you defined a function that takes a filename as input and then reads the file into a text object. The function then checks to see whether the number of lines is greater than 500. If it is greater than 500 lines then the filename is printed to the console output. Finally, the function returns the number of lines in the text file.</p> <pre class="line-numbers"><code class="language-java">processFile &lt;- function(file){ text &lt;- readLines(file,encoding="UTF-8") if (length(text)&gt; 500) print(file) length(text) }</code></pre> <p class="NormalPACKT">Next, the script discovers the property and Java files under the workspace. The file search is filtered by the value defined in the <em>pattern</em> argument. In this case, <em>.java</em>:</p> <pre class="line-numbers"><code class="language-java">javaFiles &lt;- list.files(Sys.getenv('WORKSPACE'), recursive = TRUE, full.names = TRUE, pattern = "\\.java$") propertiesFiles &lt;- list.files(Sys.getenv('WORKSPACE'), recursive = TRUE, full.names = TRUE, pattern = "\\.properties$")</code></pre> <p class="NormalPACKT">The list of filenames is passed one name at a time to the <em>processFile</em> function you have previously defined. The results are a list of file lengths that are stored in the <em>resultJava </em>and <em>resultProperties</em> objects:</p> <pre class="line-numbers"><code class="language-java">resultJava &lt;- sapply(javaFiles, processFile) resultProperties &lt;- sapply(propertiesFiles,processFile)</code></pre> <p class="NormalPACKT">The <em>warnings()</em> function produces a list of issues generated while running the <em>sapply</em> command:</p> <pre class="line-numbers"><code class="language-java">14: In readLines(file, encoding = "UTF-8") : incomplete final line found on '/var/lib/jenkins/workspace/ che/wicket/markup/html/form/upload/MultiFileUploadField_ca_ES.prop erties'</code></pre> <p class="NormalPACKT">This is stating that a new line was expected at the end of the file. It is not a critical issue. Showing the warnings is a helpful approach to discovering corrupted files.</p> <p class="NormalPACKT">Finally, we generate two histograms of the results, one for the Java file and the other for the properties files. The filename is created from a constant string followed by the <em>BUILD_NUMBER</em> environment variable that is set uniquely for each build. The <em>pdf</em> function tells R that the output is to be stored in a PDF file and the <em>hist</em> function draws a histogram of the results:</p> <pre class="line-numbers"><code class="language-java">filename &lt;-paste('Lengths_JAVA_',Sys.getenv('BUILD_NUMBER'),'.pdf',sep="") pdf(file=filename) hist(resultJava,main="Frequency of length of JAVA files")</code></pre> <h2><span lang="EN-GB" style="color: windowtext;">There's more...</span></h2> <p class="NormalPACKT">When writing R code for processing your files, don't reinvent the wheel. R has many libraries for manipulating text. The stringi library is one example (<a href=""></a>). Here is some example code that counts the number of words in a text file:</p> <pre class="line-numbers"><code class="language-java">library(stringi) processFile &lt;-function(file){ stri_stats_latex(readLines(file,encoding="UTF-8")) } results&lt;-processFile(file.choose()) paste("Number of words in file:", results[4])</code></pre> <p class="NormalPACKT">The script defines the function <em>processFile</em>. The function requires a filename. The file is read into the <em>stri_stats_latex</em> function. This function is included in the <em>stringi</em> library. It returns a summary of the file as a vector (a series of numbers).</p> <p class="IgnorePACKT" style="margin: 0in 0in 6.0pt 0in;">The <em>file.choose()</em> function pops up a dialog that allows you to browse your file system and choose a file. The call returns the fully qualified path to the file. It passes the value to the <em>processFile</em> function call. The results are stored in the results vector. The script then prints out the fourth number that is the number of words in the file.</p> <blockquote> <p class="InformationBoxPACKT" style="margin: 9.0pt 0in 9.0pt 0in;"><em>Another interesting R package for text mining is tm: (<a href=""></a>). The tm package has the ability to load a set of text files and analyze them in many different ways.</em></p> </blockquote> <div class="book-toc-chapter">&nbsp;</div>
Table of Contents (16 chapters)
Jenkins Continuous Integration Cookbook Second Edition
About the Author
About the Reviewers

Configuring the LDAP plugin

LDAP is the standard for Enterprise directory services. This recipe explains how to attach Jenkins to your test LDAP server.

Getting ready

To try this recipe, you should first perform the steps mentioned in the Installing OpenLDAP recipe.

How to do it...

  1. Go to the Configure Global Security screen and select Enable security.

  2. Check the LDAP checkbox.

  3. Add the Server value as

  4. Click on the Advance button and add the following details:

    • User Search Base: ou=people,ou=mycompany,dc=nodomain

    • User Search filter: uid={0}

    • Group Search base: ou=groups,ou=mycompany,dc=nodomain

How it works...

The test LDAP server supports anonymous binding: you can search the server without authenticating. Most LDAP servers allow this approach. However, some servers are configured to enforce specific information security policies. For example, your policy might enforce being able to anonymously verify that a user's record exists, but you may not be able to retrieve specific attributes such...