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