Skip to content

Commit c8e62e6

Browse files
feat: enhance build process with versioning and configuration improvements
1 parent c269fac commit c8e62e6

File tree

5 files changed

+115
-87
lines changed

5 files changed

+115
-87
lines changed

.github/workflows/build&release.yml

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,15 @@ jobs:
3636
- name: Prepare Build Variables
3737
id: vars
3838
run: |
39-
repo_name=${GITHUB_REPOSITORY##*/}
39+
# Prepare version information
40+
VERSION=$(git describe --tags --always --dirty 2>/dev/null || echo "dev")
41+
GIT_COMMIT=$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")
42+
BUILD_TIME=$(date -u '+%Y-%m-%d_%H:%M:%S')
4043
41-
# For non-tag builds, use commit SHA as identifier
42-
build_id=${GITHUB_SHA::7}
43-
echo "build_id=$build_id" >> $GITHUB_OUTPUT
44+
echo "version=$VERSION" >> $GITHUB_OUTPUT
45+
echo "git_commit=$GIT_COMMIT" >> $GITHUB_OUTPUT
46+
echo "build_time=$BUILD_TIME" >> $GITHUB_OUTPUT
4447
echo "binary_name=supernode-linux-amd64" >> $GITHUB_OUTPUT
45-
46-
# Debug output
47-
echo "Output variables:"
48-
echo "- build_id: $build_id"
49-
echo "- binary_name: supernode-linux-amd64"
5048
5149
- name: Build binary
5250
run: |
@@ -57,17 +55,15 @@ jobs:
5755
GOARCH=amd64 \
5856
go build \
5957
-trimpath \
60-
-ldflags="-s -w" \
58+
-ldflags="-s -w \
59+
-X github.com/LumeraProtocol/supernode/supernode/cmd.Version=${{ steps.vars.outputs.version }} \
60+
-X github.com/LumeraProtocol/supernode/supernode/cmd.GitCommit=${{ steps.vars.outputs.git_commit }} \
61+
-X github.com/LumeraProtocol/supernode/supernode/cmd.BuildTime=${{ steps.vars.outputs.build_time }}" \
6162
-o release/${{ steps.vars.outputs.binary_name }} \
62-
./supernode/main.go
63+
./supernode
6364
64-
# Make binary executable
6565
chmod +x release/${{ steps.vars.outputs.binary_name }}
66-
67-
# Show build results
68-
ls -la release/
6966
70-
# Fix permissions
7167
- name: Fix Release Directory Permissions
7268
run: |
7369
sudo chown -R $USER:$USER release/
@@ -88,25 +84,17 @@ jobs:
8884
- name: Get tag information
8985
id: tag_info
9086
run: |
91-
# Get the tag name
9287
TAG_NAME=${GITHUB_REF#refs/tags/}
9388
echo "tag_name=$TAG_NAME" >> $GITHUB_OUTPUT
9489
95-
# Get the tag message
9690
TAG_MESSAGE=$(git tag -l --format='%(contents)' $TAG_NAME)
97-
# If tag message is empty, use the tag name as message
9891
if [ -z "$TAG_MESSAGE" ]; then
9992
TAG_MESSAGE="Release $TAG_NAME"
10093
fi
101-
# Handle multiline tag messages
10294
TAG_MESSAGE="${TAG_MESSAGE//'%'/'%25'}"
10395
TAG_MESSAGE="${TAG_MESSAGE//$'\n'/'%0A'}"
10496
TAG_MESSAGE="${TAG_MESSAGE//$'\r'/'%0D'}"
10597
echo "tag_message=$TAG_MESSAGE" >> $GITHUB_OUTPUT
106-
107-
# Get the annotated tag commit
108-
TAG_COMMIT=$(git rev-list -n 1 $TAG_NAME)
109-
echo "tag_commit=$TAG_COMMIT" >> $GITHUB_OUTPUT
11098
11199
- name: Configure Git Safe Directory
112100
run: git config --global --add safe.directory $GITHUB_WORKSPACE
@@ -117,7 +105,13 @@ jobs:
117105
- name: Prepare Release Variables
118106
id: vars
119107
run: |
120-
repo_name=${GITHUB_REPOSITORY##*/}
108+
VERSION=${{ steps.tag_info.outputs.tag_name }}
109+
GIT_COMMIT=$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")
110+
BUILD_TIME=$(date -u '+%Y-%m-%d_%H:%M:%S')
111+
112+
echo "version=$VERSION" >> $GITHUB_OUTPUT
113+
echo "git_commit=$GIT_COMMIT" >> $GITHUB_OUTPUT
114+
echo "build_time=$BUILD_TIME" >> $GITHUB_OUTPUT
121115
echo "binary_name=supernode-linux-amd64" >> $GITHUB_OUTPUT
122116
123117
- name: Build Release Version
@@ -129,17 +123,15 @@ jobs:
129123
GOARCH=amd64 \
130124
go build \
131125
-trimpath \
132-
-ldflags="-s -w" \
126+
-ldflags="-s -w \
127+
-X github.com/LumeraProtocol/supernode/supernode/cmd.Version=${{ steps.vars.outputs.version }} \
128+
-X github.com/LumeraProtocol/supernode/supernode/cmd.GitCommit=${{ steps.vars.outputs.git_commit }} \
129+
-X github.com/LumeraProtocol/supernode/supernode/cmd.BuildTime=${{ steps.vars.outputs.build_time }}" \
133130
-o release/${{ steps.vars.outputs.binary_name }} \
134-
./supernode/main.go
131+
./supernode
135132
136-
# Make binary executable
137133
chmod +x release/${{ steps.vars.outputs.binary_name }}
138-
139-
# Show build results
140-
ls -la release/
141134
142-
# Fix permissions
143135
- name: Fix Release Directory Permissions
144136
run: |
145137
sudo chown -R $USER:$USER release/
@@ -155,11 +147,12 @@ jobs:
155147
body: |
156148
${{ steps.tag_info.outputs.tag_message }}
157149
158-
Tag: ${{ steps.tag_info.outputs.tag_name }}
159-
Commit: ${{ steps.tag_info.outputs.tag_commit }}
150+
Version: ${{ steps.vars.outputs.version }}
151+
Git Commit: ${{ steps.vars.outputs.git_commit }}
152+
Build Time: ${{ steps.vars.outputs.build_time }}
160153
161154
Installation:
162-
1. Download the binary
163-
2. Make it executable: `chmod +x supernode-linux-amd64`
164-
3. Run the binary: `./supernode-linux-amd64`
155+
1. Download: `supernode-linux-amd64`
156+
2. Make executable: `chmod +x supernode-linux-amd64`
157+
3. Run: `./supernode-linux-amd64`
165158
token: ${{ secrets.GITHUB_TOKEN }}

Makefile

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,31 @@
1-
.PHONY: test-unit test-integration test-system install-lumera setup-supernodes system-test-setup
1+
.PHONY: test-unit test-integration test-system install-lumera setup-supernodes system-test-setup build build-release
2+
3+
# Build variables
4+
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
5+
GIT_COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
6+
BUILD_TIME ?= $(shell date -u '+%Y-%m-%d_%H:%M:%S')
7+
8+
# Linker flags for version information
9+
LDFLAGS = -X github.com/LumeraProtocol/supernode/supernode/cmd.Version=$(VERSION) \
10+
-X github.com/LumeraProtocol/supernode/supernode/cmd.GitCommit=$(GIT_COMMIT) \
11+
-X github.com/LumeraProtocol/supernode/supernode/cmd.BuildTime=$(BUILD_TIME)
12+
13+
# Development build
14+
build:
15+
go build -ldflags "$(LDFLAGS)" -o supernode ./supernode
16+
17+
# Release build (matches GitHub workflow)
18+
build-release:
19+
@mkdir -p release
20+
CGO_ENABLED=1 \
21+
GOOS=linux \
22+
GOARCH=amd64 \
23+
go build \
24+
-trimpath \
25+
-ldflags="-s -w $(LDFLAGS)" \
26+
-o release/supernode-linux-amd64 \
27+
./supernode
28+
@chmod +x release/supernode-linux-amd64
229

330
# Run unit tests (regular tests with code)
431
test-unit:

supernode/cmd/root.go

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,52 +15,42 @@ var (
1515
appConfig *config.Config
1616
)
1717

18+
const (
19+
DefaultConfigFile = "config.yaml"
20+
DefaultBaseDir = ".supernode"
21+
)
22+
1823
// fileExists checks if a file exists at the given path
1924
func fileExists(path string) bool {
2025
_, err := os.Stat(path)
2126
return err == nil
2227
}
2328

24-
// findConfigFile searches for config files in multiple locations
25-
func findConfigFile() string {
29+
// findConfigFile searches for config files in base directory only
30+
func findConfigFile(baseDirPath string) string {
2631
// If config file is explicitly specified, use it
2732
if cfgFile != "" {
2833
return cfgFile
2934
}
3035

31-
// Try current working directory first (for go run)
32-
workingDir, err := os.Getwd()
33-
if err == nil {
34-
yamlPath := filepath.Join(workingDir, "config.yaml")
35-
ymlPath := filepath.Join(workingDir, "config.yml")
36-
37-
if fileExists(yamlPath) {
38-
return yamlPath
39-
}
40-
if fileExists(ymlPath) {
41-
return ymlPath
36+
// Only search in base directory as default
37+
if baseDirPath != "" {
38+
searchPaths := []string{
39+
filepath.Join(baseDirPath, DefaultConfigFile),
40+
filepath.Join(baseDirPath, "config.yml"),
4241
}
43-
}
44-
45-
// Then try executable directory (for binary)
46-
execPath, err := os.Executable()
47-
if err == nil {
48-
execDir := filepath.Dir(execPath)
49-
yamlPath := filepath.Join(execDir, "config.yaml")
50-
ymlPath := filepath.Join(execDir, "config.yml")
5142

52-
if fileExists(yamlPath) {
53-
return yamlPath
54-
}
55-
if fileExists(ymlPath) {
56-
return ymlPath
43+
// Return first existing config file
44+
for _, path := range searchPaths {
45+
if fileExists(path) {
46+
return path
47+
}
5748
}
5849
}
5950

6051
return ""
6152
}
6253

63-
// setupBaseDir configures the base directory if not specified
6454
func setupBaseDir() (string, error) {
6555
if baseDir != "" {
6656
return baseDir, nil
@@ -71,20 +61,18 @@ func setupBaseDir() (string, error) {
7161
return "", fmt.Errorf("failed to get home directory: %w", err)
7262
}
7363

74-
return filepath.Join(homeDir, ".supernode"), nil
64+
return filepath.Join(homeDir, DefaultBaseDir), nil
7565
}
7666

7767
// logConfig logs information about config and base directory
7868
func logConfig(configPath, baseDirPath string) {
7969
// For config file
80-
absPath, err := filepath.Abs(configPath)
81-
if err == nil {
70+
if absPath, err := filepath.Abs(configPath); err == nil {
8271
fmt.Printf("Using config file: %s\n", absPath)
8372
} else {
8473
fmt.Printf("Using config file: %s\n", configPath)
8574
}
8675

87-
// For base directory
8876
fmt.Printf("Using base directory: %s\n", baseDirPath)
8977
}
9078

@@ -100,16 +88,16 @@ This application allows you to create and recover keys using mnemonics.`,
10088
}
10189

10290
// Setup base directory
103-
setupDir, err := setupBaseDir()
91+
var err error
92+
baseDir, err = setupBaseDir()
10493
if err != nil {
10594
return err
10695
}
107-
baseDir = setupDir
10896

109-
// Find config file
110-
cfgFile = findConfigFile()
97+
// Find config file (only searches in base directory by default)
98+
cfgFile = findConfigFile(baseDir)
11199
if cfgFile == "" {
112-
return fmt.Errorf("no config file found in working directory or executable directory")
100+
return fmt.Errorf("no config file found in base directory (%s)", baseDir)
113101
}
114102

115103
// Log configuration
@@ -134,7 +122,9 @@ func Execute() {
134122
}
135123

136124
func init() {
137-
// Allow user to override config file location with --config flag
138-
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "Config file path (default is ./config.yaml or ./config.yml)")
139-
rootCmd.PersistentFlags().StringVarP(&baseDir, "basedir", "d", "", "Base directory for all data (default is ~/.supernode)")
125+
// Use default values in flag descriptions
126+
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "",
127+
fmt.Sprintf("Config file path (default is ~/%s/%s)", DefaultBaseDir, DefaultConfigFile))
128+
rootCmd.PersistentFlags().StringVarP(&baseDir, "basedir", "d", "",
129+
fmt.Sprintf("Base directory for all data (default is ~/%s)", DefaultBaseDir))
140130
}

supernode/cmd/version.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
var (
10+
// Build-time variables set by go build -ldflags
11+
Version = "dev"
12+
GitCommit = "unknown"
13+
BuildTime = "unknown"
14+
)
15+
16+
// versionCmd represents the version command
17+
var versionCmd = &cobra.Command{
18+
Use: "version",
19+
Short: "Show version information",
20+
Long: `Display version information for the supernode.`,
21+
Run: func(cmd *cobra.Command, args []string) {
22+
fmt.Println(Version)
23+
},
24+
}
25+
26+
func init() {
27+
rootCmd.AddCommand(versionCmd)
28+
}

supernode/main.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,12 @@
11
package main
22

33
import (
4-
"context"
5-
64
"github.com/LumeraProtocol/supernode/pkg/keyring"
7-
"github.com/LumeraProtocol/supernode/pkg/logtrace"
85
"github.com/LumeraProtocol/supernode/supernode/cmd"
96
)
107

118
func main() {
12-
// Create initial context with correlation ID
13-
ctx := logtrace.CtxWithCorrelationID(context.Background(), "supernode-main")
149

15-
// Initialize Cosmos SDK configuration
16-
logtrace.Info(ctx, "Initializing Cosmos SDK configuration", logtrace.Fields{})
1710
keyring.InitSDKConfig()
18-
19-
// Execute root command
20-
logtrace.Info(ctx, "Executing CLI command", logtrace.Fields{})
2111
cmd.Execute()
2212
}

0 commit comments

Comments
 (0)