Thanks for the ideas all, I think go embed is the best path for me.

runtime/debug will give me the revision but sadly it can't give me the 
branch.

I want the branch as we run a microservice architecture and seeing that all 
the microservices are running
the release branch and not master (or main) is a useful cross check for our 
QA dept; we have had cases of
them spending time checking development code which is - well, imperfect.

-Steve

On Tuesday, 7 February 2023 at 16:37:16 UTC klau...@schwarzvogel.de wrote:

> Hi! 
>
> On Mon, 06 Feb 2023, quin...@gmail.com wrote:
> > I would like to be able to extract the VCS branch name used during build.
> > Currently I append "-X main.BranchName=${BRANCH}" to the build line 
> which 
> > works, but I was hoping there might be a cunning way to extract this 
> from 
> > runtime/debug?
>
> Indeed there is. On top of the already mentioned approaches using go
> generate, the runtime/debug package:
>
> ```
> import "runtime/debug"
>
> var Commit = func() string {
> if info, ok := debug.ReadBuildInfo(); ok {
> for _, setting := range info.Settings {
> if setting.Key == "vcs.revision" {
> return setting.Value
> }
> }
> }
> return ""
> }()
> ```
>
> (shamelessly stolen from
>
> https://icinga.com/blog/2022/05/25/embedding-git-commit-information-in-go-binaries/
> )
>
> HTH,
> Tobias
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/b53d020d-147e-4a34-9ce9-d5efbc185b99n%40googlegroups.com.

Reply via email to