Skip to content

Commit

Permalink
Improve buildctl import and export cache to use GitHub env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
TomChv committed Mar 8, 2022
1 parent d622a11 commit f29ac5b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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 bindGithubCacheAttributes(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 bindGithubCacheAttributes(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

0 comments on commit f29ac5b

Please sign in to comment.