【问题标题】:Cannot return the file value as list in jenkins active choice reference parameter script in jenkins无法将文件值作为詹金斯詹金斯活动选择参考参数脚本中的列表返回
【发布时间】:2021-07-02 05:35:24
【问题描述】:

您好,我有以下代码作为我的 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) 脚本中的值。帮帮我!!谢谢

【问题讨论】:

    标签: jenkins jenkins-pipeline jenkins-plugins jenkins-groovy jenkins-cli


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-30
      • 1970-01-01
      • 1970-01-01
      • 2020-07-04
      • 1970-01-01
      • 2020-03-30
      • 1970-01-01
      相关资源
      最近更新 更多