Skip to content

Commit 2e2bd9a

Browse files
committed
buildctl: set fallback url for gha cache
Signed-off-by: CrazyMax <[email protected]>
1 parent dae48b3 commit 2e2bd9a

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,8 @@ GitHub Actions cache saves both cache metadata and layers to GitHub's Cache serv
516516
Similarly to using [actions/cache](https://github.com/actions/cache), caches are [scoped by branch](https://docs.github.com/en/actions/advanced-guides/caching-dependencies-to-speed-up-workflows#restrictions-for-accessing-a-cache), with the default and target branches being available to every branch.
517517

518518
Following attributes are required to authenticate against the [GitHub Actions Cache service API](https://github.com/tonistiigi/go-actions-cache/blob/master/api.md#authentication):
519-
* `url`: Cache server URL (default `$ACTIONS_CACHE_URL`)
519+
* `url`: Cache server URL (default `$ACTIONS_CACHE_URL` or fallback to `$ACTIONS_RESULTS_URL`)
520+
* `url_v2`: Cache v2 server URL if `$ACTIONS_CACHE_SERVICE_V2` set on the runner (default `$ACTIONS_RESULTS_URL`)
520521
* `token`: Access token (default `$ACTIONS_RUNTIME_TOKEN`)
521522

522523
:information_source: This type of cache can be used with [Docker Build Push Action](https://github.com/docker/build-push-action)

cmd/buildctl/build/util.go

+15-10
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ import (
44
"os"
55
"strconv"
66

7-
"github.com/pkg/errors"
8-
97
"github.com/moby/buildkit/client"
108
"github.com/moby/buildkit/util/bklog"
9+
"github.com/pkg/errors"
1110
)
1211

1312
// loadGithubEnv verify that url and token attributes exists in the
@@ -18,6 +17,7 @@ import (
1817
func loadGithubEnv(cache client.CacheOptionsEntry) (client.CacheOptionsEntry, error) {
1918
version, ok := cache.Attrs["version"]
2019
if !ok {
20+
// https://github.com/actions/toolkit/blob/2b08dc18f261b9fdd978b70279b85cbef81af8bc/packages/cache/src/internal/config.ts#L19
2121
if v, ok := os.LookupEnv("ACTIONS_CACHE_SERVICE_V2"); ok {
2222
if b, err := strconv.ParseBool(v); err == nil && b {
2323
version = "2"
@@ -26,18 +26,23 @@ func loadGithubEnv(cache client.CacheOptionsEntry) (client.CacheOptionsEntry, er
2626
}
2727

2828
if _, ok := cache.Attrs["url_v2"]; !ok && version == "2" {
29-
url, ok := os.LookupEnv("ACTIONS_RESULTS_URL")
30-
if !ok {
31-
return cache, errors.New("cache with type gha requires url parameter or $ACTIONS_RESULTS_URL")
29+
// https://github.com/actions/toolkit/blob/2b08dc18f261b9fdd978b70279b85cbef81af8bc/packages/cache/src/internal/config.ts#L34-L35
30+
if v, ok := os.LookupEnv("ACTIONS_RESULTS_URL"); ok {
31+
cache.Attrs["url_v2"] = v
3232
}
33-
cache.Attrs["url_v2"] = url
3433
}
3534
if _, ok := cache.Attrs["url"]; !ok {
36-
url, ok := os.LookupEnv("ACTIONS_CACHE_URL")
37-
if !ok {
38-
return cache, errors.New("cache with type gha requires url parameter or $ACTIONS_CACHE_URL")
35+
// https://github.com/actions/toolkit/blob/2b08dc18f261b9fdd978b70279b85cbef81af8bc/packages/cache/src/internal/config.ts#L28-L33
36+
if v, ok := os.LookupEnv("ACTIONS_CACHE_URL"); ok {
37+
cache.Attrs["url"] = v
38+
} else if v, ok := os.LookupEnv("ACTIONS_RESULTS_URL"); ok {
39+
cache.Attrs["url"] = v
40+
}
41+
}
42+
if _, ok := cache.Attrs["url"]; !ok {
43+
if _, ok := cache.Attrs["url_v2"]; !ok {
44+
return cache, errors.New("cache with type gha requires url parameter to be set")
3945
}
40-
cache.Attrs["url"] = url
4146
}
4247

4348
if _, ok := cache.Attrs["token"]; !ok {

0 commit comments

Comments
 (0)