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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7805603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:05:48  来源:igfitidea点击:

Is it possible for Jenkins to automatically detect and build newly created tags in a git repo?

gitjenkins

提问by Ricardo Gladwell

It would be nice for our Jenkins CI server to automatically detect, deploy and build tags as they are created in our Github repository.

我们的 Jenkins CI 服务器能够自动检测、部署和构建在我们的 Github 存储库中创建的标签会很好。

Is this possible?

这可能吗?

采纳答案by oberlies

With the following configuration, you can make a job build all tags:

使用以下配置,您可以让作业构建所有标签:

  1. Make the job fetch tags as if they were branches: Click on the Advanced button below the repository URL and enter the Refspec +refs/tags/*:refs/remotes/origin/tags/*
  2. Have it build all tag "branches" with the Branch Specifier */tags/*
  3. Enable SCM polling, so that the job detects new tags.
  1. 使作业提取标签就像它们是分支一样:单击存储库 URL 下方的“高级”按钮并输入 Refspec +refs/tags/*:refs/remotes/origin/tags/*
  2. 让它使用分支说明符构建所有标签“分支” */tags/*
  3. 启用 SCM 轮询,以便作业检测新标签。

This approach has one drawback: The job will build alltags and not just newly added tags. So after you have created the job, it will be triggered once for every existing tag. So you probably want to have the job do nothing at first, then wait until all existing tags have been processed, and only then configure the build steps you want to be done for every new tag.

这种方法有一个缺点:该作业将构建所有标签,而不仅仅是新添加的标签。因此,在您创建作业后,它将为每个现有标签触发一次。因此,您可能希望首先让作业什么都不做,然后等到所有现有标签都处理完毕,然后才配置您希望为每个新标签完成的构建步骤。

Since tags don't change in git, the job will then only be triggered once for every new tag.

由于标签在 git 中不会更改,因此对于每个新标签,作业只会被触发一次。

回答by sschuberth

To overcome the drawback of @oberlies' answer that all tags will be built I'm using a special trigger build instead. The trigger build uses the same git repository and branch as the main build and the following (post) build steps.

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

Build -> Execute shell:

构建 -> 执行 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 -> Inject environment variables:

构建 ->注入环境变量

Properties File Path: release.properties

Post-build Actions -> : Trigger parameterized build on other projects

Post-build Actions -> :在其他项目上触发参数化构建

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

Finally, in your main build, tick "This build is parameterized" with the following string parameter

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

Name: TAG
Default Value: <your release branch>

And in the "Source Code management" section use "$TAG" in the "Branches to build" field.

并在“源代码管理”部分的“要构建的分支”字段中使用“$TAG”。

回答by zbyszek

You can install a post-receive hook, which checks if a tag was commited, and creates a build in jenkins.

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

The hook can look something like this [*]:

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

#!/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())

[*] note: this is just a quick conversion from a slightly different script, so it's quite probable that there are some small bugs here. This is mostly to show the idea.

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

On the jenkins side, you need to configure a parametrized job. The only parameter is 'branch'.

在 jenkins 方面,您需要配置一个参数化作业。唯一的参数是“分支”。

  1. Check 'this build is parametrized' and add the parameter
  2. In 'source code management -> branches to build' put '$branch'
  1. 选中“此构建已参数化”并添加参数
  2. 在“源代码管理->要构建的分支”中放置“$branch”

This gives a fairly secure and robust way to build. To test, run a build through the web interface, it'll ask for the value of the parameter.

这提供了一种相当安全和健壮的构建方式。要进行测试,请通过 Web 界面运行构建,它会询问参数的值。

回答by Raphael

In the world of modern (?) multi-branch pipelines, building tags works as follows.

在现代 (?) 多分支管道的世界中,构建标签的工作方式如下。

  1. Add the "behaviour" to discover tags:
    enter image description here
  2. Use plugin Basic Branch Build Strategiesto add a "build strategy" for tags: enter image description hereDon't forget to add a build strategy for branches as well; the plugin disables the defaults completely!
  1. 添加“行为”以发现标签:
    在此处输入图片说明
  2. 使用插件Basic Branch Build Strategies为标签添加“构建策略”: 在此处输入图片说明不要忘记为分支添加构建策略;该插件完全禁用默认值!

回答by leenasn

You can use the option "Git Publisher" which comes as part of the Git Pluginto create a tag after a successful build/deploy.

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