【问题标题】:Add gitparameter in jenkins pipeline在 jenkins 管道中添加 git 参数
【发布时间】:2018-05-19 04:39:45
【问题描述】:

执行管道时我需要用户输入。 输入将是特定存储库的 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 存储库对其进行配置。

【问题讨论】:

  • 您是否尝试根据从用户输入中获得的分支名称克隆某个存储库?
  • @PrasadMarne 是的,正确
  • GitParameterDefinition 不能用作输入类型。所以我发布了另一种实现你想要做的事情的方法。希望对你有帮助。
  • 根据jenkins.io/doc/pipeline/steps/pipeline-input-step gitParameter 是可用的,但是它说“此参数将在构建时显示一个选择Git标签(或修订号)的选项,该标签为参数化构建设置参数。”到目前为止,我无法让它作为input() 的一部分工作

标签: git variables jenkins input jenkins-pipeline


【解决方案1】:

你可以这样做:

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']
   }
}

【讨论】:

  • 这将要求分支作为字符串输入,我希望从 git 中自动检索分支。我实际上是通过添加主动选择插件并在此处添加步骤:apassionatechie.wordpress.com/2017/12/06/…
  • 我认为您的问题是“输入将是特定存储库的 git 分支”
  • 是的,存储库将被添加到管道中,但应动态检索分支
【解决方案2】:

有一个List Git Branches Parameters Plugin 应该可以工作:

此插件增加了从配置为构建参数的 git 存储库中选择分支、标签或修订的功能。与 Git Parameter Plugin 不同,此插件需要定义一个 git 存储库,而不是从您的项目中读取 GIT SCM 配置

我通过使用 git commandChoiceParameter 解决了它略有不同,不需要额外的插件。然而,这有点像 hack,但它确实有效:

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"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多