Skip to content

Commit

Permalink
refactor(cmd/goproxy): inject version info at build time
Browse files Browse the repository at this point in the history
  • Loading branch information
aofei committed Mar 14, 2024
1 parent a1e2a77 commit 639e995
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ jobs:
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v5
with:
args: release --rm-dist
workdir: cmd/goproxy
args: release --clean
env:
GITHUB_TOKEN: ${{github.token}}
docker:
Expand Down
28 changes: 28 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
before:
hooks:
- go mod download

builds:
- id: goproxy
main: ./cmd/goproxy
ldflags:
- -s -w -X github.com/goproxy/goproxy/cmd/goproxy/internal.Version=v{{.Version}}
env:
- CGO_ENABLED=0
targets:
- darwin_amd64
- darwin_arm64
- linux_386
- linux_amd64
- linux_arm64
- linux_arm_6
- linux_arm_7
- linux_ppc64le
- linux_s390x
- windows_386
- windows_amd64
- windows_arm64
mod_timestamp: "{{.CommitTimestamp}}"

archives:
- id: default
6 changes: 6 additions & 0 deletions cmd/goproxy/internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ func Execute() int {
return 0
}

// Version is the version of the running binary set by the Go linker.
var Version string

// binaryVersion returns the version of the running binary.
func binaryVersion() string {
info, ok := debug.ReadBuildInfo()
if !ok {
return ""
}
if Version != "" {
info.Main.Version = Version
}
version := "Version: " + info.Main.Version + "\n"
for _, setting := range info.Settings {
switch setting.Key {
Expand Down

0 comments on commit 639e995

Please sign in to comment.