如何说服詹金斯分享多个工作的内部版本号? [英] How to convince jenkins to share a build number for several jobs?

查看:15
本文介绍了如何说服詹金斯分享多个工作的内部版本号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开发团队确实要求我设置构建系统,以便每个构建对所有分支都有唯一的构建编号.

I do have a requirement from the development team to setup the build system so each build will have an unique build number for all branches.

构建由 jenkins 使用每个分支的作业进行.

The builds are made by jenkins using jobs for each branch.

有一个 jenkins 插件可以为工作设置下一个版本号,但这至少有两个原因没用:

There is a jenkins plugin that can setup the next buildnumber for a job but this is kinda useless for at least two reasons:

  • 它将为单个作业设置内部版本号,您无法知道如何为所有分支设置它,因为它们可以随时删除或添加
  • 它没有为当前构建设置它

我们如何获取内部版本号:我们确实使用 git/mercurial 中的分支名称和修订号进行 HTTP 调用.基于此,集中式服务器给我们一个内部版本号作为响应.如果你用相同的参数调用它两次,你会得到相同的响应(期望的行为).

How to we get the build numbers: we do make a HTTP call with the branch name and the revision number in git/mercurial. Based on this the centralized sever is giving us a build number as a response. If you call it twice with the same parameters, you will get the same response (desired behaviour).

现在,我们如何调整 jenkins 以使用与我们相同的内部版本号?显然我可以使用脚本返回的内部版本号,但作业号会有所不同,我怀疑 jenkins 会知道我在脚本中接触了 BUILD_NUMBER 变量.

Now, how can we tweak jenkins to use the same build numbers as us? Obviously I could use the build number returned from the script, but the job number would be different and I doubt jenkins will know that I touched the BUILD_NUMBER variable inside my script.

主要是,我需要的是某种可以运行的作业启动前脚本,该脚本可以在将内部版本号分配给作业之前运行.

Mainly, what I need is some kind of pre-job-start script that I can run, one that would run before the build number is assigned to the job.

推荐答案

您可以使用 Environment Injector Plugin 在运行前评估 Groovy 脚本.我有几乎相同的要求,但是对我来说,只有名称中具有相同 job_prefix_ 的作业共享相同的唯一 nextBuildNumber(换句话说,具有 job_prefix_ 的其他作业>job_prefix2_ 在他们的名字中共享不同的 nextBuildNumber).

You can use the Environment Injector Plugin to evaluate a Groovy script before your run. I have almost the same requirement, however for me, only the jobs with the same job_prefix_ in their name share the same unique nextBuildNumber (in other words, other jobs with job_prefix2_ in their name share a different nextBuildNumber).

Evaluated Groovy Script 部分,请使用:

import jenkins.model.*

// Access to the Jenkins instance
jenkins_instance = jenkins.model.Jenkins.instance

// Select jobs that match.
job_name = "^job_prefix_.*"
allItems = jenkins_instance.items
chosenJobs = allItems.findAll{ job -> job.name.matches(job_name) }

// Get the max
build_number = chosenJobs.collect{ it -> it.nextBuildNumber }.max()

// Increase next build number
currentJob.nextBuildNumber = build_number + 1

// and use it.
def map = [BUILD_NUMBER: build_number]
return map

这篇关于如何说服詹金斯分享多个工作的内部版本号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