
可以使用以下XBS-
方法在 debian/control 檔案中新增使用者定義的字段,依照 Debian 政策手冊中的定義,這些將被注入到二進位 .deb 和原始碼包中。
在呼叫 dpkg-buildpackage 之前類似的操作將向來源套件和二進位套件中註入一個新欄位。但有些軟體包在生成時不包含 debian/control(核心等),因此這並不總是可靠的。
sed -i "0,/^\s*$/s//XBS-Git-Branch: ${CI_COMMIT_BRANCH}\n/" debian/control
dpkg-buildpackage
也可以使用以下命令將使用者定義的欄位注入到生成的來源包中傳遞給 dpkg-source 的選項建置包時:
dpkg-buildpackage --source-option=-DGit-Branch=${CI_COMMIT_BRANCH}
但是,這只會將該欄位注入到生成的來源包中,而不是生成的二進位包中。有沒有一種方法可以可靠地將欄位注入到二進位包中?
答案1
看來您的根本問題並非debian/control
在所有情況下都有。解決這個問題的方法始終是從來源套件開始,因為它們必須提供一個debian/control
檔案。任何其他方法都將涉及直接運行建置的一部分,因為dpkg-buildpackage
需要debian/control
;例如,您可以運行debian/rules build
,然後在運行(生成二進位包)debian/control
之前打補丁(此時必須存在) 。debian/rules binary
要控制二進位套件control
檔案中的內容,您也可以新增選項dpkg-gencontrol
,例如使用dh_gencontrol
:
override_dh_gencontrol:
dh_gencontrol -- -Dfoo=bar
Foo: bar
將會在您的二進位包檔案中新增一個條目control
。這在您的場景中可能更有用。
你需要做出改變debian/rules
才能做到這一點;您可以dh_gencontrol
按照上面的方式使用,或者dpkg-gencontrol
如果套件不使用則直接使用dh
。
答案2
可以從呼叫--hook-buildinfo
後立即調用的掛鉤修改二進制包控製文件debian/rules build
,用於dpkg-deb
解包/重新打包它們。
#!/bin/bash
#
# deb_insert_meta.sh
#
# Inserts CI metadata into all deb files in parent directory
# Intended for use with:
# `dpkg-buildpackage --hook-buildinfo='fakeroot deb_insert_meta.sh'`
# which is the first hook after the binary deb files are generated
# but before checksums for .changes are calculated
# Should be called with 'fakeroot' so that the repacked binaries
# have their content/control ownership/permissions preserved.
pushd .. > /dev/null || exit 1
for deb_file in *.deb; do
[ -e "$deb_file" ] || continue
DEB_TMPDIR=$(mktemp -d)
if [ -z "${DEB_TMPDIR}" ]; then
echo "Failed to create a temporary work directory"
exit 1
fi
dpkg-deb -R "${deb_file}" "${DEB_TMPDIR}"
if [ -e "${DEB_TMPDIR}/DEBIAN/control" ]; then
if [ -n "${CI_PROJECT_PATH}" ]; then
echo "Git-Repo: ${CI_PROJECT_PATH}" >> "${DEB_TMPDIR}/DEBIAN/control"
fi
if [ -n "${CI_COMMIT_SHA}" ]; then
echo "Git-Hash: ${CI_COMMIT_SHA}" >> "${DEB_TMPDIR}/DEBIAN/control"
fi
if [ -n "${CI_COMMIT_BRANCH}" ]; then
echo "Git-Branch: ${CI_COMMIT_BRANCH}" >> "${DEB_TMPDIR}/DEBIAN/control"
fi
dpkg-deb -b "${DEB_TMPDIR}" "${deb_file}"
fi
rm -rf "${DEB_TMPDIR}"
done
popd > /dev/null || exit 1
exit 0