[英]Add gitparameter in jenkins pipeline
執行管道時我需要 userInput。 輸入將是特定存儲庫的 git 分支。 由於我們可以在 jenkins 作業中配置 gitparameter,我們如何在 jenkins 管道中配置它。 我得到的管道代碼如下>
stage 'promotion'
def userInput = input(
id: 'userInput', message: 'Let\'s promote?', parameters: [
[$class: 'GitParameterDefinition', description: 'Environment', name: 'env',type: 'Branch'],
[$class: 'TextParameterDefinition', defaultValue: 'uat1', description: 'Target', name: 'target']
])
echo ("Env: "+userInput['env'])
echo ("Target: "+userInput['target'])
我得到的只是一個空的輸入框,如何使用管道本身的 git 存儲庫配置它。
你可以這樣做:
node {
stage ('input') {
def userInput = input message: 'enter git details',
parameters: [
string(defaultValue: 'dev', description: 'branch name', name: 'branch'),
string(defaultValue: '', description: 'repo url', name: 'url')
]
git branch: userInput['branch'], credentialsId: 'creds', url: userInput['url']
}
}
有一個List Git Branches Parameters Plugin應該可以工作:
這個插件增加了從配置為構建參數的 git 存儲庫中選擇分支、標簽或修訂的能力。 與 Git Parameter Plugin 不同,此插件需要定義一個 git 存儲庫,而不是從您的項目中讀取 GIT SCM 配置
我通過使用不需要額外插件的git command
和ChoiceParameter
解決了這個問題。 然而,這有點黑客,但它有效:
gitUrl = "https://whateverIsyourscmurl"
//checkout in order to be able to execute git commands
checkout([
$class: 'GitSCM',
branches: [[name: '*/master']],
credentialsId: 'bitbucket.service.user',
userRemoteConfigs: [
[url: gitUrl,
noTags: false]
]
])
def selectedTag = "1.0.0"
//get all git tags sorted
command = "git tag --sort=-v:refname"
gitlog = sh(returnStdout: true, script: command).trim() //or bat() in windows
def gitTags = gitlog.tokenize("\n");
//as there is no default value for "ChoiceParameter" we jus put the found element on top
gitTags = gitTags.minus([selectedTag]);
gitTags.remove(0) // gitlog also shows the command itself so remove the first element
gitTags.add(0, selectedTag)
ChoiceParameterDefinition currentTag =
new ChoiceParameterDefinition(
'currentTag',
gitTags as String[],
"Git tag")
returnValue = input message: 'Create workorder for ' + currentStage + ' ?',
parameters: [currentTag],
submitterParameter: 'approver'
description: "whatever"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.