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

fix: use embedded files instead of ldflags to get the build info during runtime #2421

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ vendor
/cel-key
coverage.txt
go.work

# embedded files
cmd/celestia/*.txt
17 changes: 15 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
SHELL=/usr/bin/env bash
PROJECTNAME=$(shell basename "$(PWD)")
LDFLAGS=-ldflags="-X 'main.buildTime=$(shell date)' -X 'main.lastCommit=$(shell git rev-parse HEAD)' -X 'main.semanticVersion=$(shell git describe --tags --dirty=-dev 2>/dev/null || git rev-parse --abbrev-ref HEAD)'"
DOCKER_TAG := $(shell git rev-parse --short=7 HEAD)
LDFLAGS=-ldflags="-X 'main.buildTime=$(shell date)'"
ifeq (${PREFIX},)
PREFIX := /usr/local
endif
Expand All @@ -16,8 +17,14 @@ install-hooks:
@git config core.hooksPath .githooks
.PHONY: install-hooks

## gen-embed: Generate the embed files for celestia-node binary.
gen-embed:
@printf '%s' "$$(git describe --tags --abbrev=0 --dirty=-dev 2>/dev/null || git rev-parse --abbrev-ref HEAD)" > cmd/celestia/semanticVersion.txt
@printf '%s' "$$(git rev-parse HEAD)" > cmd/celestia/lastCommit.txt
.PHONY: gen-embed

## build: Build celestia-node binary.
build:
build: gen-embed
@echo "--> Building Celestia"
@go build -o build/ ${LDFLAGS} ./cmd/celestia
.PHONY: build
Expand Down Expand Up @@ -174,3 +181,9 @@ adr-gen:
@echo "--> Generating ADR"
@curl -sSL https://raw.githubusercontent.com/celestiaorg/.github/main/adr-template.md > docs/architecture/adr-$(NUM)-$(TITLE).md
.PHONY: adr-gen

## docker-build: Builds the Docker container
docker-build:
@echo "--> Docker build... [$(DOCKER_TAG)]"
@docker build -t ghcr.io/celestiaorg/$(PROJECTNAME):$(DOCKER_TAG) .
.PHONY: docker-build
8 changes: 6 additions & 2 deletions cmd/celestia/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import (
"fmt"
"runtime"

_ "embed"

"github.com/spf13/cobra"
)

var (
buildTime string
lastCommit string
buildTime string
//go:embed lastCommit.txt
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to document this lastCommit.txt here.
And exclude it from the the go test executor

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see my commetn

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm the no build directive for tests seems to not work @jrmanes do you mind looking into it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can take a look 👌

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to fix the tests, we can either add the files lastCommit.txt& semanticVersion.txt to the repo (they can be empty), or run the make gen-embed before make test, but if we run go test ./... it will fail if these files are not there
wdyt?

lastCommit string
//go:embed semanticVersion.txt
semanticVersion string

systemVersion = fmt.Sprintf("%s/%s", runtime.GOARCH, runtime.GOOS)
Expand Down