Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add enterprise commit to buildinfo #176

Merged
merged 3 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 40 additions & 9 deletions containers/buildinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package containers
import (
"context"
"fmt"
"log"
"strings"
"time"

Expand All @@ -13,19 +14,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 +47,8 @@ func GetBuildInfo(ctx context.Context, d *dagger.Client, dir *dagger.Directory,
WithMountedDirectory("/src", dir).
WithWorkdir("/src")

enterpriseSha, _ := enterpriseCommit(ctx, container)
zerok marked this conversation as resolved.
Show resolved Hide resolved

sha, err := revParseShort(ctx, container)
if err != nil {
span.RecordError(err)
Expand All @@ -57,15 +66,37 @@ 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)))
zerok marked this conversation as resolved.
Show resolved Hide resolved
return result, nil
}

func enterpriseCommit(ctx context.Context, container *dagger.Container) (string, error) {
var err error
c := container.
WithEntrypoint([]string{}).
WithExec([]string{"/bin/sh", "-c", "cat /src/.enterprise-commit || return 0"})

log.Println("Getting container exit error")
zerok marked this conversation as resolved.
Show resolved Hide resolved
c, err = ExitError(ctx, c)
if err != nil {
return "", nil
}

log.Println("Getting container stdout")
zerok marked this conversation as resolved.
Show resolved Hide resolved
stdout, err := c.Stdout(ctx)
if err != nil {
return "", nil
}

return strings.TrimSpace(stdout), nil
}

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).
zerok marked this conversation as resolved.
Show resolved Hide resolved
WithExec([]string{"/bin/sh", "build.sh"}).
WithExec([]string{"cp", "LICENSE", "../grafana"}).
WithExec([]string{"cat", "../grafana/.enterprise-commit"}).
Directory("/src/grafana")

return g
}