diff --git a/README.md b/README.md index 6544c1d5e6ec4..ebea076eb16b2 100644 --- a/README.md +++ b/README.md @@ -414,8 +414,6 @@ Following attributes are required to authenticate against the [Github Actions Ca where `url` and `token` will be automatically set. To use this backend in a inline `run` step, you have to include [crazy-max/ghaction-github-runtime](https://github.com/crazy-max/ghaction-github-runtime) in your workflow to expose the runtime. -:warning: If used directly with `buildctl` CLI, you must inline `url` and `token` attributes. For instance : `type=gha,url=$ACTIONS_CACHE_URL,token=$ACTIONS_RUNTIME_TOKEN` - `--export-cache` options: * `type=gha` * `mode=min` (default): only export layers for the resulting image diff --git a/cmd/buildctl/build/exportcache.go b/cmd/buildctl/build/exportcache.go index cf83897b8f340..705227a73df1e 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 a300f327bbaee..4845b650b7046 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 8dd18bcf16ab1..ad175812d65eb 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 0000000000000..5a111683ff0d8 --- /dev/null +++ b/cmd/buildctl/build/util.go @@ -0,0 +1,32 @@ +package build + +import ( + "errors" + "os" + + "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 +}