Skip to content

Commit

Permalink
Add enterprise commit to buildinfo (#176)
Browse files Browse the repository at this point in the history
Co-authored-by: Horst Gutmann <horst.gutmann@grafana.com>
  • Loading branch information
kminehart and zerok authored Oct 18, 2023
1 parent e168b40 commit f79b5de
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 12 deletions.
54 changes: 44 additions & 10 deletions containers/buildinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand All @@ -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"})
Expand Down
18 changes: 16 additions & 2 deletions containers/enterprise.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit f79b5de

Please sign in to comment.