-
Notifications
You must be signed in to change notification settings - Fork 9.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
consolidate version information #32973
Changes from all commits
49e308e
11a68ea
16ae079
1e2a6be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Building from Source | ||
|
||
Pre-built binaries are available for download for a variety of supported platforms through the [HashiCorp Releases website](https://releases.hashicorp.com/terraform/). | ||
|
||
However, if you'd like to build Terraform yourself, you can do so using the Go build toolchain and the options specified in this document. | ||
|
||
## Prerequisites | ||
|
||
1. Ensure you've installed the Go language version specified in [`.go-version`](https://github.com/hashicorp/terraform/blob/main/.go-version). | ||
2. Clone this repository to a location of your choice. | ||
|
||
## Terraform Build Options | ||
|
||
Terraform accepts certain options passed using `ldflags` at build time which control the behavior of the resulting binary. | ||
|
||
### Dev Version Reporting | ||
|
||
Terraform will include a `-dev` flag when reporting its own version (ex: 1.5.0-dev) unless `version.dev` is set to `no`: | ||
|
||
``` | ||
go build -ldflags "-w -s -X 'github.com/hashicorp/terraform/version.dev=no'" -o bin/ . | ||
``` | ||
|
||
### Experimental Features | ||
|
||
Experimental features of Terraform will be disabled unless `main.experimentsAllowed` is set to `yes`: | ||
|
||
``` | ||
go build -ldflags "-w -s -X 'main.experimentsAllowed=yes'" -o bin/ . | ||
``` | ||
|
||
In the official build process for Terraform, experiments are only allowed in alpha release builds. We recommend that third-party distributors follow that convention in order to reduce user confusion. | ||
|
||
## Go Options | ||
|
||
For the most part, the Terraform release process relies on the Go toolchain defaults for the target operating system and processor architecture. | ||
|
||
### `CGO_ENABLED` | ||
|
||
One exception is the `CGO_ENABLED` option, which is set explicitly when building Terraform binaries. For most platforms, we build with `CGO_ENABLED=0` in order to produce a statically linked binary. For MacOS/Darwin operating systems, we build with `CGO_ENABLED=1` to avoid a platform-specific issue with DNS resolution. | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.6.0-dev | ||
1.6.0-alpha1 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,26 +8,45 @@ | |
package version | ||
|
||
import ( | ||
_ "embed" | ||
"fmt" | ||
"strings" | ||
|
||
version "github.com/hashicorp/go-version" | ||
) | ||
|
||
// The main version number that is being run at the moment. | ||
var Version = "1.6.0" | ||
// rawVersion is the current version as a string, as read from the VERSION | ||
// file. This must be a valid semantic version. | ||
// | ||
//go:embed VERSION | ||
var rawVersion string | ||
|
||
// dev determines whether the -dev prerelease marker will | ||
// be included in version info. It is expected to be set to "no" using | ||
// linker flags when building binaries for release. | ||
var dev string = "yes" | ||
|
||
// 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. | ||
var Prerelease = "dev" | ||
// The main version number that is being run at the moment, populated from the raw version. | ||
var Version string | ||
|
||
// 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. | ||
// A pre-release marker for the version, populated using a combination of the raw version | ||
// and the dev flag. | ||
var Prerelease string | ||
|
||
// SemVer is an instance of version.Version representing the main version | ||
// without any prerelease information. | ||
var SemVer *version.Version | ||
|
||
func init() { | ||
SemVer = version.Must(version.NewVersion(Version)) | ||
semVerFull := version.Must(version.NewVersion(strings.TrimSpace(rawVersion))) | ||
SemVer = semVerFull.Core() | ||
Version = SemVer.String() | ||
|
||
if dev == "no" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there is already a prelease set in VERSION (e.g There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dropping any existing prerelease in favor of |
||
Prerelease = semVerFull.Prerelease() | ||
} else { | ||
Prerelease = "dev" | ||
} | ||
} | ||
|
||
// Header is the header name used to send the current terraform version | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package version | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
// Smoke test to validate that the version file can be read correctly and all exported | ||
// variables include the expected information. | ||
func TestVersion(t *testing.T) { | ||
if match, _ := regexp.MatchString("[^\\d+\\.]", Version); match != false { | ||
t.Fatalf("Version should contain only the main version") | ||
} | ||
|
||
if match, _ := regexp.MatchString("[^a-z\\d]", Prerelease); match != false { | ||
t.Fatalf("Prerelease should contain only letters and numbers") | ||
} | ||
|
||
if SemVer.Prerelease() != "" { | ||
t.Fatalf("SemVer should not include prerelease information") | ||
} | ||
|
||
if !strings.Contains(String(), Prerelease) { | ||
t.Fatalf("Full version string should include prerelease information") | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe worth calling out here that we only do this for alpha release builds, and recommend third-party distributors follow that same convention to reduce confusion.