Skip to content

Commit

Permalink
Merge pull request #2707 from TomChv/doc/explicit-github-cache-attrib…
Browse files Browse the repository at this point in the history
…utes

Load GitHub runtime environment when using buildctl
  • Loading branch information
tonistiigi authored Mar 15, 2022
2 parents a45b769 + 8d823f8 commit 1888aab
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cmd/buildctl/build/exportcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
3 changes: 3 additions & 0 deletions cmd/buildctl/build/importcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ func parseImportCacheCSV(s string) (client.CacheOptionsEntry, error) {
if im.Type == "" {
return im, errors.New("--import-cache requires type=<type>")
}
if im.Type == "gha" {
return loadGithubEnv(im)
}
return im, nil
}

Expand Down
29 changes: 29 additions & 0 deletions cmd/buildctl/build/importcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down
33 changes: 33 additions & 0 deletions cmd/buildctl/build/util.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 1888aab

Please sign in to comment.