From f79b5de8cea09f4ca2f840d84f8ea7b077251840 Mon Sep 17 00:00:00 2001 From: Kevin Minehart Date: Wed, 18 Oct 2023 11:04:22 +0100 Subject: [PATCH] Add enterprise commit to buildinfo (#176) Co-authored-by: Horst Gutmann --- containers/buildinfo.go | 54 ++++++++++++++++++++++++++++++++-------- containers/enterprise.go | 18 ++++++++++++-- 2 files changed, 60 insertions(+), 12 deletions(-) diff --git a/containers/buildinfo.go b/containers/buildinfo.go index 8c911dd5..2bb09139 100644 --- a/containers/buildinfo.go +++ b/containers/buildinfo.go @@ -13,19 +13,25 @@ import ( ) type BuildInfo struct { - Version string - Commit string - Branch string - Timestamp time.Time + Version string + Commit string + EnterpriseCommit string + Branch string + Timestamp time.Time } func (b *BuildInfo) LDFlags() []string { - return []string{ + flags := []string{ fmt.Sprintf("main.version=%s", strings.TrimPrefix(b.Version, "v")), fmt.Sprintf("main.commit=%s", b.Commit), fmt.Sprintf("main.buildstamp=%d", b.Timestamp.Unix()), fmt.Sprintf("main.buildBranch=%s", b.Branch), } + + if b.EnterpriseCommit != "" { + flags = append(flags, fmt.Sprintf("main.enterpriseCommit=%s", b.EnterpriseCommit)) + } + return flags } const GitImage = "alpine/git:v2.36.3" @@ -40,6 +46,8 @@ func GetBuildInfo(ctx context.Context, d *dagger.Client, dir *dagger.Directory, WithMountedDirectory("/src", dir). WithWorkdir("/src") + enterpriseSha := enterpriseCommit(ctx, container) + sha, err := revParseShort(ctx, container) if err != nil { span.RecordError(err) @@ -57,15 +65,41 @@ func GetBuildInfo(ctx context.Context, d *dagger.Client, dir *dagger.Directory, timestamp := time.Now() result := &BuildInfo{ - Version: version, - Commit: sha, - Branch: branch, - Timestamp: timestamp, + Version: version, + Commit: sha, + EnterpriseCommit: enterpriseSha, + Branch: branch, + Timestamp: timestamp, } - span.SetAttributes(attribute.String("version", version), attribute.String("commit", sha), attribute.String("branch", branch), attribute.String("timestamp", timestamp.Format(time.RFC3339))) + span.SetAttributes( + attribute.String("version", version), + attribute.String("commit", sha), + attribute.String("enterpriseCommit", enterpriseSha), + attribute.String("branch", branch), + attribute.String("timestamp", timestamp.Format(time.RFC3339)), + ) return result, nil } +func enterpriseCommit(ctx context.Context, container *dagger.Container) string { + var err error + c := container. + WithEntrypoint([]string{}). + WithExec([]string{"/bin/sh", "-c", "cat /src/.enterprise-commit || return 0"}) + + c, err = ExitError(ctx, c) + if err != nil { + return "" + } + + stdout, err := c.Stdout(ctx) + if err != nil { + return "" + } + + return strings.TrimSpace(stdout) +} + func revParseShort(ctx context.Context, container *dagger.Container) (string, error) { var err error c := container.WithExec([]string{"rev-parse", "--short", "HEAD"}) diff --git a/containers/enterprise.go b/containers/enterprise.go index fe53eccc..4961b561 100644 --- a/containers/enterprise.go +++ b/containers/enterprise.go @@ -1,15 +1,29 @@ package containers -import "dagger.io/dagger" +import ( + "dagger.io/dagger" +) const BusyboxImage = "busybox:1.36" func InitializeEnterprise(d *dagger.Client, grafana *dagger.Directory, enterprise *dagger.Directory) *dagger.Directory { - return d.Container().From(BusyboxImage). + hash := d.Container().From(GitImage). + WithDirectory("/src/grafana-enterprise", enterprise). + WithWorkdir("/src/grafana-enterprise"). + WithEntrypoint([]string{}). + WithExec([]string{"/bin/sh", "-c", "git rev-parse --short HEAD > .enterprise-commit"}). + File("/src/grafana-enterprise/.enterprise-commit") + + // Initializes Grafana Enterprise in the Grafana directory + g := d.Container().From(BusyboxImage). WithDirectory("/src/grafana", grafana). WithDirectory("/src/grafana-enterprise", enterprise). WithWorkdir("/src/grafana-enterprise"). + WithFile("/src/grafana/.enterprise-commit", hash). WithExec([]string{"/bin/sh", "build.sh"}). WithExec([]string{"cp", "LICENSE", "../grafana"}). + WithExec([]string{"cat", "../grafana/.enterprise-commit"}). Directory("/src/grafana") + + return g }