Last active
March 26, 2024 02:57
-
-
Save mtilson/7f863cddb6bc303bb61a6aefa47e443c to your computer and use it in GitHub Desktop.
how to include build information in the executable [golang] [makefile]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.PHONY: init | |
init: .git | |
.git: | |
@echo ">>> Git initialization: Start" | |
@git init | |
@git remote add origin [email protected]: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