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

feat: extract commit hash from build info #148

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
feat: extract commit hash from build info
The Go toolchain stores the git commit hash in the binary, which can
then be extracted using `debug.ReadBuildInfo`. The advantage is that the
commit hash is present even on development builds (`go build` etc), and
it simplifies CI _slightly_.
  • Loading branch information
aykevl committed Nov 21, 2024
commit 7dd0e6a2d275df510834da014161477f3ebcdda6
1 change: 0 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ jobs:
run: |
mkdir -p /tmp/out
docker run --rm \
--env COMMIT=${{ github.sha }} \
--env VERSION=${{ startsWith(github.ref_name, 'v') && github.ref_name || '' }} \
-v $PWD:/src \
go-librespot-build-${{ matrix.target }}
Expand Down
3 changes: 1 addition & 2 deletions Dockerfile.build
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
FROM --platform=linux/amd64 docker.io/golang:1.22.2-bullseye

ENV COMMIT=dev
ENV VERSION=0.0.0

# Preparations
Expand Down Expand Up @@ -56,5 +55,5 @@ ENV CGO_ENABLED=1 PKG_CONFIG_PATH=/tmp/deps/${TARGET}/lib/pkgconfig/ CC=${CC} \
GOCACHE=/src/.gocache/go-build GOMODCACHE=/src/.gocache/mod
CMD go build \
-buildvcs=false \
-ldflags="-X github.com/devgianlu/go-librespot.commit=${COMMIT} -X github.com/devgianlu/go-librespot.version=${VERSION}" \
-ldflags="-X github.com/devgianlu/go-librespot.version=${VERSION}" \
-o ./go-librespot${GOOUTSUFFIX} -a ./cmd/daemon
19 changes: 16 additions & 3 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,28 @@ package go_librespot
import (
"fmt"
"runtime"
"runtime/debug"
"strings"
)

var commit, version string
var version string

// Extract and return the commit hash stored in the binary, if available.
func commitHash() string {
if info, ok := debug.ReadBuildInfo(); ok {
for _, setting := range info.Settings {
if setting.Key == "vcs.revision" {
return setting.Value
}
}
}
return ""
}

func VersionNumberString() string {
if len(version) > 0 {
return strings.TrimPrefix(version, "v")
} else if len(commit) >= 8 {
} else if commit := commitHash(); len(commit) >= 8 {
return commit[:8]
} else {
return "dev"
Expand All @@ -20,7 +33,7 @@ func VersionNumberString() string {

func SpotifyLikeClientVersion() string {
if len(version) > 0 {
if len(commit) >= 8 {
if commit := commitHash(); len(commit) >= 8 {
return fmt.Sprintf("%s.g%s", version, commit[:8])
} else {
return version
Expand Down
Loading