我正在努力将动态选择参数包含到我的声明性管道中。我尝试了来自各个地方的许多不同建议,但这些建议并没有真正帮助,因为我现在完全迷失了方向:-)
为获得所需的功能:我为jenkins管道提供了一个Jenkinsfile(声明性)。我想要一个参数,使我可以从git存储库中选择要在管道中使用的特定分支。
jenkinsfile的基本结构:
pipeline {
agent { node { label "XXX-XXX-XXX" } }
options {
gitLabConnection('XXX-XXX-XXX')
}
stages {
stage("STAGE A") {
parallel{
stage("A"){
steps {
// DO STUFF
}
}
stage("B"){
steps {
// DO STUFF
}
}
}
}
}
}
我尝试在管道中使用以下代码段添加参数。我无法动态填写选择列表。它为空或生成错误。
parameters {
choice(
name: 'CHOICE_2',
choices: 'choice_1\nchoice_2\nchoice_3\nchoice_4\nchoice_5\nchoice_6',
description: 'CHOICE_2 description',
)
}
或者,我尝试在管道声明之外添加以下内容。我以脚本变体为例留了一个。再次在这里,我无法填充选择列表。该字段为空:
node {
label "XXX-XXX-XXX"
properties([
parameters([
[$class: 'ChoiceParameter',
choiceType: 'PT_SINGLE_SELECT',
description: 'The names',
filterLength: 1,
filterable: true,
name: 'Name',
randomName: 'choice-parameter-5631314439613978',
script: [
$class: 'GroovyScript',
script: [
classpath: [],
sandbox: false,
script: '''
def gettags = "git ls-remote git@XXX-XXX-XXX.git".execute()
def tagList = []
def t1 = []
String tagsString = null;
gettags.text.eachLine {tagList.add(it)}
for(i in tagList)
t1.add(i.split()[1].replaceAll('\\^\\{\\}', ''))
t1 = t1.unique()
tagsString = StringUtils.join((String[])t1, ',');
return tagsString
'''
]
]
],
])
])
}
在这一点上,我尝试了太多不同的方法,并且想退后一步。
有人可以用一种方法和一些提示或资源来支持我吗?
非常感谢,
答案 0 :(得分:0)
我建议您使用远程分支机构的lisf构建文件,然后再批准一个阶段并选择正确的分支机构-您是否尝试过多分支管道?也许适用于您的用例...
pipeline {
agent { node { label "XXX-XXX-XXX" } }
options {
gitLabConnection('XXX-XXX-XXX')
skipDefaultCheckout() //this will avoid to checkout the code by defaul
}
stages {
stage("Get Branches from Git Remote"){ // you might need to tweak the list retrieved from git branches cmd
steps{
withCredentials([usernamePassword(credentialsId: 'git-credential', passwordVariable: 'key', usernameVariable: 'gitUser')]) {
sh """
mkdir git_check
cd git_check
git clone https://${gitUser}:${key}@${GIT_URL} .
git branch -a | grep remotes > ${WORKSPACE}/branchesList.txt
cd ..
rm -r git_check
"""
}
}
}
}
stage('User Input'){
steps{
listBranchesAvailable = readFile 'branchesList.txt'
env.branchToDeploy = timeout (time:1, unit:"HOURS") {
input message: 'Branch to deploy', ok: 'Confirm!',
parameters: [choice(name: 'Branches Available:', choices: "${listBranchesAvailable }", description: 'Which branch do you want to build?')]
}
}
}
stage('Checkout Code'){
steps{
cleanWs() // ensure workspace is empty
git branch: "${branchToDeploy}", changelog: false, credentialsId: 'gitcredential', poll: false, url: "${GIT_URL}" //checks out the right code branch
}
}
stage("STAGE A") {
parallel{
stage("A"){
steps {
// DO STUFF
}
}
stage("B"){
steps {
// DO STUFF
}
}
}
}
}
}