Jenkins是否可以在git仓库中自动检测和构建新创建的标签?

Ric*_*ell 44 git jenkins

我们的Jenkins CI服务器在我们的Github存储库中创建标签时会自动检测,部署和构建标签.

这可能吗?

obe*_*ies 34

通过以下配置,您可以创建作业构建所有标记:

  1. 使作业获取标记就像它们是分支一样:单击存储库URL下面的"高级"按钮,然后输入Refspec +refs/tags/*:refs/remotes/origin/tags/*
  2. 使用Branch Specifier构建所有标记"branches" */tags/*
  3. 启用S​​CM轮询,以便作业检测到新标记.

这种方法有一个缺点:作业将构建所有标签而不仅仅是新添加的标签.因此,在创建作业后,将为每个现有标记触发一次.因此,您可能希望一开始就不执行任务,然后等待所有现有标记都已处理完毕,然后再配置要为每个新标记完成的构建步骤.

由于标签在git中没有变化,因此每个新标签只会触发一次作业.


ssc*_*rth 17

为了克服@oberlies回答所有标签将被构建的缺点,我正在使用特殊的触发器构建.触发器构建使用与主构建和以下(post)构建步骤相同的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
Run Code Online (Sandbox Code Playgroud)

构建 - > 注入环境变量:

Properties File Path: release.properties
Run Code Online (Sandbox Code Playgroud)

构建后操作 - >:在其他项目上触发参数化构建

Projects to build: <your main project>
Trigger when build is: Stable
Parameters: TAG=$TAG
Run Code Online (Sandbox Code Playgroud)

最后,在主构建中,使用以下字符串参数勾选"此构建已参数化"

Name: TAG
Default Value: <your release branch>
Run Code Online (Sandbox Code Playgroud)

在"源代码管理"部分中,在"要构建的分支"字段中使用"$ TAG".