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/Y%2BJ%2BHEXQCJy70QkU%40skade.schwarzvogel.de.

Reply via email to