packer/version/version.go

72 lines
2.2 KiB
Go
Raw Permalink Normal View History

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package version
import (
_ "embed"
"strings"
"github.com/hashicorp/go-version"
2020-12-17 16:29:25 -05:00
pluginVersion "github.com/hashicorp/packer-plugin-sdk/version"
)
2014-10-27 23:51:34 -04:00
var (
// The git commit that was compiled. This will be filled in by the compiler.
GitCommit string
GitDescribe string
// Whether cgo is enabled or not; set at build time
CgoEnabled bool
//go:embed VERSION
rawVersion string
Introduce action-set-product-version for Packer (#12135) This change introduces the new actions-set-product-version, a tiny, but mighty, GitHub action that acts as a bridge between the product repo and our new CRT feature: automated version bumping. tl;dr automated version bumping has a new command (bob update version) in the bob CLI that automatically bumps the version to a new patch. This automation has been introduced to crt-workflows-common as a new workflow (with the new bob command) and handles version bumping at the end of the release pipeline (after being released to production); for example, 1.0.0→1.0.1 and 1.0.0-dev→1.0.0. Bumping the minor version (ie 1.0.x→1.1.0) is only supported manually via bob update version -bump minor, but not supported in CRT (this work is upcoming). This is made possible by adding the new event “bump-version” in the ci.hcl file in this PR. What this small action does: Allows for the static version string from the version/VERSION file to be read by the new CRT workflow and automagically be bumped to the next version (whether it be a minor, or patch, or major version bump). Outputs an error if there’s no VERSION file in the version dir Outputs an error if there’s no version string in the VERSION file Is able to parse product_version if it is 1.3.0-alpha1 as 1.3.0 (example: when product_version = 1.3.0-alpha1, base_version = 1.3.0) Is able to parse prerelease product versions such as alpha1 (example prerelease_product_version = alpha1) in the statement above.
2023-01-19 14:02:09 -05:00
// The next version number that will be released. This will be updated after every release
// Version must conform to the format expected by github.com/hashicorp/go-version
// for tests to work.
// A pre-release marker for the version can also be specified (e.g -dev). If this is omitted
// The main version number that is being run at the moment.
Version string
// A pre-release marker for the version. If this is "" (empty string)
// then it means that it is a final release. Otherwise, this is a pre-release
// such as "dev" (in development), "beta", "rc1", etc.
VersionPrerelease string
// VersionMetadata may be added to give more non-normalised information on a build
// like a commit SHA for example.
//
// Ex: 1.0.0-dev+metadata
VersionMetadata string
)
2020-11-10 17:48:06 -05:00
var PackerVersion *pluginVersion.PluginVersion
func FormattedVersion() string {
return PackerVersion.FormattedVersion()
}
// SemVer is an instance of version.Version. This has the secondary
// benefit of verifying during tests and init time that our version is a
// proper semantic version, which should always be the case.
var SemVer *version.Version
func init() {
rawVersion = strings.TrimSpace(rawVersion)
PackerVersion = pluginVersion.NewRawVersion(rawVersion)
// A bug in the SDK prevents us from calling SemVer on the PluginVersion
// derived from the rawVersion, as when doing so, we reset the semVer
// attribute to only use the core part of the version, thereby dropping any
// information on pre-release/metadata.
SemVer, _ = version.NewVersion(rawVersion)
Version = PackerVersion.GetVersion()
VersionPrerelease = PackerVersion.GetVersionPrerelease()
VersionMetadata = PackerVersion.GetMetadata()
}
// String returns the complete version string, including prerelease
func String() string {
return PackerVersion.String()
}