不能 return 文件值作为 jenkins 中的列表 active choice reference parameter script in jenkins

Cannot return the file value as list in jenkins active choice reference parameter script in jenkins

你好,我有以下代码作为我的 Jenkinsfile。

pipeline {


agent any
  parameters{
    string(name: 'IP_ADDRESS', defaultValue: '',  description: 'Enter server IP address')
    string(name: 'USERNAME', defaultValue: '', description: '')
    string(name: 'PASSWORD', defaultValue: '', description: '')
  }
  environment{
    VERSION='1.3.0'
    
  }
  stages {
    stage('Build') {
      steps {
    echo 'Built'
    
    sh(script:"python sshMac.py ${IP_ADDRESS} ${USERNAME} ${PASSWORD}", returnStatus: true, returnStdout: true)
  }
}

stage('Test') {
  steps {
    script{
      
      env.FILENAME = readFile 'macAdds.properties'
      env.FILEDATA = FILENAME.tokenize( "," )
    
      input message: 'Please choose one',
        parameters: [
        [$class: 'ChoiceParameter', 
                                choiceType: 'PT_SINGLE_SELECT', 
                                description: 'Select the Environemnt from the Dropdown List', 
                                filterLength: 1, 
                                filterable: false, 
                                name: 'ENVIRONMENT', 
        script: [$class: 'GroovyScript',
            fallbackScript: [
                classpath: [], 
                sandbox: true, 
                script: 'return ["ERROR"]'
            ],
            script: [
                classpath: [], 
                sandbox: true, 
                script: """
                   
              return['1', '2', '3', '4']
                    
                """.stripIndent()
            ]
        ]
                            ],
                            [$class: 'ChoiceParameter', 
                                choiceType: 'PT_SINGLE_SELECT', 
                                description: 'Select the Environemnt from the Dropdown List', 
                                filterLength: 1, 
                                filterable: false, 
                                name: 'ENVIRONMENT2', 
        script: [$class: 'GroovyScript',
            fallbackScript: [
                classpath: [], 
                sandbox: true, 
                script: 'return ["ERROR"]'
            ],
            script: [
                classpath: [], 
                sandbox: true, 
                script: """
                   
              return['1', '2', '3', '4', '5']
                    
                """.stripIndent()
            ]
        ]
                            ],
      
    [$class: 'CascadeChoiceParameter', 
        choiceType: 'PT_SINGLE_SELECT',
        description: 'Select a choice',
        filterLength: 1,
        filterable: false,
        name: 'choice1',
        referencedParameters: 'ENVIRONMENT',
        script: [$class: 'GroovyScript',
            fallbackScript: [
                classpath: [], 
                sandbox: true, 
                script: 'return ["ERROR"]'
            ],
            script: [
                
                classpath: [], 
                sandbox: true, 
                script: """
                def tokens = ${FILENAME}.tokenize(',')

                    switch(ENVIRONMENT) {
                      case "1":
                      return ${env.FILEDATA}
                      case "2":
                      return ${env.FILEDATA}
                      case "3":
                      return ${env.FILEDATA}
                      case "4":
                      return ${env.FILEDATA}
                      }
                  
                    
                  """
            ]
        ]
    ],

      text(name: 'vertical', defaultValue: "${FILENAME}")
                    
                    
]

    }
    echo 'Testing'
    echo "${FILENAME}"
    echo "${FILEDATA}"
    
    
  }
}

stage('Deploy') {
  steps {
    echo 'Deploying'
   
  }
}

} }

所以我在这里从文件 (FILENAME) 中获取了值,并将它们拆分为列表 (FILEDATA)。它工作得很好,它也打印出来并回显。实际上它 return 是来自 macAdds.properties 的一组 Mac 地址,例如 [ff:ff:ee:ee:ee:ff, ff:ee:dd:aa:bb:ee, ff:ee:aa:cc:bb:dd].

但是在我的 活动选择参考参数 (choice1) 中,当我 return 在我的 switch case 中设置 FILEDATA 值作为单个 select 它无法确定它将 运行 返回脚本的位置并给我一个错误。我已经测试了 returning 一些手动给 switch 语句的随机值,它可以完美地工作。我想知道为什么我不能 return 我的 活动选择参考参数 (choice1) 脚本中的值。帮帮我!!谢谢

好吧,我已经找到了这个解决方案的答案。当您使用 groovy 沙箱值时,您可以不受限制地做很多事情。但是您必须从 Manage Jenkins => Script Approval 手动批准脚本。希望这会帮助别人。谢谢!