From 76c8d6d0bb388caf210a87359ee4b660deed82ca Mon Sep 17 00:00:00 2001 From: Boshi Lian Date: Sun, 3 Apr 2022 09:45:42 +0000 Subject: [PATCH] go1.18 and nbgv --- .github/workflows/docker-publish.yml | 6 +++++ Dockerfile | 24 ++++++------------ sshpiperd/ldflags.sh | 8 ------ sshpiperd/ver.go | 37 +++++++++++++++++++--------- version.json | 7 ++++++ 5 files changed, 46 insertions(+), 36 deletions(-) delete mode 100755 sshpiperd/ldflags.sh create mode 100644 version.json diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml index 3653a943..64044f5d 100644 --- a/.github/workflows/docker-publish.yml +++ b/.github/workflows/docker-publish.yml @@ -30,6 +30,10 @@ jobs: - name: Checkout repository uses: actions/checkout@v2 + - uses: dotnet/nbgv@master + with: + setAllVars: true + # Login against a Docker registry except on PR # https://github.com/docker/login-action - name: Log into registry ${{ env.REGISTRY }} @@ -55,6 +59,8 @@ jobs: uses: docker/build-push-action@v2 with: context: . + build-args: + - VER=${{ env.NBGV_SimpleVersion }} push: ${{ github.event_name != 'pull_request' }} tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} diff --git a/Dockerfile b/Dockerfile index b795f07e..a5a468fa 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,29 +1,19 @@ -FROM golang:1.17-alpine as builder +FROM golang:1.18-stretch as builder -RUN apk update \ - && apk upgrade \ - && apk add --no-cache \ - ca-certificates git \ - && update-ca-certificates 2>/dev/null +ARG VER=devel -ADD . /go/src/github.com/tg123/sshpiper/ -WORKDIR /go/src/github.com/tg123/sshpiper/sshpiperd -RUN CGO_ENABLED=0 go build -ldflags "$(/go/src/github.com/tg123/sshpiper/sshpiperd/ldflags.sh)" -o /go/bin/sshpiperd +ADD . /src/ +WORKDIR /src/sshpiperd +RUN CGO_ENABLED=0 go build -ldflags "-X main.version=$VER" -o /sshpiperd -FROM alpine:latest +FROM busybox LABEL maintainer="Boshi Lian" -RUN apk update \ - && apk upgrade \ - && apk add --no-cache \ - ca-certificates \ - && update-ca-certificates 2>/dev/null - RUN mkdir /etc/ssh/ ADD entrypoint.sh / -COPY --from=builder /go/bin/sshpiperd / +COPY --from=builder /sshpiperd / EXPOSE 2222 ENTRYPOINT ["/entrypoint.sh"] diff --git a/sshpiperd/ldflags.sh b/sshpiperd/ldflags.sh deleted file mode 100755 index 8f979a7a..00000000 --- a/sshpiperd/ldflags.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/sh - -cd $GOPATH/src/github.com/tg123/sshpiper - -githash=`git log --pretty=format:%h,%ad --name-only --date=short . | head -n 1` -ver=`cat ver` - -echo "-X main.version=$ver -X main.githash=$githash" diff --git a/sshpiperd/ver.go b/sshpiperd/ver.go index 71cdaaf7..ce3fa63c 100644 --- a/sshpiperd/ver.go +++ b/sshpiperd/ver.go @@ -2,12 +2,11 @@ package main import ( "os" - "runtime" + "runtime/debug" "text/template" ) -var version = "DEV" -var githash = "0000000000" +var version = "(devel)" func showVersion() { versionTemplate := template.Must(template.New("ver").Parse(` @@ -17,15 +16,31 @@ https://github.com/tg123/sshpiper Version : {{.VER}} Go Runtime : {{.GOVER}} Git Commit : {{.GITHASH}} +Timestamp : {{.TIMESTAMP}} `[1:])) - _ = versionTemplate.Execute(os.Stdout, struct { - VER string - GOVER string - GITHASH string + data := struct { + VER string + GOVER string + GITHASH string + TIMESTAMP string }{ - VER: version, - GITHASH: githash, - GOVER: runtime.Version(), - }) + VER: version, + } + + bi, ok := debug.ReadBuildInfo() + if ok { + for _, s := range bi.Settings { + switch s.Key { + case "vcs.revision": + data.GITHASH = s.Value[:9] + case "vcs.time": + data.TIMESTAMP = s.Value + } + } + + data.GOVER = bi.GoVersion + } + + _ = versionTemplate.Execute(os.Stdout, data) } diff --git a/version.json b/version.json new file mode 100644 index 00000000..2158b3fb --- /dev/null +++ b/version.json @@ -0,0 +1,7 @@ +{ + "$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json", + "version": "0.7", + "publicReleaseRefSpec": [ + "^refs/heads/master$" + ] +} \ No newline at end of file