0

How can I access the value of a parameter in Groovy? This seems like a trivial problem, but it has caused me to many hours of pain.

dslFactory.job(name) {

            parameters {
                activeChoiceParam('ENTERPRISESERVER') {
                    description('')
                    choiceType('SINGLE_SELECT')
                    groovyScript {
                        script("""[
                            "vws-10-persmft",
                            "vws-10-persmft2",
                            "vws-10-persmft3",
                            // "vws-10-persmfe",
                            // "vws-10-persmfe2",
                            "vts-10-perse9",
                            //"vts-10-perse8",
                            //"vts-10-perse7",
                            "vws-10-perskmt5"
                        ]""")
                    }
                }
            }

            steps {
                def targetServer = '${ENTERPRISESERVER}'
                powerShell """
                    Write-Output 'Target Server: $targetServer'
                    Invoke-Command -ComputerName '$targetServer' -ScriptBlock {
                        Restart-Service -Name 'SEEShutdown';
                        Restart-Service -Name 'SEEMonitor';
                    };
                """
            }
        }

When I run that code, I'm getting the following error:

+ ...             Invoke-Command -ComputerName '${ENTERPRISESERVER}' -Scrip ...

So for some reason, Groovy doesn't resolve ${ENTERPRISESERVER}. I have tried using $ENTERPRISESERVER, and it won't compile:

de.akdb.pers.ci.JobScriptsSpec > test script bootstrap.groovy FAILED
    org.spockframework.runtime.UnallowedExceptionThrownError at JobScriptsSpec.groovy:24
        Caused by: javaposse.jobdsl.dsl.DslScriptException at JobScriptsSpec.groovy:21
            Caused by: groovy.lang.MissingPropertyException at JobScriptsSpec.groovy:21

What am I doing wrong?

I also tried the solutions from the following questions:

Jenkins Job DSL: Using parameters in groovyScript in job step

How to access a specific Jenkins job parameter from within a JobDSL?

How to retrieve Jenkins build parameters using the Groovy API?

PandaSITT
  • 37
  • 6
  • If you echo the param does it print the value? `echo "ENTERPRISESERVER: ${params.ENTERPRISESERVER}"` – Pamela Sarkisyan Nov 30 '22 at 16:13
  • @PamelaSarkisyan no, echo apparently doesn't exist in that version. With `println` I'm getting a `groovy.lang.MissingPropertyException` again – PandaSITT Dec 01 '22 at 14:08

2 Answers2

1

In my understanding, the ENTERPRISESERVER is defined for the job your are gonna create, it isn't valid in the dsl code of creating that job.
Which means ENTERPRISESERVER is directly visible and usable in the PowerShell script.
I never used PowerShell, but in BASH it can be accessed thru "$ENTERPRISESERVER".
A shell script inside dsl code should escape param/var dereference with "\$ENTERPRISESERVER".
Have a try!

Andy Jiang
  • 38
  • 4
  • I forgot about this question, I could access the value with `$env:ENTERPRISESERVER` with the dollar sign escaped. So almost the same. ty anyway – PandaSITT Dec 20 '22 at 15:23
0

Try one of the following.

def targetServer = "${ENTERPRISESERVER}"
def targetServer = params.ENTERPRISESERVER
ycr
  • 12,828
  • 2
  • 25
  • 45
  • That didn't work... I'm guessing, it might be, since we are still using Gradle 2.13. I don't know where I can find the other versions from other components, but they won't be up to date ether – PandaSITT Nov 30 '22 at 13:03