Skip to content

Commit bd68789

Browse files
committed
core: Use environment variables to set VersionPrerelease at compile time
Instead of using a hardcoded version prerelease string, which makes release automation difficult, set the version prerelease string from an environment variable via the go linker tool during compile time. The environment variable `TF_RELEASE` should only be set via the `make bin` target, and thus leaves the version prerelease string unset. Otherwise, when running a local compile of terraform via the `make dev` makefile target, the version prerelease string is set to `"dev"`, as usual. This also requires some changes to both the circonus and postgresql providers, as they directly used the `VersionPrerelease` constant. We now simply call the `VersionString()` function, which returns the proper interpolated version string with the prerelease string populated correctly. `TF_RELEASE` is unset: ```sh $ make dev ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2017/05/22 10:38:19 Generated command/internal_plugin_list.go ==> Removing old directory... ==> Building... Number of parallel builds: 3 --> linux/amd64: github.com/hashicorp/terraform ==> Results: total 209M -rwxr-xr-x 1 jake jake 209M May 22 10:39 terraform $ terraform version Terraform v0.9.6-dev (fd472e4+CHANGES) ``` `TF_RELEASE` is set (mimicking the `make bin` target): ```sh $ TF_RELEASE=1 make dev ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2017/05/22 10:40:39 Generated command/internal_plugin_list.go ==> Removing old directory... ==> Building... Number of parallel builds: 3 --> linux/amd64: github.com/hashicorp/terraform ==> Results: total 121M -rwxr-xr-x 1 jake jake 121M May 22 10:42 terraform $ terraform version Terraform v0.9.6 ```
1 parent fd472e4 commit bd68789

5 files changed

Lines changed: 8 additions & 24 deletions

File tree

‎builtin/providers/circonus/provider.go‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package circonus
22

33
import (
4-
"bytes"
54
"fmt"
65

76
"github.com/circonus-labs/circonus-gometrics/api"
@@ -123,13 +122,5 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
123122
}
124123

125124
func tfAppName() string {
126-
const VersionPrerelease = terraform.VersionPrerelease
127-
var versionString bytes.Buffer
128-
129-
fmt.Fprintf(&versionString, "Terraform v%s", terraform.Version)
130-
if VersionPrerelease != "" {
131-
fmt.Fprintf(&versionString, "-%s", VersionPrerelease)
132-
}
133-
134-
return versionString.String()
125+
return fmt.Sprintf("Terraform v%s", terraform.VersionString())
135126
}

‎builtin/providers/postgresql/provider.go‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package postgresql
22

33
import (
4-
"bytes"
54
"fmt"
65

76
"github.com/hashicorp/errwrap"
@@ -109,13 +108,5 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
109108
}
110109

111110
func tfAppName() string {
112-
const VersionPrerelease = terraform.VersionPrerelease
113-
var versionString bytes.Buffer
114-
115-
fmt.Fprintf(&versionString, "Terraform v%s", terraform.Version)
116-
if terraform.VersionPrerelease != "" {
117-
fmt.Fprintf(&versionString, "-%s", terraform.VersionPrerelease)
118-
}
119-
120-
return versionString.String()
111+
return fmt.Sprintf("Terraform v%s", terraform.VersionString())
121112
}

‎scripts/build.sh‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ fi
4040
export CGO_ENABLED=0
4141

4242
# Allow LD_FLAGS to be appended during development compilations
43-
LD_FLAGS="-X main.GitCommit=${GIT_COMMIT}${GIT_DIRTY} $LD_FLAGS"
44-
# In relase mode we don't want debug information in the binary
43+
LD_FLAGS="-X main.GitCommit=${GIT_COMMIT}${GIT_DIRTY} -X github.com/hashicorp/terraform/terraform.VersionPrerelease=dev $LD_FLAGS"
44+
45+
# In release mode we don't want debug information in the binary
4546
if [[ -n "${TF_RELEASE}" ]]; then
4647
LD_FLAGS="-X main.GitCommit=${GIT_COMMIT}${GIT_DIRTY} -s -w"
4748
fi

‎terraform/version.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const Version = "0.9.6"
1212
// A pre-release marker for the version. If this is "" (empty string)
1313
// then it means that it is a final release. Otherwise, this is a pre-release
1414
// such as "dev" (in development), "beta", "rc1", etc.
15-
const VersionPrerelease = "dev"
15+
var VersionPrerelease string
1616

1717
// SemVersion is an instance of version.Version. This has the secondary
1818
// benefit of verifying during tests and init time that our version is a

‎version.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ import "github.com/hashicorp/terraform/terraform"
66
var GitCommit string
77

88
const Version = terraform.Version
9-
const VersionPrerelease = terraform.VersionPrerelease
9+
10+
var VersionPrerelease = terraform.VersionPrerelease

0 commit comments

Comments
 (0)