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
Signed-off-by: Vasek - Tom C <tom.chauveau@epitech.eu>
  • Loading branch information
TomChv committed Mar 11, 2022
1 parent 4f63e7e commit 2f68ff4
Show file tree
Hide file tree
Showing 5 changed files with 68 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 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 2f68ff4

Please sign in to comment.