From fcc8e2752e44c461c9327f22bbe8643b9b750067 Mon Sep 17 00:00:00 2001 From: Dennis Trautwein Date: Fri, 3 May 2024 17:10:30 +0200 Subject: [PATCH] refactor: goreleaser configuration --- .github/workflows/goreleaser.yml | 30 +++++++++++++++++++++ Makefile | 16 ++++++------ README.md | 8 ++++++ cmd/nebula/cmd.go | 15 ++++++++--- config/config.go | 45 +++++++++++++++++++++++++++----- version | 1 - 6 files changed, 95 insertions(+), 20 deletions(-) create mode 100644 .github/workflows/goreleaser.yml delete mode 100644 version diff --git a/.github/workflows/goreleaser.yml b/.github/workflows/goreleaser.yml new file mode 100644 index 00000000..16c3344a --- /dev/null +++ b/.github/workflows/goreleaser.yml @@ -0,0 +1,30 @@ +name: goreleaser + +on: + push: + tags: + - '*' + +permissions: + contents: write + +jobs: + goreleaser: + runs-on: ubuntu-latest + steps: + - name: Checking out repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Setting up Go + uses: actions/setup-go@v4 + + - name: Running GoReleaser + uses: goreleaser/goreleaser-action@v5 + with: + distribution: goreleaser + version: latest + args: release --clean + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/Makefile b/Makefile index 9f7173ce..89a8499e 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,7 @@ -default: all +GIT_SHA := $(shell git rev-parse --short HEAD) +DATE := $(shell date "+%Y-%m-%dT%H:%M:%SZ") +USER := $(shell id -un) +VERSION := $(shell git describe --tags --abbrev=0) all: clean build @@ -8,10 +11,7 @@ test: go test `go list ./... | grep -v maxmind | grep -v discvx` build: - go build -ldflags "-X main.RawVersion=`cat version`" -o dist/nebula github.com/dennis-tra/nebula-crawler/cmd/nebula - -build-linux: - GOOS=linux GOARCH=amd64 make build + go build -ldflags "-X main.version=${VERSION} -X main.commit=${GIT_SHA} -X main.date=${DATE} -X main.builtBy=${USER}" -o dist/nebula github.com/dennis-tra/nebula-crawler/cmd/nebula format: gofumpt -w -l . @@ -20,13 +20,13 @@ clean: rm -r dist || true docker: - docker build -t dennistra/nebula:latest -t dennistra/nebula:`cat version` . + docker build -t dennistra/nebula:latest -t dennistra/nebula:${GIT_SHA} . docker-linux: - docker build --platform linux/amd64 -t 019120760881.dkr.ecr.us-east-1.amazonaws.com/probelab:nebula-sha6bc3e96 . + docker build --platform linux/amd64 -t 019120760881.dkr.ecr.us-east-1.amazonaws.com/probelab:nebula-sha${GIT_SHA} . docker-push: docker-linux - docker push dennistra/nebula:latest dennistra/nebula:`cat version` + docker push dennistra/nebula:latest dennistra/nebula:${GIT_SHA} tools: go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@v4.15.2 diff --git a/README.md b/README.md index a86736fc..81bbe51a 100644 --- a/README.md +++ b/README.md @@ -372,6 +372,14 @@ make database make test ``` +## Release Checklist + +- [ ] Merge everything into `main` +- [ ] Create a new tag with the new version +- [ ] Push tag to GitHub + +This will trigger the [`goreleaser.yml`](./.github/workflows/goreleaser.yml) workflow which pushes creates a new _draft_ release in GitHub. + ## Related Efforts - [`wiberlin/ipfs-crawler`](https://github.com/wiberlin/ipfs-crawler) - A crawler for the IPFS network, code for their paper ([arXiv](https://arxiv.org/abs/2002.07747)). diff --git a/cmd/nebula/cmd.go b/cmd/nebula/cmd.go index a47d4199..357cf1df 100644 --- a/cmd/nebula/cmd.go +++ b/cmd/nebula/cmd.go @@ -24,11 +24,14 @@ const ( flagCategoryNetwork = "Network Specific Configuration:" ) -// RawVersion and build tag of the Nebula command line tool. -var RawVersion = "dev" +var ( + version = "dev" + commit = "none" + date = "unknown" + builtBy = "local" +) var rootConfig = &config.Root{ - RawVersion: RawVersion, Debug: false, LogLevel: 4, LogFormat: "text", @@ -52,6 +55,10 @@ var rootConfig = &config.Root{ ProtocolsCacheSize: 100, ProtocolsSetCacheSize: 200, }, + RawVersion: version, + BuildCommit: commit, + BuildDate: date, + BuiltBy: builtBy, } func main() { @@ -65,7 +72,7 @@ func main() { Email: "nebula@dtrautwein.eu", }, }, - Version: rootConfig.Version(), + Version: fmt.Sprintf("v%s (%s)", rootConfig.Version(), rootConfig.BuildAuthor()), Before: Before, Flags: []cli.Flag{ &cli.BoolFlag{ diff --git a/config/config.go b/config/config.go index ab966c37..86db84d5 100644 --- a/config/config.go +++ b/config/config.go @@ -5,6 +5,7 @@ import ( "fmt" "runtime/debug" "strconv" + "strings" "time" "github.com/ethereum/go-ethereum/p2p/enode" @@ -68,9 +69,6 @@ const ( // Root contains general user configuration. type Root struct { - // The version string of nebula - RawVersion string - // Enables debug logging (equivalent to log level 5) Debug bool @@ -109,6 +107,18 @@ type Root struct { // TracerProvider is the tracer provider to use when initialising tracing TracerProvider trace.TracerProvider + + // The raw version of Nebula in the for X.Y.Z. Raw, because it's missing, e.g., commit information (set by GoReleaser or in Makefile) + RawVersion string + + // The commit hash used to build the Nebula binary (set by GoReleaser or in Makefile) + BuildCommit string + + // The date when Nebula was built (set by GoReleaser or in Makefile) + BuildDate string + + // Who built Nebula (set by GoReleaser or in Makefile) + BuiltBy string } // Version returns the actual version string which includes VCS information @@ -119,17 +129,27 @@ func (r *Root) Version() string { for _, setting := range info.Settings { switch setting.Key { case "vcs.revision": - shortCommit = setting.Value[:7] + shortCommit = setting.Value + if len(shortCommit) > 8 { + shortCommit = shortCommit[:8] + } case "vcs.modified": dirty, _ = strconv.ParseBool(setting.Value) } } } - versionStr := "v" + r.RawVersion + versionStr := r.RawVersion - if shortCommit != "" { - versionStr += "+" + shortCommit + if r.BuildCommit != "" { + if len(r.BuildCommit) > 8 { + r.BuildCommit = r.BuildCommit[:8] + } + shortCommit = r.BuildCommit + } + + if !strings.HasSuffix(versionStr, shortCommit) { + versionStr += "-" + shortCommit } if dirty { @@ -139,6 +159,17 @@ func (r *Root) Version() string { return versionStr } +func (r *Root) BuildAuthor() string { + if r.BuildDate != "" && r.BuiltBy != "" { + return fmt.Sprintf("built at %s by %s", r.BuildDate, r.BuiltBy) + } else if r.BuildDate != "" { + return fmt.Sprintf("built at %s", r.BuildDate) + } else if r.BuiltBy != "" { + return fmt.Sprintf("built by %s", r.BuiltBy) + } + return "" +} + // String prints the configuration as a json string func (r *Root) String() string { data, _ := json.MarshalIndent(r, "", " ") diff --git a/version b/version deleted file mode 100644 index ccbccc3d..00000000 --- a/version +++ /dev/null @@ -1 +0,0 @@ -2.2.0