From 3fef64f584141a2cff056c4d9204a47a64a5af4e Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Sat, 6 Feb 2021 00:17:50 -0800 Subject: [PATCH] allow exporting to github cache backend Signed-off-by: Tonis Tiigi --- build/cache.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/build/cache.go b/build/cache.go index 6d9b7f04..97a64b95 100644 --- a/build/cache.go +++ b/build/cache.go @@ -2,6 +2,7 @@ package build import ( "encoding/csv" + "os" "strings" "github.com/moby/buildkit/client" @@ -45,6 +46,9 @@ func ParseCacheEntry(in []string) ([]client.CacheOptionsEntry, error) { if im.Type == "" { return nil, errors.Errorf("type required form> %q", in) } + if !addGithubToken(&im) { + continue + } imports = append(imports, im) } return imports, nil @@ -58,3 +62,20 @@ func isRefOnlyFormat(in []string) bool { } return true } + +func addGithubToken(ci *client.CacheOptionsEntry) bool { + if ci.Type != "gha" { + return true + } + if _, ok := ci.Attrs["token"]; !ok { + if v, ok := os.LookupEnv("ACTIONS_RUNTIME_TOKEN"); ok { + ci.Attrs["token"] = v + } + } + if _, ok := ci.Attrs["url"]; !ok { + if v, ok := os.LookupEnv("ACTIONS_CACHE_URL"); ok { + ci.Attrs["url"] = v + } + } + return ci.Attrs["token"] != "" && ci.Attrs["url"] != "" +}