1

It's possible to add user-defined fields in the debian/control file using the XBS- method, as defined in the Debian Policy Manual, and those will be injected into the binary .deb and source packages.

Something like this before calling dpkg-buildpackage will work to inject a new field into both the Source and Binary packages. But some packages don't contain debian/control as they generate it (kernel etc) so this is not always reliable.

sed -i "0,/^\s*$/s//XBS-Git-Branch: ${CI_COMMIT_BRANCH}\n/" debian/control
dpkg-buildpackage

It's also possible to inject the user-defined field into the generated source package using options passed to dpkg-source while building the package:

dpkg-buildpackage --source-option=-DGit-Branch=${CI_COMMIT_BRANCH}

However, that will only inject the field into the generated source package, not the generated binary packages. Is there a method to reliably inject fields into the binary packages?

0

It seems your underlying problem is not having debian/control in all cases. The fix for that is always to start from source packages, since they have to provide a debian/control file. Any other approach is going to involve running part of the build directly, since dpkg-buildpackage requires debian/control; for example, you could run debian/rules build, and then patch debian/control (which must exist at this point) before running debian/rules binary (which produces the binary packages).

To control what goes into the binary packages’ control file, you can also add options for dpkg-gencontrol, e.g. using dh_gencontrol:

override_dh_gencontrol:
        dh_gencontrol -- -Dfoo=bar

will add a Foo: bar entry to your binary packages’ control files. This is probably more useful in your scenario.

You’ll need to change debian/rules to do this; you can use dh_gencontrol as above, or dpkg-gencontrol directly if the package doesn’t use dh.

3
  • Thanks, that's a useful bit of information. I'm not sure it helps me, as making that change to debian/rules using sed, for an arbitrary package, is even less reliable than my original sed modification of debian/control. I'm looking for method to inject fields into both the generated source/binary packages when building an arbitrary package with dpkg-buildpackage (or gbp).
    – NickBroon
    May 25 at 9:53
  • Ah right, I hadn’t understood that this is for arbitrary packages that you don’t necessarily want to modify. May 25 at 10:42
  • See the updated answer. May 25 at 10:53
0

It is possible to modify the binary packages control files from the --hook-buildinfo hook that's called immediately after debian/rules build is called, using dpkg-deb to unpack/repack them.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.