Skip to content

Commit 6ef2ccf

Browse files
authored
fix version embedding for provider version (#593)
Setting version using ldflags is brittle, and seems not to work the way goreleaser does it today. Instead, let's just get our version from `BuildInfo` which is more reliable. Signed-off-by: Jason Hall <[email protected]>
1 parent 259aef3 commit 6ef2ccf

File tree

6 files changed

+21
-12
lines changed

6 files changed

+21
-12
lines changed

.goreleaser.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ builds:
1212
mod_timestamp: '{{ .CommitTimestamp }}'
1313
flags:
1414
- -trimpath
15-
ldflags:
16-
- '-s -w -X main.version={{.Version}} -X main.commit={{.Commit}}'
1715
goos:
1816
- linux
1917
- darwin

internal/provider/layering_validation_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestLayeringStrategyValidation(t *testing.T) {
1414
resource.Test(t, resource.TestCase{
1515
PreCheck: func() { testAccPreCheck(t) },
1616
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
17-
"apko": providerserver.NewProtocol6WithError(New("test")()),
17+
"apko": providerserver.NewProtocol6WithError(New()()),
1818
},
1919
Steps: []resource.TestStep{
2020
{
@@ -48,7 +48,7 @@ func TestLayeringBudgetValidation(t *testing.T) {
4848
resource.Test(t, resource.TestCase{
4949
PreCheck: func() { testAccPreCheck(t) },
5050
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
51-
"apko": providerserver.NewProtocol6WithError(New("test")()),
51+
"apko": providerserver.NewProtocol6WithError(New()()),
5252
},
5353
Steps: []resource.TestStep{
5454
{

internal/provider/provider.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package provider
22

33
import (
44
"context"
5+
"runtime/debug"
56
"time"
67

78
"chainguard.dev/apko/pkg/apk/apk"
@@ -205,10 +206,21 @@ func (p *Provider) Functions(ctx context.Context) []func() function.Function {
205206
}
206207
}
207208

208-
func New(version string) func() provider.Provider {
209+
// getVersion attempts to get the version from build info.
210+
func getVersion() string {
211+
if info, ok := debug.ReadBuildInfo(); ok {
212+
// When built with goreleaser, this will be the actual version
213+
if info.Main.Version != "" && info.Main.Version != "(devel)" {
214+
return info.Main.Version
215+
}
216+
}
217+
return "dev"
218+
}
219+
220+
func New() func() provider.Provider {
209221
return func() provider.Provider {
210222
return &Provider{
211-
version: version,
223+
version: getVersion(),
212224
}
213225
}
214226
}

internal/provider/provider_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
// CLI command executed to create a provider server to which the CLI can
1313
// reattach.
1414
var testAccProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServer, error){
15-
"apko": providerserver.NewProtocol6WithError(New("test")()),
15+
"apko": providerserver.NewProtocol6WithError(New()()),
1616
}
1717

1818
func testAccPreCheck(t *testing.T) {

internal/provider/version_function_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ output "apko_version" {
2525
value = local.version_info.apko_version
2626
}`,
2727
Check: resource.ComposeTestCheckFunc(
28-
// Check that the provider version is "test" (as set in testAccProtoV6ProviderFactories)
29-
resource.TestCheckOutput("provider_version", "test"),
28+
// Check that the provider version is not empty and looks like a version
29+
// With build info, it should be something like "v0.23.1-0.20250729145754-7f4b7167d62c" or "dev"
30+
resource.TestMatchOutput("provider_version", regexp.MustCompile(`^(v\d+\.\d+\.\d+.*|dev)$`)),
3031
// Check that apko version is not empty and looks like a version
3132
resource.TestMatchOutput("apko_version", regexp.MustCompile(`^v\d+\.\d+\.\d+.*|unknown$`)),
3233
),

main.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import (
1414
//go:generate terraform fmt -recursive ./examples/
1515
//go:generate go run github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs
1616

17-
const version string = "dev"
18-
1917
func main() {
2018
var debug bool
2119
flag.BoolVar(&debug, "debug", false, "set to true to run the provider with support for debuggers like delve")
@@ -26,7 +24,7 @@ func main() {
2624
Debug: debug,
2725
}
2826

29-
if err := providerserver.Serve(context.Background(), provider.New(version), opts); err != nil {
27+
if err := providerserver.Serve(context.Background(), provider.New(), opts); err != nil {
3028
log.Fatal(err.Error())
3129
}
3230
}

0 commit comments

Comments
 (0)