Jenkins 是否可以自动检测并构建 git 存储库中新创建的标签?

发布于 2024-12-10 22:29:53 字数 80 浏览 0 评论 0原文

如果我们的 Jenkins CI 服务器能够在 Github 存储库中创建标签时自动检测、部署和构建标签,那就太好了。

这可能吗?

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

Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

逐鹿 2024-12-17 22:29:53

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

  1. 使作业获取标签就像它们是分支一样: 单击存储库 URL 下方的“高级”按钮,然后输入 Refspec +refs/tags/*:refs/ remotes/origin/tags/*
  2. 使用分支说明符构建所有标签“分支”*/tags/*
  3. 启用 SCM 轮询,以便作业检测新标签。

这种方法有一个缺点:该作业将构建所有标签,而不仅仅是新添加的标签。因此,在创建作业后,将为每个现有标签触发一次。因此,您可能想让作业一开始不执行任何操作,然后等到所有现有标签都已处理完毕,然后才配置您想要为每个新标签完成的构建步骤。

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

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.

This approach has one drawback: The job will build all tags 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.

定格我的天空 2024-12-17 22:29:53

为了克服@oberlies'答案的缺点,即所有标签都将被构建,我使用特殊的触发器构建来代替。触发器构建使用与主构建和以下(后)构建步骤相同的 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 -> 注入环境变量

Properties File Path: release.properties

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

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

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

Name: TAG
Default Value: <your release branch>

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

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.

Build -> Execute 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

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.

迷鸟归林 2024-12-17 22:29:53

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

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

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

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

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

  1. 添加参数
  2. 检查“此构建已参数化”并在“源代码管理->”中 Branches to build' put '$branch'

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

You can install a post-receive hook, which checks if a tag was commited, and creates a build in 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'.

  1. Check 'this build is parametrized' and add the parameter
  2. In 'source code management -> branches to build' put '$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.

遇见了你 2024-12-17 22:29:53

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

  1. 添加“行为”来发现标签:
    输入图片此处描述
  2. 使用插件 基本分支构建为标签添加“构建策略”的策略
    输入图片此处描述
    不要忘记为分支添加构建策略;该插件完全禁用默认设置!

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 Strategies to add a "build strategy" for tags:
    enter image description here
    Don't forget to add a build strategy for branches as well; the plugin disables the defaults completely!
那小子欠揍 2024-12-17 22:29:53

您可以使用选项“Git Publisher”,它是 Git 插件 在成功构建/部署后创建标签。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文