Book Image

Jenkins Continuous Integration Cookbook

By : Alan Mark Berg
Book Image

Jenkins Continuous Integration Cookbook

By: Alan Mark Berg

Overview of this book

<p>Jenkins is a highly popular continuous integration server. Its correct use supports a quality software development process. Jenkins is great at finding issues in software early and communicating it to a wide audience. Jenkins is also easily extendable with a simple framework for writing plugins. Currently there are over 400 plugins available for inclusion.<br /><br /><em>Jenkins Continuous Integration Cookbook</em> has over 80 recipes describing practical ways to use Jenkins and expanding its feature set by selective use of the best of breed plugins. Jenkins has a simple framework for writing plugins. There are over 400 plugins available. Therefore, it is easy to get lost in possibilities. Using practical recipes, this book will guide you through the complexities. The recipes are bundled into themes including security, maintainability, communication, building software, the valid use of code metrics, testing remotely, and writing your first plugin.<br /><br /><em>Jenkins Continuous Integration Cookbook</em> includes problem solving and how to do recipes for many common and less common tasks. A wide range of topics are covered from integrating, securing, and maintaining Jenkins in your organization to writing your first extension.<br /><br />The book begins with common maintenance tasks followed by securing Jenkins and enabling SSO. Then it explores the relationship between Jenkins builds and the Maven pom.xml. You will then learn ways to effectively communicate with various target audiences (developers, project managers, the public). You will then explore source code metrics with related recipes, and set up and run remote stress and functional tests. The book finally lists a series of 11 interesting plugins with a concluding recipe on how to create your first plugin.<br /><br /><em>Jenkins Continuous Integration Cookbook</em> describes solutions and optimizations to problems commonly found.</p>
Table of Contents (15 chapters)
Jenkins Continuous Integration Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Signaling the need to archive


Each development team is unique. Teams have their own way of doing business. In many organizations, there are one-off tasks that need to be done periodically, for example at the end of each year.

This recipe details a script that checks for the last successful run of any Job, and if the year is different to the current year, then a warning is set at the beginning of the Jobs description. Thus, hinting to you it is time to perform some action, such as archiving and then deleting. You can, of course, programmatically do the archiving. However, for high value actions, it is worth forcing interceding, letting the Groovy scripts focus your attention.

Getting ready

Log in to Jenkins with an administrative account.

How to do it...

Within the Manage Jenkins page, click on the Script console link, and run the following script:

import hudson.model.Run;
import java.text.DateFormat;

def warning='<font color=\'red\'>[ARCHIVE]</font> '
def now=new Date()

for (job in hudson.model.Hudson.instance.items) { 
    println "\nName: ${job.name}"
    Run lastSuccessfulBuild = job.getLastSuccessfulBuild()
    if (lastSuccessfulBuild != null) {
      def time = lastSuccessfulBuild.getTimestamp().getTime()
      if (now.year.equals(time.year)){
         println("Project has same year as build");
      }else {
        if (job.description.startsWith(warning)){
            println("Description has already been changed");
        }else{
            job.setDescription("${warning}${job.description}")
        }
     }
   }
}

Any project that had its last successful build in another year than this will have the word [ARCHIVE] in red, added at the start of its description.

How it works...

Reviewing the code listing:

A warning string is defined, and the current date is stored in now. Each Job in Jenkins is programmatically iterated through the for statement.

Jenkins has a class to store information about the running of builds. The runtime information is retrieved through job.getLastSuccessfulBuild(), and is stored in the lastSuccessfulBuild instance. If no successful build has occurred, then lastSuccessfulBuild is set to null, otherwise it has the runtime information.

The time of the last successful build is retrieved, and then stored in the time instance through lastSuccessfulBuild.getTimestamp().getTime().

The current year is compared with the year of the last successful build, and if they are different and the warning string has not already been added to the front of the Job description, then the description is updated.

Tip

Javadoc

You will find the Job API mentioned at http://javadoc.jenkins-ci.org/hudson/model/Job.html and the Run information at http://javadoc.jenkins-ci.org/hudson/model/Run.html.

There's more...

Before writing your own code, you should review what already exists. With 300 plugins, Jenkins has a large, freely-available, and openly licensed example code base. Although in this case the standard API was used, it is well worth reviewing the plugin code base. In this example, you will find part of the code re-used from the lastsuccessversioncolumn plugin (https://github.com/jenkinsci/lastsuccessversioncolumn-plugin/blob/master/src/main/java/hudson/plugins/lastsuccessversioncolumn/LastSuccessVersionColumn.java).

Tip

If you find any defects while reviewing the plugin code base, please contribute to the community through patches and bug reports.

See also

  • Scripting the Jenkins command-line interface

  • Global modifications of Jobs with Groovy