Andre's Blog

Andre's Technical Blog About Using Jenkins


20160717 About This Jenkins Blog
20160717 What Jenkins Does
20160224 How To Start Jenkins With Java From Shell
20160717 How To Configure A Job
20160717 How To Install Plug-ins
20160224 How To Block Jenkins Jobs With Lockable Resources
20160221 How To Create The Skeleton Of A Jenkins Plugin With Gradle
20160221 Compile A Hello-World-JPI
20160221 How To Manually Test The Hello-World-JPI With Gradle
20160221 How To Modify the Hello-World-JPI And See The Modification In Jenkins

20160717 About This Jenkins Blog

To know why I write a blog about Jenkins, please read my blog post

I have moved my previous blog entries about Jenkins to this blog, to make it easier for you to find them. Though the time line has changed I think the new order of the entries makes more sense than starting with the details before explaining the big picture.

20160717 What Jenkins Does

The Jenkins Wiki explains Jenkins this way :

"Jenkins is an award-winning, cross-platform, continuous integration and continuous delivery application that increases your productivity. Use Jenkins to build and test your software projects continuously making it easier for developers to integrate changes to the project, and making it easier for users to obtain a fresh build. It also allows you to continuously deliver your software by providing powerful ways to define your build pipelines and integrating with a large number of testing and deployment technologies."

The question is, does this help you if you don't know about continuous integration and continuous delivery or if you know about it, but not how it works?

Let me try to explain what Jenkins does and what you can do with Jenkins to setup continuous integration and continuous delivery for your software product.

FYI : Jenkins is a really mighty tool and I don't want to explain every detail and function of Jenkins, I will just mention the basics to explain how to use Jenkins in general.

  1. Jenkins is a web application with a central Master and any number of Nodes.
  2. Jenkins functionality can be extended by installing plug-ins.
  3. Jenkins jobs execute software, the simplest way to execute software is to add the build step "Execute Windows batch command", which is like writing a batch.
  4. Jenkins jobs can be started by a time trigger, they can be cron jobs.
  5. Jenkins jobs can depend on each other, a job can be triggered by an other job and this trigger can depend on the success of the preceding job.
  6. Jenkins jobs can be configured to run only on specific nodes. This can be very interesting if you need special hardware to execute your job and if this hardware is not available on every node.
  7. The execution of Jenkins jobs can be blocked if the required resource is in use.

20160224 How To Start Jenkins With Java From Shell

You just need to download the Jenkins .war file and run it with Java, this will create all directories and files for you.

Jenkins can be started standalone with

java -jar jenkins-war-1.649.war

and the jenkins home will be in your home directory in (here with bash)

$HOME/.jenkins

and the default URL is

localhost:8080

20160717 How To Configure A Job

FYI : In Jenkins a job is an item.

  1. Click New Item
  2. Enter an item name, click Freestyle project and click OK
  3. Go to the Build tab
  4. Click Add build step and select Execute Windows batch command
  5. Add some example commands like echo execute my job
  6. Click Save
  7. Click Build Now
  8. Click #1 in the Build Historyat the bottom of the jobs page
  9. Click Console Output

And there you see something like

[job1] $ cmd /c call C:\Users\amos\AppData\Local\Temp\hudson4614265032990843424.bat

C:\Users\amos\.jenkins\workspace\job1>echo execute my job
execute my job

C:\Users\amos\.jenkins\workspace\job1>exit 0
Finished: SUCCESS

20160717 How To Install Plug-ins

  1. Click Manage Jenkins
  2. Click Manage Plugins
  3. Click the Available tab
  4. Type a fragment of the plug-in name into Filter
  5. Click the box in the Install column of the plug-in you want to install.
  6. Click Install without restart
  7. Repeat the steps till you have installed all plug-ins you need.

20160224 How To Block Jenkins Jobs With Lockable Resources

It often happens you have to block jenkins jobs, because they are mutual exclusive due to resource usage.

