From 8d823f88d079e4421efe8feb9a0d4a8268c289ac Mon Sep 17 00:00:00 2001 From: Vasek - Tom C Date: Fri, 4 Mar 2022 18:14:47 +0100 Subject: [PATCH] Improve `buildctl` import and export cache to use GitHub env variables Signed-off-by: Vasek - Tom C --- cmd/buildctl/build/exportcache.go | 3 +++ cmd/buildctl/build/importcache.go | 3 +++ cmd/buildctl/build/importcache_test.go | 29 ++++++++++++++++++++++ cmd/buildctl/build/util.go | 33 ++++++++++++++++++++++++++ 4 files changed, 68 insertions(+) create mode 100644 cmd/buildctl/build/util.go diff --git a/cmd/buildctl/build/exportcache.go b/cmd/buildctl/build/exportcache.go index cf83897b8f34..705227a73df1 100644 --- a/cmd/buildctl/build/exportcache.go +++ b/cmd/buildctl/build/exportcache.go @@ -39,6 +39,9 @@ func parseExportCacheCSV(s string) (client.CacheOptionsEntry, error) { if _, ok := ex.Attrs["mode"]; !ok { ex.Attrs["mode"] = "min" } + if ex.Type == "gha" { + return loadGithubEnv(ex) + } return ex, nil } diff --git a/cmd/buildctl/build/importcache.go b/cmd/buildctl/build/importcache.go index a300f327bbae..4845b650b704 100644 --- a/cmd/buildctl/build/importcache.go +++ b/cmd/buildctl/build/importcache.go @@ -36,6 +36,9 @@ func parseImportCacheCSV(s string) (client.CacheOptionsEntry, error) { if im.Type == "" { return im, errors.New("--import-cache requires type=") } + if im.Type == "gha" { + return loadGithubEnv(im) + } return im, nil } diff --git a/cmd/buildctl/build/importcache_test.go b/cmd/buildctl/build/importcache_test.go index 8dd18bcf16ab..ad175812d65e 100644 --- a/cmd/buildctl/build/importcache_test.go +++ b/cmd/buildctl/build/importcache_test.go @@ -48,7 +48,36 @@ func TestParseImportCache(t *testing.T) { }, }, }, + { + importCaches: []string{"type=gha,url=https://foo.bar,token=foo"}, + expected: []client.CacheOptionsEntry{ + { + Type: "gha", + Attrs: map[string]string{ + "url": "https://foo.bar", + "token": "foo", + }, + }, + }, + }, + { + importCaches: []string{"type=gha"}, + expected: []client.CacheOptionsEntry{ + { + Type: "gha", + Attrs: map[string]string{ + "url": "https://github.com/test", // Set from env below + "token": "bar", // Set from env below + }, + }, + }, + }, } + + // Set values for GitHub parse cache + t.Setenv("ACTIONS_CACHE_URL", "https://github.com/test") + t.Setenv("ACTIONS_RUNTIME_TOKEN", "bar") + for _, tc := range testCases { im, err := ParseImportCache(tc.importCaches) if tc.expectedErr == "" { diff --git a/cmd/buildctl/build/util.go b/cmd/buildctl/build/util.go new file mode 100644 index 000000000000..4dfe63289791 --- /dev/null +++ b/cmd/buildctl/build/util.go @@ -0,0 +1,33 @@ +package build + +import ( + "os" + + "github.com/pkg/errors" + + "github.com/moby/buildkit/client" +) + +// loadGithubEnv verify that url and token attributes exists in the +// cache. +// If not, it will search for $ACTIONS_RUNTIME_TOKEN and $ACTIONS_CACHE_URL +// environments variables and add it to cache Options +// Since it works for both import and export +func loadGithubEnv(cache client.CacheOptionsEntry) (client.CacheOptionsEntry, error) { + if _, ok := cache.Attrs["url"]; !ok { + url, ok := os.LookupEnv("ACTIONS_CACHE_URL") + if !ok { + return cache, errors.New("cache with type gha requires url parameter or $ACTIONS_CACHE_URL") + } + cache.Attrs["url"] = url + } + + if _, ok := cache.Attrs["token"]; !ok { + token, ok := os.LookupEnv("ACTIONS_RUNTIME_TOKEN") + if !ok { + return cache, errors.New("cache with type gha requires token parameter or $ACTIONS_RUNTIME_TOKEN") + } + cache.Attrs["token"] = token + } + return cache, nil +}