From d7d4e02c8c2741652f14783c8c4449b41e707e93 Mon Sep 17 00:00:00 2001 From: Martin Sehnoutka Date: Wed, 15 Sep 2021 12:15:32 +0200 Subject: [PATCH] internal/common: introduce git revision and rpm version The variables are set to the git revision from which the build is triggered and rpm version from the spec file, if it is build using RPM. This can be later used to query exact source version while running osbuild-composer. It is necessary to use both, because none of them is available in all possible scenarios. Use either git-rev (preferably) or RPM version (NEVRA) instead of the "devel" build type. It was just a placeholder. --- internal/common/constants.go | 27 +++++++++++++++++++++++++++ internal/weldr/api.go | 2 +- osbuild-composer.spec | 8 ++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 internal/common/constants.go diff --git a/internal/common/constants.go b/internal/common/constants.go new file mode 100644 index 000000000..57dc40843 --- /dev/null +++ b/internal/common/constants.go @@ -0,0 +1,27 @@ +package common + +import "fmt" + +// These constants are set during buildtime using additional +// compiler flags. Not all of them are necessarily defined +// because RPMs can be build from a tarball and spec file without +// being in a git repository. On the other hand when building +// composer inside of a container, there is no RPM layer so in +// that case the RPM version doesn't exist at all. +var ( + // Git revision from which this code was built + GitRev = "undefined" + + // RPM Version + RpmVersion = "undefined" +) + +func BuildVersion() string { + if GitRev != "undefined" { + return fmt.Sprintf("git-rev:%s", GitRev) + } else if RpmVersion != "undefined" { + return fmt.Sprintf("NEVRA:%s", RpmVersion) + } else { + return "devel" + } +} diff --git a/internal/weldr/api.go b/internal/weldr/api.go index af90a4c18..798682ce3 100644 --- a/internal/weldr/api.go +++ b/internal/weldr/api.go @@ -561,7 +561,7 @@ func (api *API) statusHandler(writer http.ResponseWriter, request *http.Request, DBVersion: "0", SchemaVersion: "0", Backend: "osbuild-composer", - Build: "devel", + Build: common.BuildVersion(), Messages: make([]string, 0), }) common.PanicOnError(err) diff --git a/osbuild-composer.spec b/osbuild-composer.spec index 28f62871e..756104aad 100644 --- a/osbuild-composer.spec +++ b/osbuild-composer.spec @@ -124,6 +124,14 @@ export GOPATH=$GO_BUILD_PATH:%{gopath} export GOFLAGS=-mod=vendor %endif +# Set the commit hash so that composer can report what source version +# was used to build it. This has to be set explicitly when calling rpmbuild, +# this script will not attempt to automatically discover it. +%if %{?commit:1}0 +export LDFLAGS="${LDFLAGS} -X 'github.com/osbuild/osbuild-composer/internal/common.GitRev=%{commit}'" +%endif +export LDFLAGS="${LDFLAGS} -X 'github.com/osbuild/osbuild-composer/internal/common.RpmVersion=%{name}-%{epoch}:%{version}-%{release}.%{_arch}'" + %gobuild -o _bin/osbuild-composer %{goipath}/cmd/osbuild-composer %gobuild -o _bin/osbuild-worker %{goipath}/cmd/osbuild-worker