From af9dec448dfaf5331d5200d96722e5c648c25624 Mon Sep 17 00:00:00 2001 From: pauhull Date: Fri, 22 Dec 2023 16:33:13 +0100 Subject: [PATCH] ci: assign version using release-please (#654) This PR changes version number output to the following: ### Release ``` $ hcloud version hcloud 1.23.4 ``` ### goreleaser --snapshot ``` $ hcloud version hcloud 1.23.4-dev+ ``` ### go build/go install ``` $ hcloud version hcloud 1.23.4-dev ``` Closes #636 --------- Co-authored-by: jo --- .github/release-please-config.json | 3 ++- .goreleaser.yml | 11 +++++++++-- internal/version/version.go | 19 +++++++++++++++++-- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/.github/release-please-config.json b/.github/release-please-config.json index b3ec043a..25483364 100644 --- a/.github/release-please-config.json +++ b/.github/release-please-config.json @@ -6,7 +6,8 @@ "packages": { ".": { "release-type": "go", - "package-name": "hcloud-cli" + "package-name": "hcloud-cli", + "extra-files": ["internal/version/version.go"] } } } diff --git a/.goreleaser.yml b/.goreleaser.yml index fd17b19f..6a700432 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -7,7 +7,9 @@ builds: main: ./cmd/hcloud/main.go binary: hcloud ldflags: - - -w -X github.com/hetznercloud/cli/internal/version.Version={{.Version}} + - -w + - -X {{ .ModulePath }}/internal/version.version={{ .Version }} + - -X {{ .ModulePath }}/internal/version.versionPrerelease={{- if .IsSnapshot -}}dev+{{ .ShortCommit }}{{- end -}} env: - CGO_ENABLED=0 goos: @@ -27,7 +29,9 @@ builds: main: ./cmd/hcloud/main.go binary: hcloud ldflags: - - -w -X github.com/hetznercloud/cli/internal/version.Version={{.Version}} + - -w + - -X {{ .ModulePath }}/internal/version.version={{ .Version }} + - -X {{ .ModulePath }}/internal/version.versionPrerelease={{- if .IsSnapshot -}}dev+{{ .ShortCommit }}{{- end -}} env: - CGO_ENABLED=0 goos: @@ -40,6 +44,9 @@ builds: - cmd: bash script/gon.sh "{{ .Path }}" output: true +snapshot: + name_template: "{{ .Version }}-dev+{{ .ShortCommit }}" + checksum: name_template: checksums.txt algorithm: sha256 diff --git a/internal/version/version.go b/internal/version/version.go index 489ddf77..841359e0 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -1,4 +1,19 @@ package version -// Version is set via compiler flags (see goreleaser config) -var Version = "unknown" +var ( + // version is a semver version (https://semver.org). + version = "1.41.1" // x-release-please-version + + // versionPrerelease is a semver version pre-release identifier (https://semver.org). + // + // For final releases, we set this to an empty string. + versionPrerelease = "dev" + + // Version of the hcloud CLI. + Version = func() string { + if versionPrerelease != "" { + return version + "-" + versionPrerelease + } + return version + }() +)