Skip to content
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

Add support for SOURCE_DATE_EPOCH #93

Merged
merged 1 commit into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions internal/provider/resource_ko_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ import (
"fmt"
"io"
"log"
"os"
"strconv"
"sync"
"time"

"github.com/awslabs/amazon-ecr-credential-helper/ecr-login"
"github.com/chrismellard/docker-credential-acr-env/pkg/credhelper"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/authn/github"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/google"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/ko/pkg/build"
Expand Down Expand Up @@ -178,6 +182,16 @@ func (o *buildOptions) makeBuilder(ctx context.Context) (*build.Caching, error)
return nil, fmt.Errorf("unknown sbom type: %q", o.sbom)
}

// We read the environment variable directly here instead of plumbing it through as a provider option to keep the behavior consistent with resolve.
// While CreationTime is a build.Option, it is not a field in options.BuildOptions and is inferred from the environment variable when a new resolver is created.
if epoch := os.Getenv("SOURCE_DATE_EPOCH"); epoch != "" {
s, err := strconv.ParseInt(epoch, 10, 64)
if err != nil {
return nil, fmt.Errorf("the environment variable %s should be the number of seconds since January 1st 1970, 00:00 UTC, got: %w", epoch, err)
}
bo = append(bo, build.WithCreationTime(v1.Time{Time: time.Unix(s, 0)}))
}

b, err := build.NewGo(ctx, o.workingDir, bo...)
if err != nil {
return nil, fmt.Errorf("NewGo: %w", err)
Expand Down
27 changes: 27 additions & 0 deletions internal/provider/resource_ko_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,33 @@ func TestAccResourceKoBuild(t *testing.T) {
}},
})
}

t.Run("SOURCE_DATE_EPOCH", func(t *testing.T) {
t.Setenv("SOURCE_DATE_EPOCH", "1234567890")
resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
Steps: []resource.TestStep{{
Config: `resource "ko_build" "foo" {
importpath = "github.com/ko-build/terraform-provider-ko/cmd/test"
}`,
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr("ko_build.foo", "image_ref", imageRefRE),
),
}},
})
})
t.Run("SOURCE_DATE_EPOCH_failure", func(t *testing.T) {
t.Setenv("SOURCE_DATE_EPOCH", "abc123")
resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
Steps: []resource.TestStep{{
Config: `resource "ko_build" "foo" {
importpath = "github.com/ko-build/terraform-provider-ko/cmd/test"
}`,
ExpectError: regexp.MustCompile("should be the number of seconds since"),
}},
})
})
}

func TestAccResourceKoBuild_ImageRepo(t *testing.T) {
Expand Down