diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 3c06550..149ac4e 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -30,7 +30,9 @@ jobs: steps: - uses: actions/checkout@v1 - name: Build image - run: docker build . --tag image + run: | + echo ${GITHUB_REF##*/} | tee ci-version.txt + docker build . --tag image - name: Log into registry run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login docker.pkg.github.com -u ${GITHUB_ACTOR} --password-stdin - name: Push image diff --git a/Dockerfile b/Dockerfile index 275a30c..974fcaf 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,7 +5,11 @@ ENV GOPROXY=https://proxy.golang.org CGO_ENABLED=0 COPY go.mod go.sum ./ RUN go mod download COPY . ./ -RUN go build -ldflags='-s -w' -o bitmapist-server && upx --lzma bitmapist-server +RUN test -s ci-version.txt && \ + go build -ldflags='-s -w' -o bitmapist-server \ + -ldflags="-X=main.explicitVersion=$(cat ci-version.txt)" || \ + go build -ldflags='-s -w' -o bitmapist-server \ + && upx --lzma bitmapist-server FROM scratch COPY --from=builder /app/bitmapist-server . diff --git a/main.go b/main.go index 0e22f2c..e53fbfb 100644 --- a/main.go +++ b/main.go @@ -3,11 +3,13 @@ package main import ( "flag" + "fmt" "io/ioutil" "log" "os" "os/signal" "path/filepath" + "runtime/debug" "syscall" "time" @@ -16,6 +18,8 @@ import ( "github.com/artyom/red" ) +var explicitVersion string // to be set by CI with -ldflags="-X=main.explicitVersion=v1.2.3" + func main() { args := struct { Addr string `flag:"addr,address to listen"` @@ -27,7 +31,21 @@ func main() { File: "bitmapist.db", } autoflags.Define(&args) + var versionOnly bool + flag.BoolVar(&versionOnly, "v", versionOnly, "print version and exit") + flag.BoolVar(&versionOnly, "version", versionOnly, "print version and exit") flag.Parse() + if versionOnly || (len(os.Args) == 2 && os.Args[1] == "version") { + v := "unknown" + if explicitVersion != "" { + v = explicitVersion + } else if bi, ok := debug.ReadBuildInfo(); ok { + v = bi.Main.Version + } + fmt.Printf("bitmapist-server %s\n", v) + return + } + log := log.New(os.Stderr, "", log.LstdFlags) log.Println("loading data from", args.File)