build: Inject version when build the binaries

The version include git current commit and branch info.
This commit is contained in:
Christian Ditaputratama 2024-07-04 03:45:37 +07:00
parent f69a232c22
commit 0d72dd9995
No known key found for this signature in database
GPG key ID: 31D3D06D77950979
9 changed files with 113 additions and 23 deletions

View file

@ -5,9 +5,9 @@ tmp_dir = "tmp"
[build] [build]
args_bin = [] args_bin = []
bin = "./tmp/main" bin = "./tmp/main"
cmd = "go build -tags server -o ./tmp/main ." cmd = "make dev"
delay = 0 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_file = []
exclude_regex = ["_test.go"] exclude_regex = ["_test.go"]
exclude_unchanged = false exclude_unchanged = false

View file

@ -39,7 +39,7 @@ jobs:
goarch: ${{ matrix.goarch }} goarch: ${{ matrix.goarch }}
overwrite: true overwrite: true
pre_command: export CGO_ENABLED=0 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 build_flags: -tags server
project_path: . project_path: .
binary_name: server binary_name: server
@ -53,7 +53,7 @@ jobs:
goarch: ${{ matrix.goarch }} goarch: ${{ matrix.goarch }}
overwrite: true overwrite: true
pre_command: export CGO_ENABLED=0 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 binary_name: client
project_path: . project_path: .
extra_files: LICENSE README.md extra_files: LICENSE README.md

View file

@ -1,5 +1,39 @@
BINARY_NAME = xmr-nodes 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 .PHONY: build
build: client server build: client server
@ -9,13 +43,21 @@ ui:
.PHONY: client .PHONY: client
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=amd64 GOOS=linux go build \
CGO_ENABLED=0 GOARCH=arm64 GOOS=linux go build -ldflags="-s -w" -o bin/${BINARY_NAME}-client-linux-arm64 -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 .PHONY: server
server: ui 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=amd64 GOOS=linux go build \
CGO_ENABLED=0 GOARCH=arm64 GOOS=linux go build -ldflags="-s -w" -tags server -o bin/${BINARY_NAME}-server-linux-arm64 -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 .PHONY: clean
clean: clean:
@ -26,7 +68,6 @@ clean:
# Deploying new binary file to server and probers host # 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. # 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. # And make sure the inventory and deploy-*.yml file is properly configured.
.PHONY: deploy-server .PHONY: deploy-server
deploy-server: deploy-server:
ansible-playbook -i ./deployment/ansible/inventory.ini -l server ./deployment/ansible/deploy-server.yml -K ansible-playbook -i ./deployment/ansible/inventory.ini -l server ./deployment/ansible/deploy-server.yml -K

View file

@ -8,14 +8,12 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
const AppVer = "0.0.1"
var configFile string var configFile string
var Root = &cobra.Command{ var Root = &cobra.Command{
Use: "xmr-nodes", Use: "xmr-nodes",
Short: "XMR Nodes", Short: "XMR Nodes",
Version: AppVer, Version: config.Version,
} }
func Execute() { func Execute() {

View file

@ -55,7 +55,11 @@ func serve() {
} }
// Define Fiber config & app. // 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 // recover
app.Use(recover.New(recover.Config{EnableStackTrace: true})) 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)) 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",
}
}

View file

@ -1,6 +1,6 @@
{ {
"name": "xmr-nodes-frontend", "name": "xmr-nodes-frontend",
"version": "0.0.1", "version": "v0.0.3",
"private": true, "private": true,
"scripts": { "scripts": {
"dev": "VITE_API_URL=http://127.0.0.1:18901 vite dev --host 127.0.0.1", "dev": "VITE_API_URL=http://127.0.0.1:18901 vite dev --host 127.0.0.1",

View file

@ -1,10 +1,14 @@
<script>
import { version } from '$app/environment';
</script>
<div class="flex w-full items-end border-t border-surface-500/10 bg-surface-50 dark:bg-surface-900"> <div class="flex w-full items-end border-t border-surface-500/10 bg-surface-50 dark:bg-surface-900">
<footer class="w-full"> <footer class="w-full">
<div class="bg-surface-500/5"> <div class="bg-surface-500/5">
<div class="container mx-auto px-5 py-4"> <div class="container mx-auto px-5 py-4">
<!-- prettier-ignore --> <!-- prettier-ignore -->
<p class="text-center text-sm"> <p class="text-center text-sm">
XMR Nodes by <a href="https://www.ditatompel.com" class="anchor">ditatompel.com</a>, XMR Nodes {version} by <a href="https://www.ditatompel.com" class="anchor">ditatompel.com</a>,
<a href="https://github.com/ditatompel/xmr-remote-nodes" class="anchor">source code</a> licensed under <strong>GLWTPL</strong>. <a href="https://github.com/ditatompel/xmr-remote-nodes" class="anchor">source code</a> licensed under <strong>GLWTPL</strong>.
</p> </p>
</div> </div>

View file

@ -1,10 +1,53 @@
import adapter from '@sveltejs/adapter-static'; import adapter from '@sveltejs/adapter-static';
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte'; import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
import * as child_process from 'node:child_process';
import { readFileSync } from 'fs';
import { join } from 'path';
// Helper function to execute shell commands
function execSync(cmd) {
return child_process.execSync(cmd).toString().trim();
}
// Read version from package.json
const packageJsonPath = join(process.cwd(), 'package.json');
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8'));
const VERSION = packageJson.version;
// Retrieve current branch
const BRANCH = execSync('git rev-parse --abbrev-ref HEAD');
// Retrieve current tag if it exists
const RELEASE_TAG = execSync('git tag -l --points-at HEAD');
// Generate version suffix
const commitCount = execSync('git rev-list --count HEAD');
const shortCommitHash = execSync('git show --no-patch --no-notes --pretty="%h" HEAD');
const VERSION_SUFFIX = `-beta.${commitCount}.${shortCommitHash}`;
// Determine branch-specific values
let TAG_BRANCH = `.${BRANCH}`;
if (BRANCH === 'HEAD' || BRANCH === 'main') {
TAG_BRANCH = '';
}
// Determine final tag
let TAG = `${VERSION}${VERSION_SUFFIX}${TAG_BRANCH}`;
if (RELEASE_TAG) {
TAG = RELEASE_TAG;
TAG_BRANCH = 'main';
}
console.log('Building with tag', TAG);
/** @type {import('@sveltejs/kit').Config} */ /** @type {import('@sveltejs/kit').Config} */
const config = { const config = {
preprocess: vitePreprocess(), preprocess: vitePreprocess(),
kit: { kit: {
version: {
name: TAG
},
// paths: { // paths: {
// base: '/' // base: '/'
// }, // },

View file

@ -6,6 +6,8 @@ import (
"strconv" "strconv"
) )
var Version string
type App struct { type App struct {
// general config // general config
LogLevel string LogLevel string
@ -24,6 +26,12 @@ type App struct {
TorSOCKS string TorSOCKS string
} }
func init() {
if Version == "" {
Version = "v0.0.0-alpha.0.000000.dev"
}
}
var app = &App{} var app = &App{}
func AppCfg() *App { func AppCfg() *App {