Skip to content

Instantly share code, notes, and snippets.

@mtilson
Last active March 26, 2024 02:57
Show Gist options
  • Save mtilson/7f863cddb6bc303bb61a6aefa47e443c to your computer and use it in GitHub Desktop.
Save mtilson/7f863cddb6bc303bb61a6aefa47e443c to your computer and use it in GitHub Desktop.
how to include build information in the executable [golang] [makefile]
package main
import "fmt"
var (
buildBranch = "UNKNOWN"
buildCommit = "UNKNOWN"
buildCommitShort = "UNKNOWN"
buildDate = "UNKNOWN"
buildRepo = "UNKNOWN"
buildVersion = "UNKNOWN"
)
func main() {
fmt.Println("branch:", buildBranch)
fmt.Println("commit:", buildCommit)
fmt.Println("commit short:", buildCommitShort)
fmt.Println("build time:", buildDate)
fmt.Println("repo:", buildRepo)
fmt.Println("version:", buildVersion)
}
.PHONY: init
init: .git
.git:
@echo ">>> Git initialization: Start"
@git init
@git remote add origin git@github.com:mtilson/gist-example.git
@git add .
@git commit -m "init"
@git tag v0.1.0
@echo ">>> Git initialization: Finished"
@echo
.PHONY: build
build: init
@go build -ldflags \
"-X main.buildBranch=${shell git rev-parse --abbrev-ref HEAD} \
-X main.buildCommit=${shell git rev-parse HEAD} \
-X main.buildCommitShort=${shell git rev-parse --short HEAD} \
-X main.buildDate=${shell date -u +%Y%m%d.%H%M%S} \
-X main.buildRepo=${shell git config --get remote.origin.url} \
-X main.buildVersion=${shell git describe --tags}" \
main.go
.PHONY: run
run: clean build
@echo ">>> Application: Start"
@./main
@echo ">>> Application: Finished"
.PHONY: clean
clean:
@rm -f ./main
.DEFAULT_GOAL := run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment