Skip to content

Commit 7509bc1

Browse files
Break out RPM version logic from RPM build script
In order to determine what RPM package version would be built from a given commit, the logic for determining the version needs to be split out from the script that builds RPMs. Now, if you are going to use `hack/build-rpm-release.sh` you can first call `os::build::rpm::format_nvr` to determine the NVR of the package you will build. Signed-off-by: Steve Kuznetsov <[email protected]>
1 parent e61510a commit 7509bc1

File tree

2 files changed

+86
-14
lines changed

2 files changed

+86
-14
lines changed

hack/build-rpm-release.sh

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,9 @@ else
1717
fi
1818

1919
os::log::info 'Building Origin release RPMs with tito...'
20-
os::build::get_version_vars
21-
if [[ "${OS_GIT_TREE_STATE}" == "dirty" ]]; then
22-
os::log::fatal "Cannot build RPMs with a dirty git tree. Commit your changes and try again."
23-
fi
24-
if [[ "${OS_GIT_VERSION}" =~ ^v([0-9](\.[0-9]+)*)(.*) ]]; then
25-
# we need to translate from the semantic version
26-
# provided by the Origin build scripts to the
27-
# version that RPM will expect.
28-
rpm_version="${BASH_REMATCH[1]}"
29-
rpm_release="999${BASH_REMATCH[3]//-/.}"
30-
fi
31-
tito tag --use-version="${rpm_version}" \
32-
--use-release="${rpm_release}" \
20+
os::build::rpm::get_nvr_vars
21+
tito tag --use-version="${OS_RPM_VERSION}" \
22+
--use-release="${OS_RPM_RELEASE}" \
3323
--no-auto-changelog --offline
3424
tito_tmp_dir="${BASETMPDIR}/tito"
3525
mkdir -p "${tito_tmp_dir}"
@@ -56,7 +46,7 @@ else
5646
output_directory="${output_directories[0]}"
5747
fi
5848

59-
tito_output_directory="$( find "${output_directory}" -type d -path "*/BUILD/origin-${rpm_version}/_output/local" )"
49+
tito_output_directory="$( find "${output_directory}" -type d -path "*/BUILD/origin-${OS_RPM_VERSION}/_output/local" )"
6050
if [[ -z "${tito_output_directory}" ]]; then
6151
os::log::fatal 'No _output artifact directory found in tito rpmbuild artifacts!'
6252
fi

hack/lib/build/rpm.sh

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/bin/bash
2+
3+
# This library holds utilities for building RPMs from Origin.
4+
5+
# os::build::rpm::generate_nevra_vars determines the NEVRA of the RPMs
6+
# that would be built from the current git state.
7+
#
8+
# Globals:
9+
# - OS_GIT_VERSION
10+
# Arguments:
11+
# - None
12+
# Exports:
13+
# - OS_RPM_NAME
14+
# - OS_RPM_VERSION
15+
# - OS_RPM_RELEASE
16+
function os::build::rpm::get_nvr_vars() {
17+
# the package name can be overwritten but is normally 'origin'
18+
OS_RPM_NAME="${OS_RPM_NAME:-"origin"}"
19+
20+
# we can extract the pacakge version from the build version
21+
os::build::get_version_vars
22+
if [[ "${OS_GIT_VERSION}" =~ ^v([0-9](\.[0-9]+)*)(.*) ]]; then
23+
OS_RPM_VERSION="${BASH_REMATCH[1]}"
24+
metadata="${BASH_REMATCH[3]}"
25+
else
26+
os::log::fatal "Malformed \$OS_GIT_VERSION: ${OS_GIT_VERSION}"
27+
fi
28+
29+
# we can generate the package release from the git version metadata
30+
# OS_GIT_VERSION will always have metadata, but either contain
31+
# pre-release information _and_ build metadata, or only the latter
32+
# ex.
33+
# -alpha.0+shasums-123-dirty
34+
# -alpha.0+shasums-123
35+
# +shasums-123-dirty
36+
# +shasums-123
37+
if [[ "${metadata:0:1}" == "+" ]]; then
38+
# we only have build metadata, but need to massage it so
39+
# we can generate a valid RPM release from it
40+
if [[ "${metadata}" =~ ^\+([a-z0-9]{7})-([0-9]+)(-dirty)?$ ]]; then
41+
build_sha="${BASH_REMATCH[1]}"
42+
build_num="${BASH_REMATCH[2]}"
43+
else
44+
os::log::fatal "Malformed git version metadata: ${metadata}"
45+
fi
46+
OS_RPM_RELEASE="1.${build_num}.${build_sha}"
47+
elif [[ "${metadata:0:1}" == "-" ]]; then
48+
# we have both build metadata and pre-release info
49+
if [[ "${metadata}" =~ ^-([^\+]+)\+([a-z0-9]{7})-([0-9]+)(-dirty)?$ ]]; then
50+
pre_release="${BASH_REMATCH[1]}"
51+
build_sha="${BASH_REMATCH[2]}"
52+
build_num="${BASH_REMATCH[3]}"
53+
else
54+
os::log::fatal "Malformed git version metadata: ${metadata}"
55+
fi
56+
OS_RPM_RELEASE="0.${pre_release}.${build_num}.${build_sha}"
57+
else
58+
os::log::fatal "Malformed git version metadata: ${metadata}"
59+
fi
60+
61+
export OS_RPM_NAME OS_RPM_VERSION OS_RPM_RELEASE
62+
}
63+
64+
65+
# os::build::rpm::format_nvr formats the rpm NVR vars generated by
66+
# os::build::rpm::get_nvr_vars and will generate them if necessary
67+
#
68+
# Globals:
69+
# - OS_RPM_NAME
70+
# - OS_RPM_VERSION
71+
# - OS_RPM_RELEASE
72+
# Arguments:
73+
# None
74+
# Returns:
75+
# None
76+
function os::build::rpm::format_nvr() {
77+
if [[ -z "${OS_RPM_NAME:-}" || -z "${OS_RPM_VERSION:-}" || -z "${OS_RPM_RELEASE:-}" ]]; then
78+
os::build::rpm::get_nvr_vars
79+
fi
80+
81+
echo "${OS_RPM_NAME}-${OS_RPM_VERSION}-${OS_RPM_RELEASE}"
82+
}

0 commit comments

Comments
 (0)