How to validate the transformer of a job template using the Script Console?

Article ID:115002011431
1 minute readKnowledge base

Issue

  • As a Jenkins admin, I would like to validate the job transformer via Script Console

Environment

  • CloudBees Jenkins Enterprise - Managed controller (CJEMM) CloudBees Jenkins Template Plugin

Resolution

Script

The following script illustrates how to solve the issue. For Case A the transformer is OK whereas for Case B not.

Notes:

  1. Please make a backup of your $JENKINS_HOME before running it.

  2. Overall - RunScripts (admin) permission is needed in order to run it.

import com.cloudbees.hudson.plugins.modeling.ModelList
import com.cloudbees.hudson.plugins.modeling.impl.jobTemplate.InstanceFromJobTemplate
import com.cloudbees.hudson.plugins.modeling.impl.jobTemplate.JobPropertyImpl

// getAllItems type JobTemplate and then filtering by item name
def MyTemplate1= Jenkins.instance.getAllItems(com.cloudbees.hudson.plugins.modeling.impl.jobTemplate.JobTemplate).find{job-> job.name=="My Template Example 1"}
// getItem() needs full name as described in https://javadoc.jenkins.io/hudson/model/Item.html#getFullName
def MyTemplate2= ModelList.get().getItem("SUPPORT-Team/Templates/My Template Example 2")

println "CASE A: Transformer OK"
println "----------------------"

try{
    MyTemplate1.transformer.compileTemplate()
    println "[INFO] : $MyTemplate1.name transformer is OK"
}catch (Exception e){
    println "[ERROR] :"+ e.toString()
}

println "\n\n"
println "CASE B: Transformer with Errors"
println "----------------------"
try{
    MyTemplate2.transformer.compileTemplate()
    println "[INFO] : $MyTemplate2.name transformer is OK"
}catch (Exception e){
    println "[ERROR] :"+ e.toString()
}

Console Output

CASE A: Transformer OK
----------------------
[INFO] : My Template Example 1 transformer is OK



CASE B: Transformer with Errors
----------------------
[ERROR] :groovy.lang.GroovyRuntimeException: Failed to parse template script (your template may contain an error or be trying to use expressions not currently supported): startup failed:
SimpleTemplateScript28.groovy: 83: unexpected token: & @ line 83, column 41.
   lease_to_Artifactory=="Yes" &&& !!! bla bla
                                 ^

1 error

Tested product/plugin versions

The latest update of this article was tested with:

  • CloudBees Jenkins Template: 4.33

This article is part of our Knowledge Base and is provided for guidance-based purposes only. The solutions or workarounds described here are not officially supported by CloudBees and may not be applicable in all environments. Use at your own discretion, and test changes in a safe environment before applying them to production systems.