简体   繁体   English

在 RPM 规范文件中使用 Jenkins BUILD NUMBER

[英]Using Jenkins BUILD NUMBER in RPM spec file

Name:                   My Software
Version:                1.0.5
Release:                1
Summary:                This is my software

Not sure if anyone has tried this before or if it is easy, but:不确定之前是否有人尝试过或者是否很容易,但是:

A spec file has two unique indicators for its version:规范文件的版本有两个独特的指标:

  • Version (which specifies software version)版本(指定软件版本)
  • Release (which specifies the package's number - if you build an RPM, it's broken, and build another one, you up the 'Release' number.发布(它指定了包的编号——如果你构建了一个 RPM,它就坏了,然后再构建一个,你就增加了“发布”号。

I'm wondering if anyone has tried, or knows how, I could use the Jenkins $BUILD_NUMBER variable to dynamically change the Release number, thereby increasing the Release number every time a new successful build completes...?我想知道是否有人尝试过或知道如何使用 Jenkins $BUILD_NUMBER 变量来动态更改Release编号,从而在每次新的成功构建完成时增加Release编号......?

It's been a long time... and thankfully I have no rpm based systems so I can't test this.已经很长时间了……幸好我没有基于 rpm 的系统,所以我无法对此进行测试。

You can pass parameters to rpmbuild on the commandline您可以在命令行上将参数传递给rpmbuild

rpmbuild --define="version ${env.BUILD_NUMBER}"

It would be helpful to post snippets of the spec and the script you're using to build the rpm.发布规范的片段和用于构建 rpm 的脚本会很有帮助。 You don't want your build script editing the spec file, which I'm assuming it's pulling out down from some source control.你不希望你的构建脚本编辑规范文件,我假设它是从一些源代码控制中提取出来的。

I've been using the Jenkins build number as the 'release' and packaging via fpm .我一直在使用 Jenkins 内部版本号作为“发布”并通过fpm打包。

Couple fpm with some globals provided by Jenkins将 fpm 与 Jenkins 提供的一些全局变量结合使用

# $BUILD_ID - The current build id, such as "2005-08-22_23-59-59" (YYYY-MM-DD_hh-mm-ss)
# $BUILD_NUMBER - The current build number, such as "153"
# $BUILD_TAG - String of jenkins-${JOB_NAME}-${BUILD_NUMBER}. Convenient to put into a resource file, a jar file, etc for easier identification.

There's some nebulous variables in the example command below, but $BUILD_NUMBER is what I'm using for the release here (fpm calls it iteration instead).下面的示例命令中有一些模糊的变量,但$BUILD_NUMBER是我在此处用于发布的变量(fpm 将其称为迭代)。

fpm_out=$(fpm -a all -n $real_pkg_name -v $version -t rpm -s dir --iteration $BUILD_NUMBER ./*)

In my Jenkins setup, I've decided to bypass the build number with regards to the RPM version numbering completely.在我的 Jenkins 设置中,我决定完全绕过 RPM 版本编号的内部版本号。 Instead, I use a home-made script that generates and keeps track of the various releases that are being generated.相反,我使用一个自制的脚本来生成并跟踪正在生成的各种版本。

In my spec file:在我的规范文件中:

Version:    %{_iv_pkg_version}
Release:    %{_iv_pkg_release}%{?dist}

And in the Jenkins build script:在 Jenkins 构建脚本中:

# Just initialising some variables, and retrieving the release number.
package="$JOB_NAME"
# We use setuptools, so we can query the package version like so.
# Use other means to suit your needs.
pkg_version="$(python setup.py --version)"
pkg_release="$(rpm-release-number.py "$package" "$pkg_version")"

# Creating the src.rpm (ignore the spec file variables)
rpmbuild --define "_iv_pkg_version $pkg_version" \
    --define "_iv_pkg_release $pkg_release" \
    -bs "path/to/my/file.spec"

# Use mock to build the package in a clean chroot
mock -r epel-6-x86_64 --define "_iv_pkg_version $pkg_version" \
    --define "_iv_pkg_release $pkg_release" \
    "path/to/my/file.src.rpm"

rpm-release-number.py is a simple script that maintains a file-based database (in JSON format, for easy maintenance). rpm-release-number.py是一个简单的脚本,用于维护基于文件的数据库(JSON 格式,便于维护)。 It can handle being run at the same time, so no worries there, but it won't work if you have build slaves (as far as I can tell, I don't use them so can't test).它可以处理同时运行,所以不用担心,但如果你有构建奴隶它就不会工作(据我所知,我不使用它们,所以无法测试)。 You can find the source code and documentation here .您可以在此处找到源代码和文档。

The result is that I get the following package versioning scheme:结果是我得到以下包版本控制方案:

# Build the same version 3 times
foo-1.1-1
foo-1.1-2
foo-1.1-3
# Increment the version number, and build twice
foo-1.2-1
foo-1.2-2

PS: Note that the Jenkins build script is just an example, the logic behind creating the rpmbuild directory structure and retrieving the .src.rpm and .spec file names is a bit more complicated. PS:注意 Jenkins 构建脚本只是一个例子,创建 rpmbuild 目录结构和检索 .src.rpm 和 .spec 文件名背后的逻辑有点复杂。

Taking into account that spec file could be 3rd-party I prefer to do pre-build sed-patching of Release field:考虑到规范文件可能是第 3 方,我更喜欢对 Release 字段进行预构建 sed-patching:

sed -i 's/^Release:\(\s*\)\(.*\)$/Release:\1%{?_build_num:%{_build_num}.}%{expand:\2}/g' ./path/to/spec
rpmbuild --define '_build_num $BUILD_NUM' -ba ./path/to/spec

Here %{expand:...} macro is used to handle macro defined release numbers like ones in Mageia specs:这里%{expand:...}宏用于处理宏定义的版本号,如 Mageia 规范中的版本号:

Release: %mkrel 1

Resulting field will be:结果字段将是:

Release: %{?_build_num:%{_build_num}.}%{expand:%mkrel 1}

Conditional expansion of _build_num macro makes spec still usable for local build. _build_num宏的条件扩展使规范仍然可用于本地构建。 Ie if SRPM is also prepared by build system.即如果 SRPM 也由构建系统准备。 But it could be reduced to:但可以简化为:

sed -i 's/^Release:\(\s*\)\(.*\)$/Release:\1'$BUILD_NUM'.%{expand:\2}/g' ./path/to/spec
rpmbuild -ba ./path/to/spec

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM