Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

270 add ldflags to dagger #273

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added first version of Makefile; include ldflags; tests, dependencies
Signed-off-by: Patrick Eschenbach <[email protected]>
qcserestipy committed Nov 28, 2024

Verified

This commit was signed with the committer’s verified signature.
adellibovi Alfredo Delli Bovi
commit 008ded2d4b246e17dbcf8be07de48ccfefae7a43
78 changes: 78 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Makefile for building the Harbor CLI project

# Variables
BINARY_NAME=harbor
BUILD_DIR=bin
SRC_DIR=cmd/harbor
MAIN_FILE=$(SRC_DIR)/main.go

# Retrieve the current Git commit hash
GIT_COMMIT := $(shell git rev-parse --short HEAD)

# Retrieve the current Git tag or fallback to describe
VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo "v0.0.1")

# Go version
GO_VERSION := $(shell go version | awk '{print $$3}')

# Build time
BUILD_TIME := $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")

# System information
SYSTEM := $(shell go env GOOS)/$(shell go env GOARCH)

# Output directory
OUTPUT_DIR := $(BUILD_DIR)

# ldflags for embedding version information
LDFLAGS := -X github.com/goharbor/harbor-cli/cmd/harbor/internal/version.Version=$(VERSION) \
-X github.com/goharbor/harbor-cli/cmd/harbor/internal/version.GoVersion=$(GO_VERSION) \
-X github.com/goharbor/harbor-cli/cmd/harbor/internal/version.GitCommit=$(GIT_COMMIT) \
-X github.com/goharbor/harbor-cli/cmd/harbor/internal/version.BuildTime=$(BUILD_TIME) \
-X github.com/goharbor/harbor-cli/cmd/harbor/internal/version.System=$(SYSTEM)

# Default target
.PHONY: all
all: build

# Build the binary with ldflags
.PHONY: build
build:
@echo "Building $(BINARY_NAME)..."
@mkdir -p $(OUTPUT_DIR)
@echo "ldflags: $(LDFLAGS)"
go build -ldflags "$(LDFLAGS)" -o $(OUTPUT_DIR)/$(BINARY_NAME) $(MAIN_FILE)
@echo "Build complete: $(OUTPUT_DIR)/$(BINARY_NAME)"

# Clean the build artifacts
.PHONY: clean
clean:
@echo "Cleaning build artifacts..."
@rm -rf $(BUILD_DIR)
@echo "Clean complete."

# Display version information
.PHONY: version
version:
@./$(OUTPUT_DIR)/$(BINARY_NAME) version

# Install dependencies
.PHONY: deps
deps:
go mod download

# Run tests
.PHONY: test
test:
@pushd ./test/e2e && go test -v && popd

# Help message
.PHONY: help
help:
@echo "Available targets:"
@echo " build - Build the binary with version information"
@echo " clean - Remove build artifacts"
@echo " version - Display the version of the built binary"
@echo " deps - Download and install dependencies"
@echo " test - Run tests"
@echo " help - Show this help message"