找不到这样的字段:运行我的 jenkinsfile 时字段 java.lang.String sinput 错误

Vic*_*cky 4 groovy jenkins jenkins-pipeline jenkins-declarative-pipeline

获取No such field found: field java.lang.String sinput错误而运行我的Jenkinsfile。

我开发了一个 Jenkinsfile,它可以接受用户输入,并进一步在远程机器上运行一个命令,将用户输入作为变量

stages {
    stage("Interactive_Input") {
        steps {
            script {
                def apiinput
                def userInput = input(
                        id: 'userInput', message: 'This is my project',
                        parameters: [
                                string(defaultValue: 'None',
                                        description: 'Enter the name of the service',
                                        name: 'sinput'),
                                
                        ])
                // Save to variables. Default to empty string if not found.
                apiinput = userInput.sinput?:''
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

mke*_*erz 7

解决方案

apiinput = userInput?:'' 将消除您的问题。


解释:

sinput错误地访问了您的变量。您id: 'userInput'确实直接代表用户输入的变量。您尝试访问调用时不存在的变量apiinput = userInput.sinput?:''

引自 Source3:

如果只列出一个参数,它的值将成为输入步的值。如果列出了多个参数,则返回值将是一个以参数名称为键的映射。如果未请求参数,则该步骤在获得批准后不返回任何内容。

您有 1 个参数,因此它成为输入步骤的值。没有创建地图。

云蜂 1 | 云蜂 2 | 管道输入步骤


小智 6

好吧...我遇到了这样的问题,就我而言,这是我使用变量的方式,当我确实喜欢$VARIABLE它不起作用时,我遇到了与此处描述的错误类似的错误。然而,当我这样做时${VARIABLE}->一切都有效了!