使用 SCM 存储库中的文件夹列表填充 jenkinsfile 中的选择参数
Populate choice parameter in jenkinsfile with list of folders in SCM repository
我有一个 Jenkinsfile 驱动管道,用户必须select bitbucket 存储库中的特定文件夹作为目标。我希望动态填充该选择参数下拉列表。
目前,我已经按照这个通用示例对选择参数列表进行了硬编码:
choice(name: 'IMAGE', choices: ['workload-x','workload-y','workload-z'])
我想知道这是否可以从 jenkinsfile 本身内部实现,或者我是否必须为此创建一个特定的 groovy 脚本然后调用它。无论哪种方式,我都有点迷路,因为我对 jenkins 还很陌生,而且才刚刚开始使用 Jenkinsfiles。
一些试错谷歌搜索让我创建了一个 groovy 脚本,其中 returns 存储库中的文件夹名称数组使用 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
本returns结果如下:
Result: [workload-a,workload-b,workload-c,workload-x,workload-y,workload-z]
但是我不确定如何在我的 Jenkinsfile 中调用它来填充下拉列表。
如有任何帮助,我们将不胜感激。
您可以这样做(按照我下面的示例)或使用 Active Choice Jenkins Plugins - 因为它允许一些 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}"
}
}
}
}
我有一个 Jenkinsfile 驱动管道,用户必须select bitbucket 存储库中的特定文件夹作为目标。我希望动态填充该选择参数下拉列表。
目前,我已经按照这个通用示例对选择参数列表进行了硬编码:
choice(name: 'IMAGE', choices: ['workload-x','workload-y','workload-z'])
我想知道这是否可以从 jenkinsfile 本身内部实现,或者我是否必须为此创建一个特定的 groovy 脚本然后调用它。无论哪种方式,我都有点迷路,因为我对 jenkins 还很陌生,而且才刚刚开始使用 Jenkinsfiles。
一些试错谷歌搜索让我创建了一个 groovy 脚本,其中 returns 存储库中的文件夹名称数组使用 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
本returns结果如下:
Result: [workload-a,workload-b,workload-c,workload-x,workload-y,workload-z]
但是我不确定如何在我的 Jenkinsfile 中调用它来填充下拉列表。
如有任何帮助,我们将不胜感激。
您可以这样做(按照我下面的示例)或使用 Active Choice Jenkins Plugins - 因为它允许一些 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}"
}
}
}
}