From 0d72dd9995c56c374fdf55d12b71274e055bf9ec Mon Sep 17 00:00:00 2001 From: Christian Ditaputratama Date: Thu, 4 Jul 2024 03:45:37 +0700 Subject: [PATCH] build: Inject version when build the binaries The version include git current commit and branch info. --- .air.toml | 4 +- .github/workflows/release.yml | 4 +- Makefile | 51 ++++++++++++++++++++--- cmd/cmd.go | 4 +- cmd/server/serve.go | 14 +++---- frontend/package.json | 2 +- frontend/src/lib/components/Footer.svelte | 6 ++- frontend/svelte.config.js | 43 +++++++++++++++++++ internal/config/app.go | 8 ++++ 9 files changed, 113 insertions(+), 23 deletions(-) diff --git a/.air.toml b/.air.toml index 7bb5528..85efed1 100644 --- a/.air.toml +++ b/.air.toml @@ -5,9 +5,9 @@ tmp_dir = "tmp" [build] args_bin = [] bin = "./tmp/main" - cmd = "go build -tags server -o ./tmp/main ." + cmd = "make dev" delay = 0 - exclude_dir = ["assets", "tmp", "testdata", "frontend/node_modules", "data", "bin", "tools"] + exclude_dir = ["assets", "tmp", "testdata", "frontend/node_modules", "data", "bin"] exclude_file = [] exclude_regex = ["_test.go"] exclude_unchanged = false diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 842f8db..1670b20 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -39,7 +39,7 @@ jobs: goarch: ${{ matrix.goarch }} overwrite: true pre_command: export CGO_ENABLED=0 - ldflags: -s -w + ldflags: -s -w -X xmr-remote-nodes/internal/config.Version=${{github.ref_name}} build_flags: -tags server project_path: . binary_name: server @@ -53,7 +53,7 @@ jobs: goarch: ${{ matrix.goarch }} overwrite: true pre_command: export CGO_ENABLED=0 - ldflags: -s -w + ldflags: -s -w -X xmr-remote-nodes/internal/config.Version=${{github.ref_name}} binary_name: client project_path: . extra_files: LICENSE README.md diff --git a/Makefile b/Makefile index b1600d8..20c0923 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,39 @@ BINARY_NAME = xmr-nodes +# These build are modified version of rclone's Makefile +# https://github.com/rclone/rclone/blob/master/Makefile +VERSION := $(shell cat VERSION) +BRANCH := $(shell git rev-parse --abbrev-ref HEAD) +# Last tag on this branch (eg. v1.0.0) +LAST_TAG := $(shell git describe --tags --abbrev=0) +# Tag of the current commit, if any. If this is not "" then we are building a release +RELEASE_TAG := $(shell git tag -l --points-at HEAD) +# If we are working on a release, override branch to main +ifdef RELEASE_TAG + BRANCH := main + LAST_TAG := $(shell git describe --abbrev=0 --tags $(VERSION)^) +endif +# Make version suffix -beta.NNNN.CCCCCCCC (N=Commit number, C=Commit) +VERSION_SUFFIX := -beta.$(shell git rev-list --count HEAD).$(shell git show --no-patch --no-notes --pretty='%h' HEAD) +TAG_BRANCH := .$(BRANCH) +# If building HEAD or master then unset TAG_BRANCH +ifeq ($(subst HEAD,,$(subst master,,$(BRANCH))),) + TAG_BRANCH := +endif +# TAG is current version + commit number + commit + branch +TAG := $(VERSION)$(VERSION_SUFFIX)$(TAG_BRANCH) +ifdef RELEASE_TAG + TAG := $(RELEASE_TAG) +endif +# end modified rclone's Makefile + +BUILD_LDFLAGS := -s -w -X xmr-remote-nodes/internal/config.Version=$(TAG) + +# This called from air cmd (see .air.toml) +.PHONY: dev +dev: + go build -ldflags="$(BUILD_LDFLAGS)" -tags server -o ./tmp/main . + .PHONY: build build: client server @@ -9,13 +43,21 @@ ui: .PHONY: client client: - CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -ldflags="-s -w" -o bin/${BINARY_NAME}-client-linux-amd64 - CGO_ENABLED=0 GOARCH=arm64 GOOS=linux go build -ldflags="-s -w" -o bin/${BINARY_NAME}-client-linux-arm64 + CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build \ + -ldflags="$(BUILD_LDFLAGS)" \ + -o bin/${BINARY_NAME}-client-linux-amd64 + CGO_ENABLED=0 GOARCH=arm64 GOOS=linux go build \ + -ldflags="$(BUILD_LDFLAGS)" \ + -o bin/${BINARY_NAME}-client-linux-arm64 .PHONY: server server: ui - CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build -ldflags="-s -w" -tags server -o bin/${BINARY_NAME}-server-linux-amd64 - CGO_ENABLED=0 GOARCH=arm64 GOOS=linux go build -ldflags="-s -w" -tags server -o bin/${BINARY_NAME}-server-linux-arm64 + CGO_ENABLED=0 GOARCH=amd64 GOOS=linux go build \ + -ldflags="$(BUILD_LDFLAGS)" -tags server \ + -o bin/${BINARY_NAME}-server-linux-amd64 + CGO_ENABLED=0 GOARCH=arm64 GOOS=linux go build \ + -ldflags="$(BUILD_LDFLAGS)" -tags server \ + -o bin/${BINARY_NAME}-server-linux-arm64 .PHONY: clean clean: @@ -26,7 +68,6 @@ clean: # Deploying new binary file to server and probers host # The deploy-* command doesn't build the binary file, so you need to run `make build` first. # And make sure the inventory and deploy-*.yml file is properly configured. - .PHONY: deploy-server deploy-server: ansible-playbook -i ./deployment/ansible/inventory.ini -l server ./deployment/ansible/deploy-server.yml -K diff --git a/cmd/cmd.go b/cmd/cmd.go index 6ab94ce..2ea6217 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -8,14 +8,12 @@ import ( "github.com/spf13/cobra" ) -const AppVer = "0.0.1" - var configFile string var Root = &cobra.Command{ Use: "xmr-nodes", Short: "XMR Nodes", - Version: AppVer, + Version: config.Version, } func Execute() { diff --git a/cmd/server/serve.go b/cmd/server/serve.go index d7d929f..ffb5a73 100644 --- a/cmd/server/serve.go +++ b/cmd/server/serve.go @@ -55,7 +55,11 @@ func serve() { } // Define Fiber config & app. - app := fiber.New(fiberConfig()) + app := fiber.New(fiber.Config{ + Prefork: appCfg.Prefork, + ProxyHeader: appCfg.ProxyHeader, + AppName: "ditatompel's XMR Nodes HTTP server", + }) // recover app.Use(recover.New(recover.Config{EnableStackTrace: true})) @@ -97,11 +101,3 @@ func serve() { slog.Error(fmt.Sprintf("[HTTP] Server is not running! error: %v", err)) } } - -func fiberConfig() fiber.Config { - return fiber.Config{ - Prefork: config.AppCfg().Prefork, - ProxyHeader: config.AppCfg().ProxyHeader, - AppName: "ditatompel's XMR Nodes HTTP server", - } -} diff --git a/frontend/package.json b/frontend/package.json index c389a6a..40249ca 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -1,6 +1,6 @@ { "name": "xmr-nodes-frontend", - "version": "0.0.1", + "version": "v0.0.3", "private": true, "scripts": { "dev": "VITE_API_URL=http://127.0.0.1:18901 vite dev --host 127.0.0.1", diff --git a/frontend/src/lib/components/Footer.svelte b/frontend/src/lib/components/Footer.svelte index 40068a9..c59921f 100644 --- a/frontend/src/lib/components/Footer.svelte +++ b/frontend/src/lib/components/Footer.svelte @@ -1,10 +1,14 @@ + +