Skip to content

Commit 03b001b

Browse files
committed
Read Go build info from the binary
1 parent bd7d4b6 commit 03b001b

File tree

6 files changed

+39
-18
lines changed

6 files changed

+39
-18
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ jobs:
3636
name: Checks and variables
3737
runs-on: ubuntu-20.04
3838
outputs:
39-
sha: ${{ steps.vars.outputs.sha }}
4039
go_path: ${{ steps.vars.outputs.go_path }}
4140
steps:
4241
- name: Checkout Repository
@@ -49,7 +48,6 @@ jobs:
4948
- name: Output Variables
5049
id: vars
5150
run: |
52-
echo "::set-output name=sha::$(echo ${GITHUB_SHA} | cut -c1-7)"
5351
echo "::set-output name=go_path::$(go env GOPATH)"
5452
- name: Check if go.mod and go.sum are up to date
5553
run: |
@@ -75,7 +73,7 @@ jobs:
7573
- name: Upload Coverage Report
7674
uses: actions/upload-artifact@v3
7775
with:
78-
name: cover-${{ needs.vars.outputs.sha }}.html
76+
name: cover-${{ github.run_id }}.html
7977
path: ${{ github.workspace }}/cover.html
8078
if: always()
8179

.goreleaser.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ builds:
1414
- all=-trimpath={{.Env.GOPATH}}
1515
asmflags:
1616
- all=-trimpath={{.Env.GOPATH}}
17+
ldflags:
18+
- -s -w -X main.version={{.Version}}
1719
main: ./cmd/gateway/
1820
binary: gateway
1921

Makefile

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ VERSION = 0.0.1
22
TAG = $(VERSION)
33
PREFIX ?= nginx-kubernetes-gateway
44

5-
GIT_COMMIT = $(shell git rev-parse HEAD)
6-
DATE = $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
7-
85
TARGET ?= local
96

107
KIND_KUBE_CONFIG_FOLDER = $${HOME}/.kube/kind
@@ -14,12 +11,12 @@ export DOCKER_BUILDKIT = 1
1411

1512
.PHONY: container
1613
container: build
17-
docker build --build-arg VERSION=$(VERSION) --build-arg GIT_COMMIT=$(GIT_COMMIT) --build-arg DATE=$(DATE) --target $(TARGET) -f build/Dockerfile -t $(PREFIX):$(TAG) .
14+
docker build --build-arg VERSION=$(VERSION) --target $(TARGET) -f build/Dockerfile -t $(PREFIX):$(TAG) .
1815

1916
.PHONY: build
2017
build:
2118
ifeq (${TARGET},local)
22-
CGO_ENABLED=0 GOOS=linux go build -trimpath -a -ldflags "-s -w -X main.version=${VERSION} -X main.commit=${GIT_COMMIT} -X main.date=${DATE}" -o $(OUT_DIR)/gateway github.com/nginxinc/nginx-kubernetes-gateway/cmd/gateway
19+
CGO_ENABLED=0 GOOS=linux go build -trimpath -a -ldflags "-s -w -X main.version=${VERSION}" -o $(OUT_DIR)/gateway github.com/nginxinc/nginx-kubernetes-gateway/cmd/gateway
2320
endif
2421

2522
.PHONY: generate

build/Dockerfile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# syntax=docker/dockerfile:1.4
22
FROM golang:1.19 as builder
33
ARG VERSION
4-
ARG GIT_COMMIT
5-
ARG DATE
64

75
WORKDIR /go/src/github.com/nginxinc/nginx-kubernetes-gateway/cmd/gateway
86

@@ -12,7 +10,7 @@ RUN go mod download
1210
COPY cmd /go/src/github.com/nginxinc/nginx-kubernetes-gateway/cmd
1311
COPY internal /go/src/github.com/nginxinc/nginx-kubernetes-gateway/internal
1412
COPY pkg /go/src/github.com/nginxinc/nginx-kubernetes-gateway/pkg
15-
RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -a -ldflags "-s -w -X main.version=${VERSION} -X main.commit=${GIT_COMMIT} -X main.date=${DATE}" -o gateway .
13+
RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -a -ldflags "-s -w -X main.version=${VERSION}" -o gateway .
1614

1715
FROM alpine:3.16 as capabilizer
1816
RUN apk add --no-cache libcap

cmd/gateway/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ const (
1818
var (
1919
// Set during go build
2020
version string
21-
commit string
22-
date string
2321

2422
// Command-line flags
2523
gatewayCtlrName = flag.String(
@@ -50,9 +48,11 @@ func main() {
5048
GatewayClassParam(),
5149
)
5250

51+
commit, date, dirty := getBuildInfo()
5352
logger.Info("Starting NGINX Kubernetes Gateway",
5453
"version", version,
5554
"commit", commit,
55+
"dirty", dirty,
5656
"date", date)
5757

5858
err := manager.Start(conf)

cmd/gateway/setup.go

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"os"
7+
"runtime/debug"
78
"strings"
89

910
flag "github.com/spf13/pflag"
@@ -14,11 +15,13 @@ const (
1415
errTmpl = "failed validation - flag: '--%s' reason: '%s'\n"
1516
)
1617

17-
type Validator func(*flag.FlagSet) error
18-
type ValidatorContext struct {
19-
Key string
20-
V Validator
21-
}
18+
type (
19+
Validator func(*flag.FlagSet) error
20+
ValidatorContext struct {
21+
Key string
22+
V Validator
23+
}
24+
)
2225

2326
func GatewayControllerParam(domain string, namespace string) ValidatorContext {
2427
name := "gateway-ctlr-name"
@@ -120,3 +123,26 @@ func MustValidateArguments(flagset *flag.FlagSet, validators ...ValidatorContext
120123
os.Exit(1)
121124
}
122125
}
126+
127+
func getBuildInfo() (commitHash string, commitTime string, dirtyBuild bool) {
128+
commitHash = "unknown"
129+
commitTime = "unknown"
130+
dirtyBuild = true
131+
132+
info, ok := debug.ReadBuildInfo()
133+
if !ok {
134+
return
135+
}
136+
for _, kv := range info.Settings {
137+
switch kv.Key {
138+
case "vcs.revision":
139+
commitHash = kv.Value
140+
case "vcs.time":
141+
commitTime = kv.Value
142+
case "vcs.modified":
143+
dirtyBuild = kv.Value == "true"
144+
}
145+
}
146+
147+
return
148+
}

0 commit comments

Comments
 (0)