Просмотр исходного кода

Merge pull request #56 from mdaniel/embed-git-commit

Embed the git commit and emit it on startup
Ajay Tripathy 7 лет назад
Родитель
Сommit
79a7d89da2
2 измененных файлов с 17 добавлено и 1 удалено
  1. 11 1
      Dockerfile
  2. 6 0
      main.go

+ 11 - 1
Dockerfile

@@ -15,7 +15,17 @@ RUN go mod download
 # COPY the source code as the last step
 COPY . .
 # Build the binary
-RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -o /go/bin/app
+RUN set -e ;\
+    GIT_COMMIT=`git rev-parse HEAD` ;\
+    GIT_DIRTY='' ;\
+    # for our purposes, we only care about dirty .go files ;\
+    if test -n "`git status --porcelain --untracked-files=no | grep '\.go'`"; then \
+      GIT_DIRTY='+dirty' ;\
+    fi ;\
+    CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
+    go build -a -installsuffix cgo \
+        -ldflags "-X main.gitCommit=${GIT_COMMIT}${GIT_DIRTY}" \
+        -o /go/bin/app
 
 FROM alpine:3.4
 RUN apk add --update --no-cache ca-certificates

+ 6 - 0
main.go

@@ -29,6 +29,11 @@ const (
 	prometheusServerEndpointEnvVar = "PROMETHEUS_SERVER_ENDPOINT"
 )
 
+var (
+	// gitCommit is set by the build system
+	gitCommit string
+)
+
 type Accesses struct {
 	PrometheusClient       prometheusClient.Client
 	KubeClientSet          kubernetes.Interface
@@ -160,6 +165,7 @@ func main() {
 	klog.InitFlags(nil)
 	flag.Set("v", "3")
 	flag.Parse()
+	klog.V(1).Infof("Starting cost-model (git commit \"%s\")", gitCommit)
 
 	address := os.Getenv(prometheusServerEndpointEnvVar)
 	if address == "" {