Groovy 使用SCM存储库中的文件夹列表填充Jenkins文件中的choice参数

Groovy 使用SCM存储库中的文件夹列表填充Jenkins文件中的choice参数,groovy,jenkins-pipeline,jenkins-groovy,Groovy,Jenkins Pipeline,Jenkins Groovy,我有一个Jenkins文件,它驱动一个管道,用户必须在bitbucket repo中选择一个特定的文件夹作为目标。我想动态填充选择参数下拉列表 目前,我已根据以下通用示例对选项参数列表进行了硬编码: choice(name: 'IMAGE', choices: ['workload-x','workload-y','workload-z']) 我想知道这是否可以从Jenkins文件本身中实现,或者是否需要为此创建一个特定的groovy脚本,然后调用它。不管怎样,我都有点迷路了,因为我对詹金斯很

我有一个Jenkins文件,它驱动一个管道,用户必须在bitbucket repo中选择一个特定的文件夹作为目标。我想动态填充选择参数下拉列表

目前,我已根据以下通用示例对选项参数列表进行了硬编码:

choice(name: 'IMAGE', choices: ['workload-x','workload-y','workload-z'])
我想知道这是否可以从Jenkins文件本身中实现,或者是否需要为此创建一个特定的groovy脚本,然后调用它。不管怎样,我都有点迷路了,因为我对詹金斯很陌生,而且才刚刚开始使用詹金斯文件

通过谷歌搜索,我可以创建一个groovy脚本,使用json slurper返回存储库中的文件夹名称数组:

import groovy.json.JsonSlurper
import jenkins.model.Jenkins

def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
        com.cloudbees.plugins.credentials.Credentials.class,
        Jenkins.instance,
        null,
        null
    );

def credential = creds.find {it.id == "MYBITBUCKETCRED"}

if (!credential) { return "Unable to pickup credential from Jenkins" }
username = credential.username
pass = credential.password.toString()

def urlStr = "https://bitbucket.mydomain.com/rest/api/1.0/projects/MYPROJECT/repos/MYREPO/browse/"

HttpURLConnection conn = (HttpURLConnection) new URL(urlStr).openConnection()
String encoded = Base64.getEncoder().encodeToString((username + ":" + pass).getBytes("UTF-8"));
conn.setRequestProperty("Authorization", "Basic " + encoded);
conn.connect();

def slurper = new JsonSlurper() 
def browseList = slurper.parseText(conn.getInputStream().getText())

def dfList = browseList.children.values.path.name.findAll {it.contains('workload-')}

return dfList
这将返回如下结果:

Result:   [workload-a,workload-b,workload-c,workload-x,workload-y,workload-z]
然而,我不确定如何在我的Jenkins文件中调用它来填充下拉列表

任何帮助都将不胜感激。

您可以这样做(遵循下面的示例)或利用-因为它允许一些groovy脚本为您的选择做准备

注意-首次运行后,选项参数将可用

def choiceArray = []
node {
    checkout scm
    def folders = sh(returnStdout: true, script: "ls $WORKSPACE")
    
    folders.split().each {
        //condition to skip files if any
        choiceArray << it
    }
}

pipeline {
    agent any;
    parameters { choice(name: 'CHOICES', choices: choiceArray, description: 'Please Select One') }
    stages {
        stage('debug') {
            steps {
                echo "Selected choice is : ${params.CHOICES}"
            }
        }
    }
}
def choiceArray=[]
节点{
校验scm
def folders=sh(returnStdout:true,脚本:“ls$WORKSPACE”)
folders.split()。每个{
//条件以跳过文件(如果有)
choiceArray您可以这样做(遵循下面的示例)或利用-因为它允许一些groovy脚本为您的选择做准备

注意-首次运行后,选项参数将可用

def choiceArray = []
node {
    checkout scm
    def folders = sh(returnStdout: true, script: "ls $WORKSPACE")
    
    folders.split().each {
        //condition to skip files if any
        choiceArray << it
    }
}

pipeline {
    agent any;
    parameters { choice(name: 'CHOICES', choices: choiceArray, description: 'Please Select One') }
    stages {
        stage('debug') {
            steps {
                echo "Selected choice is : ${params.CHOICES}"
            }
        }
    }
}
def choiceArray=[]
节点{
校验scm
def folders=sh(returnStdout:true,脚本:“ls$WORKSPACE”)
folders.split()。每个{
//条件以跳过文件(如果有)

choiceArray这是一种干净利落的方式,效果非常好。谢谢Samit。这是一种干净利落的方式,效果非常好。谢谢Samit。