May 17, 2018

Jenkins, Groovy init scripts and custom Tools

Filed under: General Information,ResearchAndDevelopment — Ryan Wilcox @ 10:56 pm

I’ve been working with Jenkins quite a bit lately.

When I set up a system I want it to be as reproducable as possible: you can never trust hardware, especially when it’s virtual.

I found Accenture’s Jenkins Docker configuration . It’s super good, especially as a basis of sample code. Based on this code I was able to install and configure plugins (ie I set up a Node.js Jenkins tool, etc etc).

My Jenkins installation also uses the CustomTool Plugin extensively, to provide CLI tools to my Jenkins pipelines. So I wanted to add my custom tool configuration to my Jenkins init scripts.

There’s plenty of documentation on installing tools based on plugins (even a section of my learning notes!) but the custom tools plugin seems to be left out of this flurry of documentation. No longer!

Installing custom tools is a bit different from installing tools that come explicitly as part of a plugin, and here is some code that worked for me:


import jenkins.model.*
import com.cloudbees.jenkins.plugins.customtools.CustomTool;
import com.synopsys.arc.jenkinsci.plugins.customtools.versions.ToolVersionConfig;

a=Jenkins.instance.getExtensionList(com.cloudbees.jenkins.plugins.customtools.CustomTool.DescriptorImpl.class)[0];

def installs = a.getInstallations()
def found = installs.find {
it.name == "gcc"
}

if ( found ) {
println "gcc is already installed"
} else {
println "installing gcc tool"

def newI = new CustomTool("gcc", "/usr/local/gcc/", null, "bin", null, ToolVersionConfig.DEFAULT, null)
installs += newI
a.setInstallations( (com.cloudbees.jenkins.plugins.customtools.CustomTool[])installs );
a.save()
}

Enjoy!

2 Comments »

  1. Hi,

    nice codesnippet Ryan, thanks! Have you tried adding a download URL and zip extraction?
    No success for me :(

    Regards
    Tim

    Comment by Tim — September 25, 2018 @ 5:48 am

  2. Hi Tom,

    I managed to create a CustomTool that uses a Batch Command installer to install kubectl.
    Here is the code:

    import jenkins.model.*
    import hudson.tools.*
    import com.cloudbees.jenkins.plugins.customtools.*
    import com.synopsys.arc.jenkinsci.plugins.customtools.versions.ToolVersionConfig

    def KUBECTL_VERSION = “1.12.2”

    def inst = Jenkins.getInstance()
    def desc = inst.getExtensionList(com.cloudbees.jenkins.plugins.customtools.CustomTool.DescriptorImpl.class)[0]
    def customTools = []

    List installers = new ArrayList();
    installers.add(new BatchCommandInstaller(null,
    “curl –silent https://storage.googleapis.com/kubernetes-release/release/v“+KUBECTL_VERSION+”/bin/linux/amd64/kubectl > /dev/null\n” +
    “chmod +x ./kubectl”,
    “kubectl-v”+KUBECTL_VERSION))

    List<ToolProperty> properties = new ArrayList<ToolProperty>()
    properties.add(new InstallSourceProperty(installers))

    def installation = new CustomTool(
    “kubectl”,
    “”,
    properties,
    “”,
    null,
    ToolVersionConfig.DEFAULT,
    null
    )

    customTools.push(installation)

    desc.setInstallations((com.cloudbees.jenkins.plugins.customtools.CustomTool[]) customTools)
    desc.save()

    In order to use the zip extraction you will need to replace the BatchCommandInstaller with a ZipExtractionInstaller with the necessary setup.

    Comment by Darius — December 5, 2018 @ 9:32 am

RSS feed for comments on this post. TrackBack URI

Leave a comment