From 4f3b3f5c0b00b8c47c7e7d8e6c2dda624e114cde Mon Sep 17 00:00:00 2001 From: Tobi Smethurst <31960611+tsmethurst@users.noreply.github.com> Date: Mon, 28 Jun 2021 12:17:20 +0200 Subject: [PATCH] put version in binary properly (#73) Addresses #71 : Set version on the CLI framework. Add a build.sh script that injects variables into the build tooling using git and a version file. Set version in config. --- CONTRIBUTING.md | 2 +- Dockerfile | 11 ++++++++++- GETTINGSTARTED.md | 6 +++++- build.sh | 8 ++++++++ cmd/gotosocial/main.go | 11 +++++++++-- internal/config/config.go | 4 +++- internal/config/default.go | 4 ---- version | 1 + web/template/footer.tmpl | 2 +- 9 files changed, 38 insertions(+), 11 deletions(-) create mode 100755 build.sh create mode 100644 version diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index f6fafea..c09832b 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -30,7 +30,7 @@ To get started, you first need to have Go installed. GTS was developed with Go 1 Once you've got go installed, clone this repository into your Go path. Normally, this should be `~/go/src/github.com/superseriousbusiness/gotosocial`. -Once that's done, you can try building the project: `go build ./cmd/gotosocial`. This will build the `gotosocial` binary. +Once that's done, you can try building the project: `./build.sh`. This will build the `gotosocial` binary. If there are no errors, great, you're good to go! diff --git a/Dockerfile b/Dockerfile index 4fa961e..fe94ef5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,6 @@ FROM golang:1.16.4-alpine3.13 AS builder +RUN apk update && apk upgrade --no-cache +RUN apk add git # create build dir RUN mkdir -p /go/src/github.com/superseriousbusiness/gotosocial @@ -11,8 +13,15 @@ ADD testrig /go/src/github.com/superseriousbusiness/gotosocial/testrig ADD go.mod /go/src/github.com/superseriousbusiness/gotosocial/go.mod ADD go.sum /go/src/github.com/superseriousbusiness/gotosocial/go.sum +# move .git dir and version for versioning +ADD .git /go/src/github.com/superseriousbusiness/gotosocial/.git +ADD version /go/src/github.com/superseriousbusiness/gotosocial/version + +# move the build script +ADD build.sh /go/src/github.com/superseriousbusiness/gotosocial/build.sh + # do the build step -RUN go build ./cmd/gotosocial +RUN ./build.sh FROM alpine:3.13 AS executor RUN apk update && apk upgrade --no-cache diff --git a/GETTINGSTARTED.md b/GETTINGSTARTED.md index 758c0e0..be5ab23 100644 --- a/GETTINGSTARTED.md +++ b/GETTINGSTARTED.md @@ -30,7 +30,11 @@ docker run -d --network host --user postgres -e POSTGRES_PASSWORD=some_password ### 5: Build the Binary -On your local machine (not your server), with Go installed, clone the GoToSocial repository, and build the binary with `go build ./cmd/gotosocial`. +On your local machine (not your server), with Go installed, clone the GoToSocial repository, and build the binary with the provided build script: + +```bash +./build/sh +``` ### 6: Prepare VPS diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..18a927e --- /dev/null +++ b/build.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +set -eu + +export COMMIT=$(git rev-list -1 HEAD) +export VERSION=$(cat ./version) + +go build -ldflags="-X 'main.Commit=$COMMIT' -X 'main.Version=$VERSION'" ./cmd/gotosocial diff --git a/cmd/gotosocial/main.go b/cmd/gotosocial/main.go index 27810a8..9729f77 100644 --- a/cmd/gotosocial/main.go +++ b/cmd/gotosocial/main.go @@ -33,12 +33,19 @@ import ( "github.com/urfave/cli/v2" ) +// Version is the software version of GtS being used +var Version string + +// Commit is the git commit of GtS being used +var Commit string + func main() { flagNames := config.GetFlagNames() envNames := config.GetEnvNames() defaults := config.GetDefaults() app := &cli.App{ - Usage: "a fediverse social media server", + Version: Version + " " + Commit[:7], + Usage: "a fediverse social media server", Flags: []cli.Flag{ // GENERAL FLAGS &cli.StringFlag{ @@ -399,7 +406,7 @@ func runAction(c *cli.Context, a cliactions.GTSAction) error { return fmt.Errorf("error creating config: %s", err) } // ... and the flags set on the *cli.Context by urfave - if err := conf.ParseCLIFlags(c); err != nil { + if err := conf.ParseCLIFlags(c, c.App.Version); err != nil { return fmt.Errorf("error parsing config: %s", err) } diff --git a/internal/config/config.go b/internal/config/config.go index 3705c36..28bbc85 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -106,7 +106,7 @@ func loadFromFile(path string) (*Config, error) { } // ParseCLIFlags sets flags on the config using the provided Flags object -func (c *Config) ParseCLIFlags(f KeyedFlags) error { +func (c *Config) ParseCLIFlags(f KeyedFlags, version string) error { fn := GetFlagNames() // For all of these flags, we only want to set them on the config if: @@ -261,6 +261,8 @@ func (c *Config) ParseCLIFlags(f KeyedFlags) error { c.AccountCLIFlags[EmailFlag] = f.String(EmailFlag) c.AccountCLIFlags[PasswordFlag] = f.String(PasswordFlag) + c.SoftwareVersion = version + return nil } diff --git a/internal/config/default.go b/internal/config/default.go index 2cc46c9..40df4c5 100644 --- a/internal/config/default.go +++ b/internal/config/default.go @@ -1,7 +1,5 @@ package config -const softwareVersion = "0.1.0-SNAPSHOT" - // TestDefault returns a default config for testing func TestDefault() *Config { defaults := GetTestDefaults() @@ -121,7 +119,6 @@ func GetDefaults() Defaults { ConfigPath: "", Host: "", Protocol: "https", - SoftwareVersion: softwareVersion, DbType: "postgres", DbAddress: "localhost", @@ -168,7 +165,6 @@ func GetTestDefaults() Defaults { ConfigPath: "", Host: "localhost:8080", Protocol: "http", - SoftwareVersion: softwareVersion, DbType: "postgres", DbAddress: "localhost", diff --git a/version b/version new file mode 100644 index 0000000..4ecb664 --- /dev/null +++ b/version @@ -0,0 +1 @@ +0.1.0-SNAPSHOT \ No newline at end of file diff --git a/web/template/footer.tmpl b/web/template/footer.tmpl index 88f4cd8..5aba0e1 100644 --- a/web/template/footer.tmpl +++ b/web/template/footer.tmpl @@ -1,7 +1,7 @@