在Groovy中访问Jenkins参数

ifsvaxew  于 2022-12-03  发布在  Jenkins
关注(0)|答案(1)|浏览(221)

如何在Groovy中访问参数的值?这似乎是一个微不足道的问题,但却给我带来了许多小时的痛苦。

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';
                    };
                """
            }
        }

当我运行该代码时,我得到以下错误:

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

因此,出于某种原因,Groovy不解析${ENTERPRISESERVER}。我尝试过使用$ENTERPRISESERVER,但它无法编译:

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

我做错了什么?
我还尝试了以下问题的解决方案:
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?

zi8p0yeb

zi8p0yeb1#

请尝试以下操作之一。

def targetServer = "${ENTERPRISESERVER}"
def targetServer = params.ENTERPRISESERVER

相关问题