列出 git 个分支作为声明性管道中的动态参数
List git branches as dynamic parameter in declarative pipeline
我正在努力将动态选择参数包含到我的声明性管道中。我尝试了很多来自各地的不同建议,但这些建议并没有真正帮助,因为我现在完全迷失了方向:-)
对于所需的功能:我有一个用于 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
'''
]
]
],
])
])
}
此时我尝试了太多不同的方法,想退后一步。
任何人都可以通过方法和一些提示或资源来支持我吗?
谢谢并致以最诚挚的问候,
我建议您构建一个包含远程分支列表的文件,之后您可以有一个阶段来批准并选择正确的分支 - 您是否尝试过多分支管道?也许它适用于您的用例...
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
}
}
}
}
}
}
我正在努力将动态选择参数包含到我的声明性管道中。我尝试了很多来自各地的不同建议,但这些建议并没有真正帮助,因为我现在完全迷失了方向:-)
对于所需的功能:我有一个用于 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
'''
]
]
],
])
])
}
此时我尝试了太多不同的方法,想退后一步。
任何人都可以通过方法和一些提示或资源来支持我吗?
谢谢并致以最诚挚的问候,
我建议您构建一个包含远程分支列表的文件,之后您可以有一个阶段来批准并选择正确的分支 - 您是否尝试过多分支管道?也许它适用于您的用例...
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
}
}
}
}
}
}