diff --git a/.woodpecker/docker.yaml b/.woodpecker/docker.yaml index 8616b4d..1a9a2e6 100644 --- a/.woodpecker/docker.yaml +++ b/.woodpecker/docker.yaml @@ -8,6 +8,8 @@ steps: settings: registry: git.unkin.net repo: git.unkin.net/unkin/artifactapi + build_args: + VERSION: ${CI_COMMIT_TAG} username: droneci password: from_secret: DRONECI_PASSWORD diff --git a/Dockerfile b/Dockerfile index 1bd2552..c6d9db9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,7 +9,8 @@ RUN go mod download COPY . . -RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o artifactapi ./cmd/artifactapi +ARG VERSION=dev +RUN CGO_ENABLED=0 go build -ldflags="-s -w -X main.version=${VERSION}" -o artifactapi ./cmd/artifactapi FROM gcr.io/distroless/static-debian12:nonroot diff --git a/Makefile b/Makefile index df68c20..e2d1889 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ check-go: fi build: check-go tidy - go build -ldflags="-s -w" -o $(BINARY) ./cmd/artifactapi + go build -ldflags="-s -w -X main.version=$(VERSION)" -o $(BINARY) ./cmd/artifactapi test: check-go go test -race -count=1 ./pkg/... ./internal/... diff --git a/cmd/artifactapi/main.go b/cmd/artifactapi/main.go index d5529ed..22edc02 100644 --- a/cmd/artifactapi/main.go +++ b/cmd/artifactapi/main.go @@ -13,6 +13,8 @@ import ( "git.unkin.net/unkin/artifactapi/internal/tui" ) +var version = "dev" + func main() { if len(os.Args) > 1 && os.Args[1] == "tui" { endpoint := os.Getenv("ARTIFACTAPI_ENDPOINT") @@ -42,7 +44,7 @@ func main() { ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) defer cancel() - srv, err := server.New(cfg) + srv, err := server.New(cfg, version) if err != nil { slog.Error("failed to create server", "error", err) os.Exit(1) diff --git a/internal/server/server.go b/internal/server/server.go index 00ea708..1a93ebd 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -35,6 +35,7 @@ import ( type Server struct { cfg *config.Config + version string router chi.Router db *database.DB cache *cache.Redis @@ -45,7 +46,7 @@ type Server struct { gc *gc.Collector } -func New(cfg *config.Config) (*Server, error) { +func New(cfg *config.Config, version string) (*Server, error) { db, err := database.New(cfg.DatabaseDSN()) if err != nil { return nil, fmt.Errorf("database: %w", err) @@ -68,6 +69,7 @@ func New(cfg *config.Config) (*Server, error) { s := &Server{ cfg: cfg, + version: version, db: db, cache: redis, store: s3, @@ -138,7 +140,7 @@ func (s *Server) handleHealth(w http.ResponseWriter, r *http.Request) { func (s *Server) handleRoot(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) - fmt.Fprint(w, `{"name":"artifactapi","version":"3.0.0-dev"}`) + fmt.Fprintf(w, `{"name":"artifactapi","version":"%s"}`, s.version) } func (s *Server) newHTTPServer() *http.Server {