首页 > 解决方案 > 无法将文件值作为詹金斯詹金斯活动选择参考参数脚本中的列表返回

问题描述

您好,我有以下代码作为我的 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)。它工作得很好,它也能打印出来并回显。实际上它从 macAdds.properties 返回一组 Mac 地址,例如[ff:ff:ee:ee:ee:ff, ff:ee:dd:aa:bb:ee, ff:ee:aa:cc:bb: dd]

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

标签: jenkinsjenkins-pipelinejenkins-pluginsjenkins-groovyjenkins-cli

解决方案


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


推荐阅读