diff --git a/.github/workflows/release-please.yml b/.github/workflows/release-please.yml index 15eb203..05ab7a5 100644 --- a/.github/workflows/release-please.yml +++ b/.github/workflows/release-please.yml @@ -20,5 +20,8 @@ jobs: release-type: go package-name: GLAuth path: v2 - default-branch: dev + default-branch: master + pull-request-title-pattern: "ci: release ${version}" token: ${{ secrets.RELEASE_PLEASE_TOKEN }} + extra-files: | + v2/internal/version/const.go diff --git a/v2/Makefile b/v2/Makefile index 9c053c9..720dbe0 100644 --- a/v2/Makefile +++ b/v2/Makefile @@ -16,7 +16,7 @@ GIT_IS_TAG_COMMIT=$(shell git describe --abbrev=0 --tags > /dev/null 2> /dev/nul GIT_BRANCH=$(shell git rev-parse --abbrev-ref HEAD) # Build variables -BUILD_VARS?=-s -w -X main.GitCommit=${GIT_COMMIT} -X main.GitBranch=${GIT_BRANCH} -X main.BuildTime=${BUILD_TIME} -X main.GitClean=${GIT_CLEAN} -X main.LastGitTag=${LAST_GIT_TAG} -X main.GitTagIsCommit=${GIT_IS_TAG_COMMIT} +BUILD_VARS?=-s -w BUILD_FILES=. TRIM_FLAGS>?=-trimpath @@ -87,7 +87,7 @@ mkbindir: .PHONY: mkbindir build: mkbindir - @go build ${TRIM_FLAGS} -ldflags "${BUILD_VARS}" -o bin/$(GOOS)$(GOARCH)/glauth -buildvcs . + @go build ${TRIM_FLAGS} -ldflags "${BUILD_VARS}" -o bin/$(GOOS)$(GOARCH)/glauth -buildvcs=false . $(MAKE) sha256 .PHONY: build diff --git a/v2/glauth.go b/v2/glauth.go index c518e2e..1fea784 100644 --- a/v2/glauth.go +++ b/v2/glauth.go @@ -7,6 +7,7 @@ import ( "github.com/arl/statsviz" docopt "github.com/docopt/docopt-go" "github.com/fsnotify/fsnotify" + "github.com/glauth/glauth/v2/internal/version" "github.com/glauth/glauth/v2/pkg/config" "github.com/glauth/glauth/v2/pkg/frontend" "github.com/glauth/glauth/v2/pkg/logging" @@ -25,14 +26,6 @@ import ( "time" ) -// Set with buildtime vars -var LastGitTag string -var BuildTime string -var GitCommit string -var GitClean string -var GitBranch string -var GitTagIsCommit string - const programName = "glauth" var usage = `glauth: securely expose your LDAP for external auth @@ -65,48 +58,6 @@ var ( activeConfig = &config.Config{} ) -// Reads builtime vars and returns a full string containing info about -// the currently running version of the software. Primarily used by the -// --version flag at runtime. -func getVersionString() string { - - var versionstr string - - versionstr = "GLauth" - - // Notate the git context of the build - switch { - // If a release, use the tag - case GitClean == "1" && GitTagIsCommit == "1": - versionstr += " " + LastGitTag + "\n\n" - - // If this branch had a tag before, mention the branch and the tag to give a rough idea of the base version - case len(GitBranch) > 1 && len(LastGitTag) > 1: - versionstr += "\nNon-release build from branch " + GitBranch + ", based on tag " + LastGitTag + "\n\n" - - // If no previous tag specified, just mention the branch - case len(GitBranch) > 1: - versionstr += "\nNon-release build from branch " + GitBranch + "\n\n" - - // Fallback message, if all else fails - default: - versionstr += "\nNon-release build\n\n" - } - - // Include build time - if len(BuildTime) > 1 { - versionstr += "Build time: " + BuildTime + "\n" - } - - // Add commit hash - if GitClean == "1" && len(GitCommit) > 1 { - versionstr += "Commit: " + GitCommit + "\n" - } - - return versionstr - -} - func main() { if err := parseArgs(); err != nil { fmt.Println("Could not parse command-line arguments") @@ -135,7 +86,7 @@ func main() { func startService() { // stats - stats.General.Set("version", stats.Stringer(LastGitTag)) + stats.General.Set("version", stats.Stringer(version.Version)) // web API if activeConfig.API.Enabled { @@ -250,7 +201,7 @@ func startConfigWatcher() { func parseArgs() error { var err error - if args, err = docopt.Parse(usage, nil, true, getVersionString(), false); err != nil { + if args, err = docopt.Parse(usage, nil, true, version.GetVersion(), false); err != nil { return err } diff --git a/v2/internal/version/const.go b/v2/internal/version/const.go new file mode 100644 index 0000000..e25acef --- /dev/null +++ b/v2/internal/version/const.go @@ -0,0 +1,5 @@ +package version + +// Version is the tag of the latest release, it gets changed by the ci +// process when a release is happening +const Version = "v2.3.0" // x-release-please-version diff --git a/v2/internal/version/vcs.go b/v2/internal/version/vcs.go new file mode 100644 index 0000000..05c0778 --- /dev/null +++ b/v2/internal/version/vcs.go @@ -0,0 +1,68 @@ +package version + +import ( + "fmt" + "runtime/debug" +) + +type gitInfo struct { + BuildTime string + Commit string + Dirty bool +} + +// GetVersion reads builtime vars and returns a full string containing info about +// the currently running version of the software. Primarily used by the +// --version flag at runtime. +func GetVersion() string { + + // TODO @shipperizer see if it's worth replacing with info.Main.Path + // aka github.com/glauth/glauth/v2 - not great but worth checking + + name := "GLauth" + + info, ok := debug.ReadBuildInfo() + + if !ok { + return fmt.Sprintf(` +%s +Non-release build`, + name) + } + + gitInfo := vcsInfo(info.Settings) + + // If a release, use the tag + if !gitInfo.Dirty { + return fmt.Sprintf(`%s %s +Build time: %s +Commit: %s`, name, Version, gitInfo.BuildTime, gitInfo.Commit) + } + + return fmt.Sprintf(`%s +Non-release build based on tag %s +Build time: %s +Commit: %s`, name, Version, gitInfo.BuildTime, gitInfo.Commit) + +} + +func vcsInfo(settings []debug.BuildSetting) *gitInfo { + info := new(gitInfo) + + info.BuildTime = "unknown" + info.Commit = "unknown" + info.Dirty = false + + for _, v := range settings { + switch v.Key { + case "vcs.revision": + info.Commit = v.Value + case "vcs.modified": + info.Dirty = v.Value == "true" + case "vcs.time": + info.BuildTime = v.Value + } + } + + return info +}