Jenkins Pipeline - How do I use the 'tool' option to specify a custom tool?

I ran into the same problem. I got to this workaround:

 environment {
    GROOVY_HOME = tool name: 'Groovy-2.4.9', type: 'hudson.plugins.groovy.GroovyInstallation'
}
stages {
    stage('Run Groovy') {
        steps {
            bat "${groovy_home}/bin/groovy <script.name>"
        }
    }
}

Somehow the tool path is not added to PATH by default (as was customary on my 1.6 Jenkins server install). Adding the ${groovy_home} when executing the bat command fixes that for me. This way of calling a tool is basically lent from the scripted pipeline syntax. I am using this for all my custom tools (not only groovy).

The tool part:

tool name: 'Groovy-2.4.9', type: 'hudson.plugins.groovy.GroovyInstallation'

was generated by the snippet generator like you did.

According to the Jenkins users mailing list, work is still ongoing for a definitive solution, so my solution really is a work around.


This is my first time commenting on stack overflow, but I've been looking for this answer for a few days and I think I have a potential solution. Checking out Fholst answer, I'd like to expand on it. That environment stanza I think may work for declarative syntax, but on a scripted pipeline you must use the withEnv() equivalent, and pass in the tools via a gString: i.e. ${tool 'nameOfToolDefinedInGlobalTools'}. For my particular use case, for reasons beyond my control, we do not have maven installed on our jenkins host machine, but there is one defined within the global tools configuration. This means I need to add mvn to the path before executing my sh commands within my steps. What I have been able to do is this:

        withEnv(["PATH+MVN=${tool 'NameOfMavenTool'}/bin"]){
            sh '''
                echo "PATH = ${PATH}"
            '''
        }

This should give you what you need. Please ignore the triple single quotes on the sh line, I actually have several environment variables loaded and simply removed them from my snippet.

Hope this helps anyone who has been searching for this solution for days. I feel your pain. Cobbled this together from looking through the console output of a declarative pipeline script (if you use tools{} stanza it will show you how it builds those environment variables and wraps your subsequent declarative steps) and the following link: https://go.cloudbees.com/docs/cloudbees-documentation/use/automating-projects/jenkinsfile/