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.
This commit is contained in:
Tobi Smethurst 2021-06-28 12:17:20 +02:00 committed by GitHub
parent 87cf621e21
commit 4f3b3f5c0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 38 additions and 11 deletions

View File

@ -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 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! If there are no errors, great, you're good to go!

View File

@ -1,4 +1,6 @@
FROM golang:1.16.4-alpine3.13 AS builder FROM golang:1.16.4-alpine3.13 AS builder
RUN apk update && apk upgrade --no-cache
RUN apk add git
# create build dir # create build dir
RUN mkdir -p /go/src/github.com/superseriousbusiness/gotosocial 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.mod /go/src/github.com/superseriousbusiness/gotosocial/go.mod
ADD go.sum /go/src/github.com/superseriousbusiness/gotosocial/go.sum 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 # do the build step
RUN go build ./cmd/gotosocial RUN ./build.sh
FROM alpine:3.13 AS executor FROM alpine:3.13 AS executor
RUN apk update && apk upgrade --no-cache RUN apk update && apk upgrade --no-cache

View File

@ -30,7 +30,11 @@ docker run -d --network host --user postgres -e POSTGRES_PASSWORD=some_password
### 5: Build the Binary ### 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 ### 6: Prepare VPS

8
build.sh Executable file
View File

@ -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

View File

@ -33,12 +33,19 @@ import (
"github.com/urfave/cli/v2" "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() { func main() {
flagNames := config.GetFlagNames() flagNames := config.GetFlagNames()
envNames := config.GetEnvNames() envNames := config.GetEnvNames()
defaults := config.GetDefaults() defaults := config.GetDefaults()
app := &cli.App{ app := &cli.App{
Usage: "a fediverse social media server", Version: Version + " " + Commit[:7],
Usage: "a fediverse social media server",
Flags: []cli.Flag{ Flags: []cli.Flag{
// GENERAL FLAGS // GENERAL FLAGS
&cli.StringFlag{ &cli.StringFlag{
@ -399,7 +406,7 @@ func runAction(c *cli.Context, a cliactions.GTSAction) error {
return fmt.Errorf("error creating config: %s", err) return fmt.Errorf("error creating config: %s", err)
} }
// ... and the flags set on the *cli.Context by urfave // ... 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) return fmt.Errorf("error parsing config: %s", err)
} }

View File

@ -106,7 +106,7 @@ func loadFromFile(path string) (*Config, error) {
} }
// ParseCLIFlags sets flags on the config using the provided Flags object // 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() fn := GetFlagNames()
// For all of these flags, we only want to set them on the config if: // 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[EmailFlag] = f.String(EmailFlag)
c.AccountCLIFlags[PasswordFlag] = f.String(PasswordFlag) c.AccountCLIFlags[PasswordFlag] = f.String(PasswordFlag)
c.SoftwareVersion = version
return nil return nil
} }

View File

@ -1,7 +1,5 @@
package config package config
const softwareVersion = "0.1.0-SNAPSHOT"
// TestDefault returns a default config for testing // TestDefault returns a default config for testing
func TestDefault() *Config { func TestDefault() *Config {
defaults := GetTestDefaults() defaults := GetTestDefaults()
@ -121,7 +119,6 @@ func GetDefaults() Defaults {
ConfigPath: "", ConfigPath: "",
Host: "", Host: "",
Protocol: "https", Protocol: "https",
SoftwareVersion: softwareVersion,
DbType: "postgres", DbType: "postgres",
DbAddress: "localhost", DbAddress: "localhost",
@ -168,7 +165,6 @@ func GetTestDefaults() Defaults {
ConfigPath: "", ConfigPath: "",
Host: "localhost:8080", Host: "localhost:8080",
Protocol: "http", Protocol: "http",
SoftwareVersion: softwareVersion,
DbType: "postgres", DbType: "postgres",
DbAddress: "localhost", DbAddress: "localhost",

1
version Normal file
View File

@ -0,0 +1 @@
0.1.0-SNAPSHOT

View File

@ -1,7 +1,7 @@
<!-- footer.tmpl --> <!-- footer.tmpl -->
<footer> <footer>
<div id="version"> <div id="version">
Running GoToSocial version: <span class="accent">{{.instance.Version}}</span><br> GoToSocial: <span class="accent">{{.instance.Version}}</span><br>
<a href="https://github.com/superseriousbusiness/gotosocial">Source Code</a> <a href="https://github.com/superseriousbusiness/gotosocial">Source Code</a>
</div> </div>
<div id="contact"> <div id="contact">