首页 > 解决方案 > 带有字符串变量问题的Jenkinsfile

问题描述

我有一个Jenkinsfile来自输入文件的端口解析列表,

pipeline {
  parameters {
    string(defaultValue: '4000', name: 'Port', trim: true)
  }
  
  stage('Test') {
    steps {
      script {
        echo 'Testing..'
        script {
          def data = readFile(file: 'list.txt')
          println(data)
          def Port = ""
          data.eachLine { line ->
            def arr = line.tokenize('\t')
            println "port: ${arr[0]}}"   # Prints values
            println "port: arr[0]"         # Prints name of Varible
            build quietPeriod: 2,
            job: 'Downstream_Jenkinsfile_Job',
            parameters: [string(name: 'Port', value: ${arr[0]})]  # <- ERROR HERE
          }
        }
      }
    }
  }
}

该行parameters: [string(name: 'Port', value: ${arr[0]})]给出错误

[Pipeline] End of Pipeline
java.lang.NoSuchMethodError: No such DSL method '$' found among steps [archive,
...
at org.kohsuke.groovy.sandbox.GroovyInterceptor.onMethodCall(GroovyInterceptor.java:23)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:157)

我试过:
parameters: [string(name: 'Port', value: ${arr[0]}]
parameters: [string(name: 'Port', value: arr[0]]

是什么导致了这个问题?我打字,没有运气。谢谢

标签: groovyjenkins-pipelinejenkins-groovygroovydsl

解决方案


它一定要是

build quietPeriod: 2,
            job: 'Downstream_Jenkinsfile_Job',
            parameters: [string(name: 'Port', value: "${arr[0]}")]

$如果在您的示例中""和您的示例中您留下了参数值,则管道可以插入该值,${arr[0]}但它必须像"${arr[0]}"


推荐阅读