If the amount of your jobs never changes, you can use the Build Blocker Plugin, where you can define with regular expressions, which jobs shall be blocked while other jobs are running.

Adding blockers to some jobs is feasible, but when your number of jobs grows, it becomes a problem, you start repeating yourself, because you have to mutual exclude the new jobs from the old jobs and vice versa.

When this happens, better start using the Lockable Resources Plugin, where you can define resources in

  1. Click Jenkins
  2. Manage Jenkins
  3. Configure System
  4. Add Lockable Resource

which you can use in your jobs with

  1. Click your job
  2. Configure
  3. Activate "This build requires lockable resources"
  4. Add the needed resources

20160221 How To Create The Skeleton Of A Jenkins Plugin With Gradle

Jenkins plugins are usually build with maven but since I know that gradle can handle maven, I thought maybe I don't need to know about how to do it with maven.

Here you find the documentation how to create the JPI skeleton with gradle.

The build.gradle looks like

buildscript {
    repositories {
        jcenter()
        maven {
            url 'http://repo.jenkins-ci.org/releases/'
        }
    }
    dependencies {
        classpath 'org.jenkins-ci.tools:gradle-jpi-plugin:0.15.0'
    }
}

apply plugin: 'org.jenkins-ci.jpi'


group = "org.jenkins-ci.plugins"
version = "0.0.1-SNAPSHOT"    // Or whatever your version is.
description = "A description of your plugin"

jenkinsPlugin {
    // Version of Jenkins core this plugin depends on.
    coreVersion = '1.420'
    // Human-readable name of plugin.
    displayName = 'Hello World plugin built with Gradle'
    // URL for plugin on Jenkins wiki or elsewhere.
    url = 'http://domain.de'
    // Plugin URL on GitHub. Optional.
    gitHubUrl = 'https://github.com/jenkinsci/some-plugin'
    // Plugin ID, defaults to the project name without trailing '-plugin'.
    shortName = 'hello-plugin'

    // The developers section is optional,
    // and corresponds to the POM developers section.
    developers {
        developer {
            id 'developer-id'
            name 'firstname lastname'
            email 'emailaddress@domain.de'
        }
    }
}

These are the main tasks you need :

gradle jpi // creates the maven skeleton

gradle build // builds the plugin, find it in build/libs

gradle server // starts a jenkins server on localhost:8080 for testing,
              // your plugin is already installed

20160221 Compile A Hello-World-JPI

I have downloaded the Java source from here and saved it in src/main/java/hudson/plugins/ and run gradle build.

20160221 How To Manually Test The Hello-World-JPI With Gradle

FYI: Manually testing means, we will test the plug-in by using it and looking at its output with a browser. Automatically would mean this is done by a deployment job or it is tested with unit tests.

When you have built your plug-in, you want to use it in jenkins to see it works.

With gradle server you start a jenkins server where your plug-in is already installed.

You have to create a jenkins job to and use the plug-in there. The hello-world-jpi implements a build step, where it says "Hello, ...", so I added it as build step.

20160221 How To Modify the Hello-World-JPI And See The Modification In Jenkins

After reading the java source I found out the perform() function is doing something, so I added a try-catch-block there. Intellij IDEA was a big help, its intellisense gave me some hints to try out.

@Override
    public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) {

        try {
            listener.getLogger().println("build.getBuiltOn().getDisplayName() : " +
                build.getBuiltOn().getDisplayName());
            listener.getLogger().println("build.getBuiltOn().getDescriptor() : " +
                build.getBuiltOn().getDescriptor());
            listener.getLogger().println("build.getBuildVariables() : " +
                build.getBuildVariables());
        } catch (Exception e) {
            System.out.println(e);
        }

        return true;
    }

FYI: When your jenkins job has no build variables, then the output of the JPI will just be {}. Enable build variables in jenkins and add a variable as parameter in your job, then it will be printed.

Select where to go ...


The Blog
My Technical Blogs
Projects
Blogs Of Friends
RSS
CV/About
Me