使用分支参数构建管道

Chr*_*ite 18 jenkins jenkins-pipeline

我似乎无法创建构建特定分支的Jenkins管道作业,其中该分支是构建参数.

这是一些配置截图:

param配置 (我尝试过使用Git参数和字符串参数,结果相同)

分支配置 (我试过$BRANCH_NAME_PARAM,${BRANCH_NAME_PARAM}而且${env.BRANCH_NAME_PARAM},所有的变化同样的结果)

在此输入图像描述

和构建日志:

hudson.plugins.git.GitException: Command "git fetch --tags --progress origin +refs/heads/${BRANCH_NAME_PARAM}:refs/remotes/origin/${BRANCH_NAME_PARAM} --prune" returned status code 128:
stdout: 
stderr: fatal: Couldn't find remote ref refs/heads/${BRANCH_NAME_PARAM}

    at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:1970)
Run Code Online (Sandbox Code Playgroud)

我显然做错了什么 - 关于什么的任何想法?

Chr*_*ite 34

https://issues.jenkins-ci.org/plugins/servlet/mobile#issue/JENKINS-28447

看起来它与轻量级结账有关.如果我在配置中取消选择此选项,我的参数变量将被解析


小智 10

结合 VonC 答案的示例更详细一点

1 . 配置名为 BRANCH 的扩展选择参数:

  • 指定分隔符
  • 指定 groovy 脚本或 groovy 文件的路径:
def command = "git ls-remote -h $gitURL"
def proc = command.execute()

proc.waitFor()         

if ( proc.exitValue() != 0 ) {
   println "Error, ${proc.err.text}"
   System.exit(-1)
}     

def branches = proc.in.text.readLines().collect {
it.replaceAll(/[a-z0-9]*\trefs\/heads\//, '') 
}   
return branches.join(",")
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

2 . 设置要构建的分支:$BRANCH

在此处输入图片说明

3 . 在 Jenkins 作业配置的“Pipeline”部分禁用“Lightweight checkout”复选框:

在此处输入图片说明

否则作业将失败并显示以下消息:“stderr:fatal: 找不到远程引用 refs/heads/${BRANCH”}”

4 . Build with parameter 执行 groovy 脚本,然后您将获得分支的下拉列表

  • 这不是 Git Parameter Plugin 已经给你的吗?我得到了我当前工作的下拉列表,其中包含所有分支机构的列表 (2认同)

Kan*_*nth 6

我已经尝试过上述解决方案,但它对我不起作用。我选择了一种稍微不同的方法。我发帖是因为它会对将来的某人有所帮助。

  1. Goto 配置管道作业。
  2. 选中选项“该项目已参数化”
  3. 添加 git 参数。注意:如果没有显示该选项,请进入管理插件并安装git参数插件。
  4. 我的管道配置看起来像 在此输入图像描述
  5. 取消选中轻量级签出并更新管道部分中的“要构建的分支”。 在此输入图像描述
  6. 保存配置。