From 9873f9f4b2fece7a46053eaa375fe24070971e09 Mon Sep 17 00:00:00 2001 From: Jeffrey Cafferata Date: Thu, 4 Jan 2024 16:46:36 +0100 Subject: [PATCH] CICD: GoReleaser version #2 (#2761) --- .github/workflows/pr_test.yml | 24 ++++----------- .github/workflows/release_draft.yml | 13 -------- .goreleaser.yml | 3 +- build/build.go | 3 +- commands/commands.go | 7 +---- commands/writeTypes.go | 8 +++-- main.go | 13 ++------ pkg/version/version.go | 44 ---------------------------- providers/hexonet/hexonetProvider.go | 8 +++-- 9 files changed, 25 insertions(+), 98 deletions(-) delete mode 100644 pkg/version/version.go diff --git a/.github/workflows/pr_test.yml b/.github/workflows/pr_test.yml index f2b8924f0..90416eaef 100644 --- a/.github/workflows/pr_test.yml +++ b/.github/workflows/pr_test.yml @@ -11,12 +11,16 @@ env: jobs: build: runs-on: ubuntu-latest - container: - image: golang:1.21 env: TEST_RESULTS: "/tmp/test-results" steps: - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: stable - name: restore_cache uses: actions/cache@v3.3.2 with: @@ -39,16 +43,6 @@ jobs: run: | go install golang.org/x/tools/cmd/stringer@latest -# For some reason goreleaser isn't correctly setting the version -# string used by "dnscontrol version". Therefore, we're forcing the -# string using the GORELEASER_CURRENT_TAG feature. -# TODO(tlim): Use the native gorelease version mechanism. - - name: Retrieve version - id: version - run: | - echo "TAG_NAME=$(git config --global --add safe.directory /__w/dnscontrol/dnscontrol ; git describe)" >> $GITHUB_OUTPUT - - name: Reveal version - run: echo ${{ steps.version.outputs.TAG_NAME }} - id: build_binaries_tagged name: Build binaries (if tagged) @@ -58,19 +52,13 @@ jobs: distribution: goreleaser version: latest args: build - env: - GORELEASER_CURRENT_TAG: ${{ steps.version.outputs.TAG_NAME }} - id: build_binaries_not_tagged name: Build binaries (not tagged) if: github.ref_type != 'tag' uses: goreleaser/goreleaser-action@v5 with: - distribution: goreleaser - version: latest args: build --snapshot - env: - GORELEASER_CURRENT_TAG: ${{ steps.version.outputs.TAG_NAME }} integration-test-providers: needs: build runs-on: ubuntu-latest diff --git a/.github/workflows/release_draft.yml b/.github/workflows/release_draft.yml index d87e0cdc2..30f5d5007 100644 --- a/.github/workflows/release_draft.yml +++ b/.github/workflows/release_draft.yml @@ -49,18 +49,6 @@ jobs: run: | go install golang.org/x/tools/cmd/stringer@latest -# For some reason goreleaser isn't correctly setting the version -# string used by "dnscontrol version". Therefore, we're forcing the -# string using the GORELEASER_CURRENT_TAG feature. -# TODO(tlim): Use the native gorelease version mechanism. - - - name: Retrieve version - id: version - run: | - echo "TAG_NAME=$(git config --global --add safe.directory /__w/dnscontrol/dnscontrol ; git describe --tags)" >> $GITHUB_OUTPUT - - - name: Reveal version - run: echo ${{ steps.version.outputs.TAG_NAME }} - id: release name: Goreleaser release @@ -71,4 +59,3 @@ jobs: args: release env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - GORELEASER_CURRENT_TAG: ${{ steps.version.outputs.TAG_NAME }} diff --git a/.goreleaser.yml b/.goreleaser.yml index d1b1ffd2e..51a961c84 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -18,7 +18,8 @@ builds: - goos: freebsd goarch: "386" ldflags: - - -linkmode=internal -s -w -X main.Version="{{ .Version }}" -X main.SHA="{{ .FullCommit }}" -X main.BuildTime={{ .Timestamp }} + - -linkmode=internal -s -w + - -X main.version={{ .Version }} before: hooks: - go fmt ./... diff --git a/build/build.go b/build/build.go index 736618f3c..6a5af6a10 100644 --- a/build/build.go +++ b/build/build.go @@ -7,7 +7,6 @@ import ( "os" "os/exec" "strings" - "time" ) var sha = flag.String("sha", "", "SHA of current commit") @@ -16,7 +15,7 @@ var goos = flag.String("os", "", "OS to build (linux, windows, or darwin) Defaul func main() { flag.Parse() - flags := fmt.Sprintf(`-s -w -X "main.SHA=%s" -X main.BuildTime=%d`, getVersion(), time.Now().Unix()) + flags := fmt.Sprintf(`-s -w -X "main.version=%s"`, getVersion()) pkg := "github.com/StackExchange/dnscontrol/v4" build := func(out, goos string) { diff --git a/commands/commands.go b/commands/commands.go index 137591723..b46355b4f 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -25,11 +25,6 @@ const ( var commands = []*cli.Command{} -// These are set by/for goreleaser -var ( - version = "dev" -) - func cmd(cat string, c *cli.Command) bool { c.Category = cat commands = append(commands, c) @@ -52,7 +47,7 @@ func Run(v string) int { app.Version = version app.Name = "dnscontrol" app.HideVersion = true - app.Usage = "dnscontrol is a compiler and DSL for managing dns zones" + app.Usage = "DNSControl is a compiler and DSL for managing dns zones" app.Flags = []cli.Flag{ &cli.BoolFlag{ Name: "v", diff --git a/commands/writeTypes.go b/commands/writeTypes.go index 3a8adedcd..165952783 100644 --- a/commands/writeTypes.go +++ b/commands/writeTypes.go @@ -4,10 +4,14 @@ import ( _ "embed" // Required by go:embed "os" - versionInfo "github.com/StackExchange/dnscontrol/v4/pkg/version" "github.com/urfave/cli/v2" ) +// GoReleaser: version +var ( + version = "dev" +) + var _ = cmd(catUtils, func() *cli.Command { var args TypesArgs return &cli.Command{ @@ -50,7 +54,7 @@ func WriteTypes(args TypesArgs) error { file.WriteString("// This file was automatically generated by DNSControl. Do not edit it directly.\n") file.WriteString("// To update it, run `dnscontrol write-types`.\n\n") - file.WriteString("// DNSControl version: " + versionInfo.Banner() + "\n") + file.WriteString("// " + version + "\n") file.WriteString(dtsContent) if err != nil { return err diff --git a/main.go b/main.go index c86971cb8..9feb5b327 100644 --- a/main.go +++ b/main.go @@ -2,12 +2,10 @@ package main import ( "fmt" - "log" "os" "runtime/debug" "github.com/StackExchange/dnscontrol/v4/commands" - "github.com/StackExchange/dnscontrol/v4/pkg/version" _ "github.com/StackExchange/dnscontrol/v4/providers/_all" "github.com/fatih/color" ) @@ -17,22 +15,17 @@ import ( // Version management. Goals: // 1. Someone who just does "go get" has at least some information. // 2. If built with build/build.go, more specific build information gets put in. +// GoReleaser: version var ( - SHA = "" - Version = "" - BuildTime = "" + version = "dev" ) func main() { - version.SHA = SHA - version.Semver = Version - version.BuildTime = BuildTime if os.Getenv("CI") == "true" { color.NoColor = false } - log.SetFlags(log.LstdFlags | log.Lshortfile) if info, ok := debug.ReadBuildInfo(); !ok && info == nil { fmt.Fprint(os.Stderr, "Warning: dnscontrol was built without Go modules. See https://docs.dnscontrol.org/getting-started/getting-started#source for more information on how to build dnscontrol correctly.\n\n") } - os.Exit(commands.Run("dnscontrol " + version.Banner())) + os.Exit(commands.Run("DNSControl " + version)) } diff --git a/pkg/version/version.go b/pkg/version/version.go deleted file mode 100644 index 2bba701a0..000000000 --- a/pkg/version/version.go +++ /dev/null @@ -1,44 +0,0 @@ -package version - -import ( - "fmt" - "runtime/debug" - "strconv" - "time" -) - -// NOTE: main() updates these. -var ( - BuildTime = "" - SHA = "" - Semver = "" -) - -var versionCache string - -// Banner returns the version banner. -func Banner() string { - if versionCache != "" { - return versionCache - } - - var version string - if SHA != "" { - version = fmt.Sprintf("%s (%s)", Semver, SHA) - } else { - version = fmt.Sprintf("%s-dev", Semver) // no SHA. '0.x.y-dev' indicates it is run from source without build script. - } - if info, ok := debug.ReadBuildInfo(); !ok && info == nil { - version += " (non-modules)" - } - if BuildTime != "" { - i, err := strconv.ParseInt(BuildTime, 10, 64) - if err == nil { - tm := time.Unix(i, 0) - version += fmt.Sprintf(" built %s", tm.Format(time.RFC822)) - } - } - - versionCache = version - return version -} diff --git a/providers/hexonet/hexonetProvider.go b/providers/hexonet/hexonetProvider.go index 1784f1190..a45c2e39c 100644 --- a/providers/hexonet/hexonetProvider.go +++ b/providers/hexonet/hexonetProvider.go @@ -5,11 +5,15 @@ import ( "encoding/json" "fmt" - "github.com/StackExchange/dnscontrol/v4/pkg/version" "github.com/StackExchange/dnscontrol/v4/providers" hxcl "github.com/centralnicgroup-opensource/rtldev-middleware-go-sdk/v3/apiclient" ) +// GoReleaser: version +var ( + version = "dev" +) + // HXClient describes a connection to the hexonet API. type HXClient struct { APILogin string @@ -36,7 +40,7 @@ func newProvider(conf map[string]string) (*HXClient, error) { api := &HXClient{ client: hxcl.NewAPIClient(), } - api.client.SetUserAgent("DNSControl", version.Banner()) + api.client.SetUserAgent("DNSControl", version) api.APILogin, api.APIPassword, api.APIEntity = conf["apilogin"], conf["apipassword"], conf["apientity"] if conf["debugmode"] == "1" { api.client.EnableDebugMode()