From b2e02759177b81fd3279b42061189b08966b6076 Mon Sep 17 00:00:00 2001 From: eikendev Date: Sat, 22 Feb 2025 23:56:13 +0100 Subject: [PATCH] Read binary version using BuildInfo --- Dockerfile | 4 +--- Makefile | 8 +------- cmd/pushbits/main.go | 13 +++++++------ 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/Dockerfile b/Dockerfile index 263a440..9cacfbe 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,5 @@ FROM docker.io/library/golang:alpine as builder -ARG PB_BUILD_VERSION - ARG CLI_VERSION=0.0.6 ARG CLI_PLATFORM=linux_amd64 @@ -13,7 +11,7 @@ RUN set -ex \ && apk add --no-cache build-base ca-certificates curl \ && go mod download \ && go mod verify \ - && PB_BUILD_VERSION="$PB_BUILD_VERSION" make build \ + && make build \ && chmod +x /build/out/pushbits \ && curl -q -s -S -L -o /tmp/pbcli_${CLI_VERSION}.tar.gz https://github.com/pushbits/cli/releases/download/v${CLI_VERSION}/pbcli_${CLI_VERSION}_${CLI_PLATFORM}.tar.gz \ && tar -C /usr/local/bin -xvf /tmp/pbcli_${CLI_VERSION}.tar.gz pbcli \ diff --git a/Makefile b/Makefile index 3709877..218b0cf 100644 --- a/Makefile +++ b/Makefile @@ -5,15 +5,10 @@ TESTS_DIR := ./tests GO_FILES := $(shell find . -type f \( -iname '*.go' \)) GO_MODULE := github.com/pushbits/server -PB_BUILD_VERSION ?= $(shell git describe --tags) -ifeq ($(PB_BUILD_VERSION),) - _ := $(error Cannot determine build version) -endif - .PHONY: build build: mkdir -p $(OUT_DIR) - go build -ldflags="-w -s -X main.version=$(PB_BUILD_VERSION)" -o $(OUT_DIR)/pushbits ./cmd/pushbits + go build -ldflags="-w -s" -o $(OUT_DIR)/pushbits ./cmd/pushbits .PHONY: clean clean: @@ -63,7 +58,6 @@ swag: build .PHONY: docker_build_dev docker_build_dev: podman build \ - --build-arg=PB_BUILD_VERSION=dev \ -t local/pushbits . .PHONY: run_postgres_debug diff --git a/cmd/pushbits/main.go b/cmd/pushbits/main.go index db313b8..d0f4654 100644 --- a/cmd/pushbits/main.go +++ b/cmd/pushbits/main.go @@ -4,6 +4,7 @@ package main import ( "os" "os/signal" + "runtime/debug" "syscall" "github.com/pushbits/server/internal/authentication/credentials" @@ -15,8 +16,6 @@ import ( "github.com/pushbits/server/internal/runner" ) -var version string - func setupCleanup(db *database.Database, dp *dispatcher.Dispatcher) { c := make(chan os.Signal, 2) signal.Notify(c, os.Interrupt, syscall.SIGTERM) @@ -30,11 +29,13 @@ func setupCleanup(db *database.Database, dp *dispatcher.Dispatcher) { } func printStarupMessage() { - if len(version) == 0 { - log.L.Panic("Version not set") - } else { - log.L.Printf("Starting PushBits %s", version) + buildInfo, ok := debug.ReadBuildInfo() + if !ok { + log.L.Fatalln("Build info not available") + return } + + log.L.Printf("Starting PushBits %s", buildInfo.Main.Version) } // @title PushBits Server API Documentation