Skip to content

Commit

Permalink
Get the commit hash before patches are applied (#343)
Browse files Browse the repository at this point in the history
  • Loading branch information
kminehart authored Jul 15, 2024
1 parent 89dd15c commit ec5bff1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
17 changes: 17 additions & 0 deletions arguments/grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,23 @@ func grafanaDirectory(ctx context.Context, opts *pipeline.ArgumentOpts) (any, er
return nil, err
}

gitContainer := opts.Client.Container().From("alpine/git").
WithWorkdir("/src").
WithMountedDirectory("/src/.git", src.Directory(".git")).
WithEntrypoint([]string{})

commitFile := gitContainer.
WithExec([]string{"/bin/sh", "-c", "git rev-parse HEAD > .buildinfo.grafana-commit"}).
File("/src/.buildinfo.grafana-commit")

branchFile := gitContainer.
WithExec([]string{"/bin/sh", "-c", "git rev-parse --abbrev-ref HEAD > .buildinfo.grafana-branch"}).
File("/src/.buildinfo.grafana-branch")

src = src.
WithFile(".buildinfo.commit", commitFile).
WithFile(".buildinfo.branch", branchFile)

if o.PatchesRepo != "" {
withPatches, err := applyPatches(ctx, opts.Client, src, o.PatchesRepo, o.PatchesPath, o.PatchesRef, ght)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion backend/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func Build(
out string,
opts *BuildOpts,
) *dagger.Directory {
vcsinfo := GetVCSInfo(d, src, opts.Version, opts.Enterprise)
vcsinfo := GetVCSInfo(src, opts.Version, opts.Enterprise)
builder = WithVCSInfo(builder, vcsinfo, opts.Enterprise)

ldflags := LDFlagsDynamic(vcsinfo)
Expand Down
2 changes: 1 addition & 1 deletion backend/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func Builder(
return nil, err
}

commitInfo := GetVCSInfo(d, src, version, opts.Enterprise)
commitInfo := GetVCSInfo(src, version, opts.Enterprise)

builder = withCue(builder, src).
WithDirectory("/src/", src, dagger.ContainerWithDirectoryOpts{
Expand Down
15 changes: 4 additions & 11 deletions backend/vcsinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,15 @@ func WithVCSInfo(c *dagger.Container, info *VCSInfo, enterprise bool) *dagger.Co
}

// VCSInfo gets the VCS data from the directory 'src', writes them to a file on the given container, and returns the files which can be used in other containers.
func GetVCSInfo(d *dagger.Client, src *dagger.Directory, version string, enterprise bool) *VCSInfo {
c := d.Container().From("alpine/git").
WithEntrypoint([]string{}).
WithMountedDirectory("/src", src).
WithWorkdir("/src").
WithExec([]string{"/bin/sh", "-c", "git rev-parse HEAD > .buildinfo.commit"}).
WithExec([]string{"/bin/sh", "-c", "git rev-parse --abbrev-ref HEAD > .buildinfo.branch"})

func GetVCSInfo(src *dagger.Directory, version string, enterprise bool) *VCSInfo {
info := &VCSInfo{
Version: version,
Commit: c.File(".buildinfo.commit"),
Branch: c.File(".buildinfo.branch"),
Commit: src.File(".buildinfo.commit"),
Branch: src.File(".buildinfo.branch"),
}

if enterprise {
info.EnterpriseCommit = c.File(".buildinfo.enterprise-commit")
info.EnterpriseCommit = src.File(".buildinfo.enterprise-commit")
}

return info
Expand Down

0 comments on commit ec5bff1

Please sign in to comment.