Skip to content

Commit

Permalink
Move version.go out of main and use BuildInfo.
Browse files Browse the repository at this point in the history
Move version.go from main to the router module (lib/) so that it can
(later) be accessed from Router itself.

Make it use standard debug.BuildInfo that the Go toolchain produced
automatically, so we no longer have to mess about with shell scripts
and linker flags.

This eliminates some unnecessary differences between the Dockerfile and
Makefile builds.
  • Loading branch information
sengi committed Jul 30, 2023
1 parent c09270e commit ff44a5f
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 41 deletions.
14 changes: 8 additions & 6 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
.git
.gitignore
Dockerfile
README.md
docs
integration_tests
#
# Only files that are untracked by Git should be added here.
#
# The builder container needs to see a pristine checkout, otherwise
# vcs.modified in the BuildInfo will always be true, i.e. the build will always
# be marked as "dirty".
#
/router
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
router
/router
__build
18 changes: 16 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
FROM golang:1.20-alpine AS builder
ARG go_registry=""
ARG go_version=1.20
ARG go_tag_suffix=-alpine

FROM ${go_registry}golang:${go_version}${go_tag_suffix} AS builder
ARG TARGETARCH TARGETOS
ARG GOARCH=$TARGETARCH GOOS=$TARGETOS
ARG CGO_ENABLED=0
ARG GOFLAGS="-trimpath"
ARG go_ldflags="-s -w"
# Go needs git for `-buildvcs`, but the alpine version lacks git :( It's still
# way cheaper to `apk add git` than to pull the Debian-based golang image.
# hadolint ignore=DL3018
RUN apk add --no-cache git
WORKDIR /src
COPY . ./
RUN CGO_ENABLED=0 GOARCH=$TARGETARCH GOOS=$TARGETOS go build -trimpath -ldflags="-s -w"
RUN go build -ldflags="$go_ldflags" && \
./router -version && \
go version -m ./router

FROM scratch
COPY --from=builder /src/router /bin/router
Expand Down
28 changes: 11 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,35 +1,29 @@
.PHONY: all clean build lint test unit_tests integration_tests start_mongo stop_mongo
.PHONY: all clean build test lint unit_tests integration_tests start_mongo stop_mongo
.NOTPARALLEL:

BINARY ?= router
TARGET_MODULE := router
GO_BUILD_ENV := CGO_ENABLED=0
SHELL := /bin/dash

ifdef RELEASE_VERSION
VERSION := $(RELEASE_VERSION)
else
VERSION := $(shell git describe --always | tr -d '\n'; test -z "`git status --porcelain`" || echo '-dirty')
endif

all: build test
all: build

clean:
rm -f $(BINARY)
rm -f $(TARGET_MODULE)

build:
go build -ldflags "-X main.version=$(VERSION)" -o $(BINARY)
env $(GO_BUILD_ENV) go build
./$(TARGET_MODULE) -version

test: lint unit_tests integration_tests

lint:
golangci-lint run

test: lint unit_tests integration_tests

unit_tests: build
unit_tests:
go test -race $$(go list ./... | grep -v integration_tests)

integration_tests: build start_mongo
ROUTER_PUBADDR=localhost:8080 \
ROUTER_APIADDR=localhost:8081 \
go test -race -v ./integration_tests
go test -race -v ./integration_tests

start_mongo:
./mongo.sh start
Expand Down
42 changes: 42 additions & 0 deletions lib/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package router

import (
"fmt"
"runtime/debug"
)

// VersionInfo returns human-readable version information in a format suitable
// for concatenation with other messages.
func VersionInfo() (v string) {
v = "(version info unavailable)"

bi, ok := debug.ReadBuildInfo()
if !ok {
return
}

rev, commitTime, dirty := buildSettings(bi.Settings)
if rev == "" {
return
}

commitTimeOrDirty := "dirty"
if dirty == "false" {
commitTimeOrDirty = commitTime
}
return fmt.Sprintf("built from commit %.8s (%s) using %s", rev, commitTimeOrDirty, bi.GoVersion)
}

func buildSettings(bs []debug.BuildSetting) (rev, commitTime, dirty string) {
for _, b := range bs {
switch b.Key {
case "vcs.modified":
dirty = b.Value
case "vcs.revision":
rev = b.Value
case "vcs.time":
commitTime = b.Value
}
}
return
}
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ ROUTER_BACKEND_HEADER_TIMEOUT=15s Timeout for backend response headers to be re
ROUTER_FRONTEND_READ_TIMEOUT=60s See https://cs.opensource.google/go/go/+/master:src/net/http/server.go?q=symbol:ReadTimeout
ROUTER_FRONTEND_WRITE_TIMEOUT=60s See https://cs.opensource.google/go/go/+/master:src/net/http/server.go?q=symbol:WriteTimeout
`
fmt.Fprintf(os.Stderr, helpstring, versionInfo(), os.Args[0])
fmt.Fprintf(os.Stderr, helpstring, router.VersionInfo(), os.Args[0])
const ErrUsage = 64
os.Exit(ErrUsage)
}
Expand Down Expand Up @@ -89,7 +89,7 @@ func main() {
flag.Usage = usage
flag.Parse()
if *returnVersion {
fmt.Printf("GOV.UK Router %s\n", versionInfo())
fmt.Printf("GOV.UK Router %s\n", router.VersionInfo())
os.Exit(0)
}

Expand Down
13 changes: 0 additions & 13 deletions version.go

This file was deleted.

0 comments on commit ff44a5f

Please sign in to comment.