Testing Jenkins Job DSL

Hi All
I’m currently testing my DSL scripts using gradle based on this example.

With the following details:
Gradle Version: 5.3.1
Job DSL Version: 1.70
Jenkins Version: 2.138.2

But having below error:

JobScriptsSpec > test script param_job.groovy STANDARD_OUT
    Processing provided DSL script

JobScriptsSpec > test script param_job.groovy FAILED
    Expected no exception to be thrown, but got 'javaposse.jobdsl.dsl.DslScriptException'
        at spock.lang.Specification.noExceptionThrown(Specification.java:118)
        at JobScriptsSpec.test script #file.name(JobScriptsSpec.groovy:26)

        Caused by:
        javaposse.jobdsl.dsl.DslScriptException: (script, line 15) No such property: JENKINS_INSTANCE_NAME for class: javaposse.jobdsl.dsl.jobs.WorkflowJob
            at javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScriptEngine(AbstractDslScriptLoader.groovy:114)
            at javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScripts_closure1(AbstractDslScriptLoader.groovy:61)
            at groovy.lang.Closure.call(Closure.java:411)
            at groovy.lang.Closure.call(Closure.java:427)
            at javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScripts(AbstractDslScriptLoader.groovy:46)
            at javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScript(AbstractDslScriptLoader.groovy:87)
            at JobScriptsSpec.test script #file.name(JobScriptsSpec.groovy:23)

            Caused by:
            groovy.lang.MissingPropertyException: No such property: JENKINS_INSTANCE_NAME for class: javaposse.jobdsl.dsl.jobs.WorkflowJob
                at script.run_closure1(script:15)
                at groovy.lang.Closure.call(Closure.java:411)
                at groovy.lang.Closure.call(Closure.java:427)
                at javaposse.jobdsl.dsl.JobParent.processItem(JobParent.groovy:239)
                at javaposse.jobdsl.dsl.JobParent.pipelineJob(JobParent.groovy:87)
                at script.run(script:1)
                at javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScript(AbstractDslScriptLoader.groovy:138)
                at javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScriptEngine(AbstractDslScriptLoader.groovy:108)
                ... 6 more

When testing below script:

pipelineJob('param-job') {
    description('This job is parameterized')
    parameters {
        stringParam('DevelopBranch', 'develop', 'The develop branch from development.')
        stringParam('RepoName', 'test_repo', 'The name of the repository')
        stringParam('NewParam', 'param1', 'The name of the parameter')
    }

    if (JENKINS_INSTANCE_NAME == 'test') {
        triggers {
            cron('H 6 * * *')
        }
    }


    logRotator {
        numToKeep(10)
        daysToKeep(7)
    }
    // definition {
    //     cps {
    //         script(readFileFromWorkspace('./vars/param.groovy'))
    //     }

    //     }
}

By the way JENKINS_INSTANCE_NAME is define in the global variables.

Can anyone help me fix this error?
Thanks in advance.

Are you still running Jenkins 2.138.2? This release is 6 years old and not supported anymore.

Yes, unfortunately it is the current version the project is using.

But to note, I also tried running it on local jenkins with version 2.447 and yet the same error.

I know that based on this document there is no property
like JENKINS_INSTANCE_NAME.
https://jenkinsci.github.io/job-dsl-plugin/#path/pipelineJob

But this is defined as Global variables.
Is there a workaround on this just to make the testing successful?
Thanks

I don’t think that you can use environment variables in such a way, but I never used them so I do not have a better solution.