We needed a way to build RPMs from our Git projects but found no simple way to do so. So we made one. And now you can use it, too.

iovation uses Git repositories for all internal (and external) software projects. For deployment to our systems, we rely on RPMs distributed via Yum. In our Java projects, we rely on Jenkins-managed Maven to build RPMs. But for our non-Java projects, such as Sqitch-managed databases, how could we go about automating the building of RPMs for distribution and deployment?

To address this challenge, we developed a simple Git command called git-build-rpm. It has been constantly used and updated for the last three years, and today, we’re pleased to announce that we have open-sourced git-build-rpm.

So how does it work? Pretty simply, assuming familiarity with building RPMs in general. Simply create an [RPM spec file] with the same name as the Git repository in the top-level dist directory of the project. Then have the build server run:

git build-rpm

This command archives the current branch of the Git project and builds the RPM in a temporary directory. It then moves the resulting RPM to the root directory of the project. Copy it from there to your RPM distribution server, as appropriate to your environment.

Naturally, there are a number of options to control the behavior of git-build-rpm, as well as a number of details of its behavior that are worth understanding. For example, the RPM name and %{dist} value include the current epoch time unless building the master branch. This allows a continuous build environment to ensure that the latest RPM will be resolved as the latest by the RPM system. See the README for all the details, as well as installation instructions.

Some tips and tricks:

  • New to RPM spec files? The git-build-rpm.spec file is a decent place to start.

  • Be sure to install build dependencies before building your RPM. Here’s one way to do it by reading BuildRequires values from the spec file:

    rpmspec -q --buildrequires dist/*.spec` | sort -u | xargs sudo yum install -y
  • Remove previously-built RPMs and any other derived files before building by running git clean -dfx.

  • Does your project depend on Git submodules and include their contents in the RPM? That’s fine, git-build-rpm uses git-archive-all to ensure that they’re included in the archive used to build the RPM.

  • Need to use a different name for the RPM, or the spec file? There are options for that! See the README for details.

@theory @iovation git-build-rpm is great! very easy to use, clean way to build RPMs, and easier to ship with RPM than most other ways

— Sam Powers (@sampowers) June 19, 2015

We’ve got a ton of projects building with git-build-rpm now, for which it has worked out just great. Maybe it could work for your projects, too?