Jenkins可以自动检测并在git仓库中创build新创build的标签吗?

我们的Jenkins CI服务器能够自动检测,部署和构build标签,因为它们是在我们的Github存储库中创build的。

这可能吗?

通过以下configuration,您可以创build所有标签:

  1. 将作业提取标签设置为分支:单击资源库URL下的高级button,然后inputRefspec +refs/tags/*:refs/remotes/origin/tags/*
  2. 使用Branch Specifier */tags/*构build所有标签“分支”
  3. 启用S​​CM轮询,以便作业检测新标签。

这种方法有一个缺点:作业将build立所有的标签,而不仅仅是新添加的标签。 所以创build作业之后,每一个现有的标签都会触发一次。 所以你可能希望这个工作一开始不做任何事情,然后等待所有现有的标签都被处理完毕,然后只为每一个新的标签configuration你想要完成的构build步骤。

由于标签在git中不会改变,因此每个新标签只会触发一次。

为了克服@oberlies答案的缺点,即所有的标签将被构build,我使用一个特殊的触发器构build。 触发器构build使用与主构build和以下(后)构build步骤相同的git存储库和分支。

生成 – >执行shell:

 # Get the most recent release tag. PATTERN="release-tag-[0-9][0-9]-[0-9][0-9][0-9][0-9]" TAG=$(git log --tags=$PATTERN --no-walk --pretty="format:%d" | grep -m 1 -o $PATTERN) # Due to a Jenkins limitation (https://issues.jenkins-ci.org/browse/JENKINS-8952) # when passing environment variables we have to write the tag to a file and # inject it later again. mv release.properties release-old.properties || true echo "TAG = $TAG" > release.properties # Fail the build if the most recent release tag did not change. ! diff release.properties release-old.properties 

构build – > 注入环境variables :

 Properties File Path: release.properties 

生成后操作 – >: 触发其他项目上的参数化生成

 Projects to build: <your main project> Trigger when build is: Stable Parameters: TAG=$TAG 

最后,在你的主构build中,用下面的string参数勾选“这个构build是参数化的”

 Name: TAG Default Value: <your release branch> 

在“源代码pipe理”部分,在“分支build立”字段中使用“$ TAG”。

您可以安装一个post-receive钩子,它检查一个标签是否被提交,并在jenkins中创build一个构build。

钩子可以看起来像这样[*]:

 #!/usr/bin/env python import sys from subprocess import Popen, PIPE, check_call def call_git(command, args): return Popen(['git', command] + args, stdout=PIPE).communicate()[0] JENKINS = 'http://localhost:8002/jenkins' TOKEN = 'asdf8saffwedssdf' jobname = 'project-tag' def handle_ref(old, new, ref): print 'handle_ref(%s, %s, %s)' % (old, new, ref) if not ref.startswith('refs/tags/'): return url = '%s/job/%s/buildWithParameters?token=%s&branch=%s' % ( JENKINS, jobname, TOKEN, new) print "queueing jenkins job " + jobname + " for " + new check_call(["wget", "-O/dev/null", "--quiet", url]) if __name__ == '__main__': for line in sys.stdin: handle_ref(*line.split()) 

[*]注意:这只是一个稍微不同的脚本的快速转换,所以很可能这里有一些小错误。 这主要是为了展示这个想法。

在jenkins方面,你需要configuration一个参数化的工作。 唯一的参数是“分支”。

  1. 检查“这个版本是参数化”并添加参数
  2. 在'源代码pipe理 – >分支build立'放'$分支'

这给了一个相当安全和强大的方式来build立。 要testing,通过Web界面运行一个构build,它会询问参数的值。

你可以使用选项“Git Publisher”作为Git插件的一部分,在成功的构build/部署之后创build一个标签。