Jenkins Job DSL 中的合并参数

Coalesce parameters in Jenkins Job DSL

我在作业 DSL 中定义了一个 pipelineJob。

它运行一个 pipeline/Jenkinsfile,它从 git 中检出。

我希望人们能够输入 git 分支,从中提取 Jenkinsfile -(即在 stringParam 中) - 或者,如果他们没有输入分支,则默认为我在 choiceParam 中设置的分支(即这将是 'develop' 或 'master')

这不起作用:

pipelineJob('some-job') {
  parameters {
    choiceParam('gitCreds', [gitCreds], 'Stash credential')
    stringParam('gitUrl', 'https://some-repo.git', 'URL for the Stash repo')
    stringParam('gitBranchOverride', '', 'Type in some feature branch here if you wish')
    choiceParam('gitBranch', ['develop'], '...otherwise the job should default to a branch here')
 }
  definition {
    cpsScm {
      scm {
        git {
          branch('$gitBranchOverride' ?: '$gitBranch')
          extensions {
            wipeOutWorkspace()
          }
          remote {
            credentials(gitCreds)
            url ('$gitUrl')
          }
        }
      }
    }
  }
}

如果我在 gitBranchOverride 中输入一个值,它会起作用,但如果我不这样做,它似乎会枚举所有分支,并随机检查一个 - 即它不尊重 git分支机构

不知道我是否正确理解了您的问题,但这就是我创建管道作业的代码:

def git_branch = getBinding().getVariable('GIT_BRANCH')
def gitrepo = "ssh://git@some.git.repo/somerepo.git"
def credential_id = "awesomecredentials"
pipelineJob("MyAwesomeJob") {
    description("""This job is awesome\n\n__input__:\n* My parameter\n* Branch\n\n__branch__: ${git_branch}""")
    parameters {
        stringParam(name='MyParameter', description='AwesomeParameterHere')
        stringParam('branch', defaultValue='origin/develop', description='Branch to build')
    }
    definition {
        cpsScm {
            scm {
                git {
                    branch('$branch')
                    remote {
                        url("gitrepo")
                        credentials(credential_id)
                    }
                }
                scriptPath("jenkins/my_awesome_pipeline/Jenkinsfile")
            }
        }
    }
}

有了这个,我的工作创建了一个分支参数,并选择了默认参数。