dynamic填充jenkinsselect参数与Git分支在指定的回购

我有一个参数化的Jenkins作业,需要在特定的Git仓库中input特定的Git分支。 目前这个参数是一个string参数。

有没有办法使这个参数的select参数,并dynamic填充下拉列表与Git分支? 我不希望每次创build新分支时都需要通过手动configuration下拉菜单来保持此选项参数。

扩展select参数插件将允许您从文件中读取选项。

当然,现在还有一个问题:如何确保文件是最新的(可以通过post-commit钩子完成)并传播给所有用户(可以通过将其放置在共享文件服务器)。 但是可能有更好的解决办法。

我尝试了几个在这个链接中提到的答案,但不知道如何告诉jenkins用户select的分支。 正如我以前在上面的评论中提到的,我已经将分支select器字段留空。

但是,在进一步的调查中,我发现了另一种做同样的事情 – https://wiki.jenkins-ci.org/display/JENKINS/Git+Parameter+Plugin我发现这个方法简单得多,configuration!;

这是我configuration的 –

  1. 安装了git参数插件
  2. 检查了“这个构build是参数化的”,并添加了一个“Git参数”
  3. 添加了以下值: Git参数插件配置在作业中

  4. 然后在作业的git SCM部分添加了“Name”部分中提到的相同的值,就好像它是一个环境variables。 (如果你仔细阅读这个git参数插件的帮助,你会意识到这一点) 分支选择器

在这之后,我刚刚运行了构build,select了我的分支(Jenkins在构build之前检查了这个分支),并且成功完成了构build,并select了我指定的分支。

我能够使用Jenkinsdynamic参数插件实现这个结果。 我使用dynamicselect参数选项,对于select脚本,我使用了以下内容:

 proc1 = ['/bin/bash', '-c', "/usr/bin/git ls-remote -h ssh://user@server.com/path/to/repo.git"].execute() proc2 = ['/bin/bash', '-c', "awk '{print \$2}'"].execute() proc3 = ['/bin/bash', '-c', "sed s%^refs/heads%origin%"].execute() all = proc1 | proc2 | proc3 String result = all.text String filename = "/tmp/branches.txt" boolean success = new File(filename).write(result) def multiline = "cat /tmp/branches.txt".execute().text def list = multiline.readLines() 

使用“Git Parameter Plugin”非常简单。

添加名称,如“SELECT_BRANCH”##确保这个variables,因为这将在稍后使用。 然后参数types:分支

然后伸手到SCM:select:Git和分支说明符:$ {SELECT_BRANCH}

要validation,请在jenkins的shell下执行:

echo $ {SELECT_BRANCH}

env.enter图像说明在这里

在这里输入图像说明

扩展@ malenkiy_scot的答案。 我创build了一个新的jenkins作业来构buildExtended Choice Plugin使用的文件。

你可以做下面的事情(我把它作为jenkins中的执行shell步骤,但是你可以在脚本中完成):

 git ls-remote git@github.com:my/repo.git |grep refs/heads/* >tmp.txt sed -e 's/.*refs\/heads\///' tmp.txt > tmp2.txt tr '\n' ',' < tmp2.txt > tmp3.txt sed '1i\branches=' tmp3.txt > tmp4.txt tr -d '\n' < tmp4.txt > branches.txt 

然后使用Artifact部署者插件将该文件推送到url中的共享位置,然后使用Extended Choice插件中的“http://localhost/branches.txt”作为url。 奇迹般有效。

你可以用malenkiy_scot和一个简单的php脚本来扩展select参数插件,如下所示(假设你有一个服务器来部署php脚本,你可以从Jenkins机器上点击)

 <?php chdir('/path/to/repo'); exec('git branch -r', $output); print('branches='.str_replace(' origin/','',implode(',', $output))); ?> 

要么

 <?php exec('git ls-remote -h http://user:pass@repo.git', $output); print('branches='.preg_replace('/[a-z0-9]*\trefs\/heads\//','',implode(',', $output))); ?> 

有了第一个选项,你需要克隆回购。 第二个你不这样做,但在这两种情况下,你需要安装在托pipe你的PHP脚本的服务器上的git。 白色的任何这些选项,它是完全dynamic的,你不需要build立一个列表文件。 只需将URL添加到扩展select参数“属性文件”字段中的脚本即可。

是的,我写了一个小技巧的脚本,你应该添加一个“dynamicselect参数”到你的工作,并根据你的需要定制以下的groovy脚本:

 #!/usr/bin/groovy def gitURL = "git repo url" def command = "git ls-remote --heads --tags ${gitURL}" def proc = command.execute() proc.waitFor() if ( proc.exitValue() != 0 ) { println "Error, ${proc.err.text}" System.exit(-1) } def text = proc.in.text # put your version string match def match = /<REGEX>/ def tags = [] text.eachMatch(match) { tags.push(it[1]) } tags.unique() tags.sort( { a, b -> def a1 = a.tokenize('._-') def b1 = b.tokenize('._-') try { for (i in 1..<[a1.size(), b1.size()].min()) { if (a1[i].toInteger() != b1[i].toInteger()) return a1[i].toInteger() <=> b1[i].toInteger() } return 1 } catch (e) { return -1; } } ) tags.reverse() 

我的情况下,版本string是在以下格式XXXX,并可能有格式XXX用户名等用户分支…所以我不得不写我自己的sortingfunction。 这是我第一个时髦的脚本,所以如果有更好的方法做事情,我想知道。

如果你的工作没有直接使用“源代码pipe理” (同样是“Git Parameter Plugin”),但是仍然可以访问本地(克隆的)git仓库, 那么下面的groovy脚本将会很有用:

 import jenkins.model.Jenkins def envVars = Jenkins.instance.getNodeProperties()[0].getEnvVars() def GIT_PROJECT_PATH = envVars.get('GIT_PROJECT_PATH') def gettags = "git ls-remote -t --heads origin".execute(null, new File(GIT_PROJECT_PATH)) return gettags.text.readLines() .collect { it.split()[1].replaceAll('\\^\\{\\}', '').replaceAll('refs/\\w+/', '') } .unique() 

请参阅此处的完整说明: https : //stackoverflow.com/a/37810768/658497