8000 Stamp the real version into release binaries by adeelahmad · Pull Request #33 · adeelahmad/gadgetcast · GitHub
[go: up one dir, main page]

Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ jobs:
out="dist/$target"
mkdir -p "$out"
CGO_ENABLED=0 GOOS=linux GOARCH="$goarch" GOARM="$goarm" \
go build -trimpath -ldflags "-s -w -X main.version=$VERSION" -o "$out/gadgetcast" ./cmd/gadgetcast
go build -trimpath -ldflags "-s -w -X github.com/adeelahmad/gadgetcast/internal/server.Version=${VERSION#v}" -o "$out/gadgetcast" ./cmd/gadgetcast
cp install.sh "$out/"
tar -czf "dist/gadgetcast_linux_$target.tar.gz" -C "$out" gadgetcast install.sh
done
Expand Down
29 changes: 29 additions & 0 deletions cmd/gadgetcast/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"os/exec"
"path/filepath"
"strings"
"testing"
)

// releaseLdflag is the -X flag release.yml stamps the tag with; keep the two in step.
const releaseLdflag = "-X github.com/adeelahmad/gadgetcast/internal/server.Version="

// The v0.1.1 release binary reported 0.1.0: the stamp named a symbol that did
// not exist, which the linker ignores. Build the way the release does and check
// the binary reports the stamped version.
func TestReleaseStampSetsVersion(t *testing.T) {
bin := filepath.Join(t.TempDir(), "gadgetcast")
build := exec.Command("go", "build", "-ldflags", releaseLdflag+"9.8.7-test", "-o", bin, ".")
if out, err := build.CombinedOutput(); err != nil {
t.Fatalf("go build: %v\n%s", err, out)
}
out, err := exec.Command(bin, "-version").Output()
if err != nil {
t.Fatalf("-version: %v", err)
}
if got := strings.TrimSpace(string(out)); got != "9.8.7-test" {
t.Fatalf("-version = %q, want the stamped 9.8.7-test", got)
}
}
2 changes: 1 addition & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ to its bundled mock data and shows an "offline / demo" badge.

| Method | Path | Body / Response |
| --- | --- | --- |
| GET | `/api/health` | `{"ok":true,"version":"0.1.0"}` |
| GET | `/api/health` | `{"ok":true,"version":"0.1.1"}` (the release tag without its "v"; `0.0.0-dev` in unstamped builds) |
| GET | `/api/nodes` | `NodeGroup[]` — the node catalogue |
| GET | `/api/pipelines` | `Pipeline[]` |
| GET | `/api/pipelines/{id}` | `Pipeline` or 404 |
Expand Down
2 changes: 1 addition & 1 deletion internal/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func TestHealth(t *testing.T) {
resp, body := e.do(t, "GET", "/api/health", nil)
wantStatus(t, resp, body, 200)
got := decode[map[string]any](t, body)
if got["ok"] != true || got["version"] != "0.1.0" {
if got["ok"] != true || got["version"] != Version {
t.Fatalf("health = %s", body)
}
}
Expand Down
6 changes: 4 additions & 2 deletions internal/server/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"strings"
)

const noUINotice = `GadgetCast server ` + Version + `
func noUINotice() string {
return `GadgetCast server ` + Version + `

The Studio UI has not been built into this binary (web/dist/index.html is missing).
Build the UI into web/dist and rebuild the server, or run the UI dev server
against this API. The API is available under /api/ and signaling at /ws.
`
}

// staticHandler serves the embedded SPA: real files as-is, any other path
// falls back to index.html so client-side routes work on reload.
Expand All @@ -37,7 +39,7 @@ func (s *Server) staticHandler() http.Handler {
}
if index == nil {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
_, _ = w.Write([]byte(noUINotice))
_, _ = w.Write([]byte(noUINotice()))
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
Expand Down
5 changes: 3 additions & 2 deletions internal/server/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"regexp"
)

// Version is the server version reported by /api/health.
const Version = "0.1.0"
// Version is the server version reported by /api/health, without the leading
// "v". Release builds stamp it with -ldflags "-X <this package>.Version=1.2.3".
var Version = "0.0.0-dev"

// idPattern is the contract's id rule for path segments (docs/api.md).
var idPattern = regexp.MustCompile(`^[a-zA-Z0-9_-]{1,64}$`)
Expand Down
Loading
0