Skip to content

armgit / lyftr

Public Imported from GitHub
0

Self-hosted fitness tracker. Log workouts, track nutrition, and monitor progress. Docker deployment, mobile-friendly.

feat: derive app version from git tag and surface it in the UI (#26)

Carter5db1884a0c58
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 6c7a4c3..f3f06d6 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -84,12 +84,31 @@ jobs:
path: web/playwright-report/
retention-days: 7
+ version-smoke:
+ name: Version injection smoke test
+ runs-on: ubuntu-latest
+ steps:
+ - uses: actions/checkout@v4
+ - name: Build backend image with a known version
+ run: docker build --build-arg VERSION=smoke-9.9.9 -t lyftr-backend:smoke ./backend
+ - name: Assert the binary reports the injected version
+ run: |
+ set -euo pipefail
+ out=$(docker run --rm lyftr-backend:smoke --version)
+ echo "version output: $out"
+ echo "$out" | grep -q 'smoke-9.9.9'
+
build-backend:
name: Build & push backend image
- needs: [test, e2e]
+ needs: [test, e2e, version-smoke]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
+ with:
+ fetch-depth: 0 # full history so `git describe --tags` resolves
+ - name: Resolve version
+ id: ver
+ run: echo "version=$(git describe --tags --always)" >> "$GITHUB_OUTPUT"
- uses: docker/setup-qemu-action@v3
- uses: docker/setup-buildx-action@v3
- uses: docker/login-action@v3
@@ -110,6 +129,8 @@ jobs:
platforms: linux/amd64,linux/arm64,linux/arm/v7
push: ${{ github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/') }}
tags: ${{ steps.meta.outputs.tags }}
+ build-args: |
+ VERSION=${{ steps.ver.outputs.version }}
cache-from: type=gha,scope=lyftr-backend
cache-to: type=gha,scope=lyftr-backend,mode=max
@@ -144,13 +165,15 @@ jobs:
deploy-demo:
name: Deploy to Fly.io
- needs: [test, e2e]
+ needs: [test, e2e, version-smoke]
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
+ with:
+ fetch-depth: 0 # full history so `git describe --tags` resolves
- uses: superfly/flyctl-actions/setup-flyctl@master
- name: Deploy
- run: flyctl deploy --remote-only
+ run: flyctl deploy --remote-only --build-arg VERSION="$(git describe --tags --always)"
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
diff --git a/backend/Dockerfile b/backend/Dockerfile
index 865e1b2..50a3f23 100644
--- a/backend/Dockerfile
+++ b/backend/Dockerfile
@@ -3,6 +3,8 @@ FROM --platform=$BUILDPLATFORM golang:1.26-alpine AS builder
ARG TARGETOS
ARG TARGETARCH
+# Version baked into the binary. CI passes the git tag; defaults to "dev" locally.
+ARG VERSION=dev
WORKDIR /build
@@ -10,9 +12,10 @@ WORKDIR /build
COPY go.mod go.sum ./
RUN go mod download
-# Copy source and build a statically linked binary
+# Copy source and build the static binary via the shared build script
+# (single source of the version ldflags — see backend/build.sh)
COPY . .
-RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -ldflags="-s -w" -o lyftr-api .
+RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH VERSION=$VERSION sh build.sh
# Stage 2: Runtime
FROM alpine:3.19
diff --git a/backend/build.sh b/backend/build.sh
new file mode 100755
index 0000000..46e2f7c
--- /dev/null
+++ b/backend/build.sh
@@ -0,0 +1,10 @@
+#!/bin/sh
+# Single source of truth for the backend Go build, shared by backend/Dockerfile and
+# fly/Dockerfile so the version-injection ldflags can't drift between the two images.
+# The cross-compile vars (CGO_ENABLED/GOOS/GOARCH) and VERSION are supplied by the
+# calling RUN's environment; VERSION falls back to "dev" for local/untagged builds.
+set -eu
+
+go build \
+ -ldflags="-s -w -X github.com/Cawlumm/lyftr-backend/config.buildVersion=${VERSION:-dev}" \
+ -o lyftr-api .
diff --git a/backend/config/config.go b/backend/config/config.go
index 27b014e..6aea05a 100644
--- a/backend/config/config.go
+++ b/backend/config/config.go
@@ -8,23 +8,37 @@ import (
)
type Config struct {
- Port string
- DBType string // "sqlite" or "postgres"
- DBPath string // sqlite only
- DBHost string // postgres only
- DBPort string
- DBName string
- DBUser string
- DBPassword string
- JWTSecret string
- JWTExpiry string
- CORSOrigin string
- Env string
- Version string
+ Port string
+ DBType string // "sqlite" or "postgres"
+ DBPath string // sqlite only
+ DBHost string // postgres only
+ DBPort string
+ DBName string
+ DBUser string
+ DBPassword string
+ JWTSecret string
+ JWTExpiry string
+ CORSOrigin string
+ Env string
+ Version string
}
var C *Config
+// buildVersion is set at build time via:
+//
+// -ldflags "-X github.com/Cawlumm/lyftr-backend/config.buildVersion=$VERSION"
+//
+// where the Dockerfiles pass the git tag as the VERSION build-arg. It falls back
+// to "dev" for local/untagged builds. If you rename this var or the package, also
+// update backend/build.sh (the shared ldflags) and the version-smoke CI job, or the
+// injection will silently no-op and ship "dev".
+var buildVersion = "dev"
+
+// Version returns the build-time version. Unlike C.Version it needs no Load(),
+// so the --version flag can report it without touching env or the database.
+func Version() string { return buildVersion }
+
func Load() {
_ = godotenv.Load()
@@ -41,7 +55,7 @@ func Load() {
JWTExpiry: getEnv("JWT_EXPIRY", "3600"),
CORSOrigin: getEnv("CORS_ORIGIN", "http://localhost:5173"),
Env: getEnv("ENV", "development"),
- Version: getEnv("APP_VERSION", "0.1.0"),
+ Version: buildVersion,
}
if C.Env == "production" && C.JWTSecret == "change-me-in-production-min-32-chars!!" {
diff --git a/backend/main.go b/backend/main.go
index 910758c..5bbf6c9 100644
--- a/backend/main.go
+++ b/backend/main.go
@@ -1,7 +1,10 @@
package main
import (
+ "flag"
+ "fmt"
"log"
+ "os"
"github.com/Cawlumm/lyftr-backend/config"
"github.com/Cawlumm/lyftr-backend/db"
@@ -11,6 +14,13 @@ import (
)
func main() {
+ showVersion := flag.Bool("version", false, "print the build version and exit")
+ flag.Parse()
+ if *showVersion {
+ fmt.Printf("lyftr %s\n", config.Version())
+ os.Exit(0)
+ }
+
config.Load()
db.Connect()
seed.DemoUser(db.DB)
diff --git a/fly/Dockerfile b/fly/Dockerfile
index 14cb07e..4a379f0 100644
--- a/fly/Dockerfile
+++ b/fly/Dockerfile
@@ -20,12 +20,15 @@ RUN npm run build
# CGO_ENABLED=0 produces a fully static binary — no libc dependency in runtime.
FROM --platform=$BUILDPLATFORM golang:1.26-alpine AS backend
ARG TARGETOS TARGETARCH
+# Version baked into the binary. The deploy workflow passes the git tag/describe.
+ARG VERSION=dev
WORKDIR /build
COPY backend/go.mod backend/go.sum ./
RUN go mod download
COPY backend/ .
-RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH \
- go build -ldflags="-s -w" -o lyftr-api .
+# Build via backend/build.sh (copied above) — shared with backend/Dockerfile so the
+# version ldflags live in one place and can't drift between the two images.
+RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH VERSION=$VERSION sh build.sh
# ── Stage 3: Runtime ─────────────────────────────────────────────────────────
FROM nginx:alpine
diff --git a/web/package.json b/web/package.json
index 898ff45..7ffe74d 100644
--- a/web/package.json
+++ b/web/package.json
@@ -1,7 +1,7 @@
{
"name": "lyfter-web",
"private": true,
- "version": "0.0.1",
+ "version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
diff --git a/web/src/hooks/useServerInfo.ts b/web/src/hooks/useServerInfo.ts
new file mode 100644
index 0000000..15f44b6
--- /dev/null
+++ b/web/src/hooks/useServerInfo.ts
@@ -0,0 +1,32 @@
+import { useEffect, useState } from 'react'
+import { testServerConnection, type ServerInfo } from '../services/api'
+import { useServerStore } from '../stores/server'
+
+// Fetches the connected backend's /info once per server URL and caches it, so the
+// footer and Settings can show a live version. Returns null while loading or when
+// the server is unreachable — callers render a graceful fallback rather than a
+// stuck "Loading" (the failure mode some apps hit by hard-depending on the fetch).
+const cache = new Map<string, ServerInfo>()
+
+export function useServerInfo(): ServerInfo | null {
+ const base = useServerStore(s => s.serverUrl)
+ const [info, setInfo] = useState<ServerInfo | null>(() => cache.get(base) ?? null)
+
+ useEffect(() => {
+ const cached = cache.get(base)
+ if (cached) {
+ setInfo(cached)
+ return
+ }
+ let active = true
+ testServerConnection(base).then(result => {
+ if (active && result.ok) {
+ cache.set(base, result.info)
+ setInfo(result.info)
+ }
+ })
+ return () => { active = false }
+ }, [base])
+
+ return info
+}
diff --git a/web/src/pages/Login.tsx b/web/src/pages/Login.tsx
index 2be1d93..f43af3b 100644
--- a/web/src/pages/Login.tsx
+++ b/web/src/pages/Login.tsx
@@ -3,12 +3,15 @@ import { useNavigate, Link } from 'react-router-dom'
import { AlertCircle, Zap, Dumbbell, Apple, TrendingUp, LogIn } from 'lucide-react'
import { useAuthStore } from '../stores/auth'
import { apiErrorMessage } from '../services/api'
+import { useServerInfo } from '../hooks/useServerInfo'
+import { formatVersion } from '../utils/version'
import Logo from '../components/Logo'
import ServerSettings from '../components/ServerSettings'
export default function Login() {
const navigate = useNavigate()
const { login } = useAuthStore()
+ const serverInfo = useServerInfo()
const [email, setEmail] = useState('')
const [password, setPassword] = useState('')
@@ -93,7 +96,7 @@ export default function Login() {
{/* Footer */}
<div className="relative text-tx-muted text-xs">
- © lyftr · v0.1.0
+ © lyftr{serverInfo?.version ? ` · ${formatVersion(serverInfo.version)}` : ''}
</div>
</div>
diff --git a/web/src/pages/Register.tsx b/web/src/pages/Register.tsx
index 7ea2df5..53347a2 100644
--- a/web/src/pages/Register.tsx
+++ b/web/src/pages/Register.tsx
@@ -3,12 +3,15 @@ import { useNavigate, Link } from 'react-router-dom'
import { AlertCircle, Dumbbell, Apple, TrendingUp, UserPlus } from 'lucide-react'
import { useAuthStore } from '../stores/auth'
import { apiErrorMessage } from '../services/api'
+import { useServerInfo } from '../hooks/useServerInfo'
+import { formatVersion } from '../utils/version'
import Logo from '../components/Logo'
import ServerSettings from '../components/ServerSettings'
export default function Register() {
const navigate = useNavigate()
const { register } = useAuthStore()
+ const serverInfo = useServerInfo()
const [email, setEmail] = useState('')
const [password, setPassword] = useState('')
@@ -84,7 +87,7 @@ export default function Register() {
{/* Footer */}
<div className="relative text-tx-muted text-xs">
- © lyftr · v0.1.0
+ © lyftr{serverInfo?.version ? ` · ${formatVersion(serverInfo.version)}` : ''}
</div>
</div>
diff --git a/web/src/pages/Settings.tsx b/web/src/pages/Settings.tsx
index f1518ba..dcd8a4e 100644
--- a/web/src/pages/Settings.tsx
+++ b/web/src/pages/Settings.tsx
@@ -1,6 +1,7 @@
import { useState, useEffect, useCallback } from 'react'
import { useAuthStore } from '../stores/auth'
import { useServerStore } from '../stores/server'
+import { useServerInfo } from '../hooks/useServerInfo'
import { useSettingsStore } from '../stores/settings'
import { useTheme } from '../hooks/useTheme'
import { exerciseAPI } from '../services/api'
@@ -40,6 +41,7 @@ function Section({ title, children }: { title: string; children: React.ReactNode
export default function Settings() {
const { user, logout } = useAuthStore()
const serverUrl = useServerStore(s => s.serverUrl)
+ const serverInfo = useServerInfo()
const { theme, toggleTheme } = useTheme()
const { settings: storedSettings, update: updateSettings, fetch: fetchSettings, setWorkoutLayout } = useSettingsStore()
const [loading, setLoading] = useState(!useSettingsStore.getState().loaded)
@@ -301,7 +303,7 @@ export default function Settings() {
<span className="badge-dim">SQLite</span>
</SettingRow>
<SettingRow label="Version" description="lyftr backend version">
- <span className="text-xs text-tx-muted font-mono">v0.1.0</span>
+ <span className="text-xs text-tx-muted font-mono">{serverInfo?.version || '—'}</span>
</SettingRow>
</Section>
diff --git a/web/src/utils/version.ts b/web/src/utils/version.ts
new file mode 100644
index 0000000..c534e5c
--- /dev/null
+++ b/web/src/utils/version.ts
@@ -0,0 +1,10 @@
+/**
+ * `git describe` emits "<tag>-<commits>-g<sha>" for commits past the latest tag
+ * (e.g. on :latest/main builds). Collapse that tooling output to a human-friendly
+ * "<tag> (<sha>)" for display. Exact tags ("v0.1.0-beta.1") and bare commit SHAs
+ * pass through unchanged.
+ */
+export const formatVersion = (raw: string): string => {
+ const m = raw.match(/^(.+)-\d+-g([0-9a-f]+)$/)
+ return m ? `${m[1]} (${m[2]})` : raw
+}