Skip to content

Commit 4712052

Browse files
authored
Merge pull request #5754 from crazy-max/update-gha-cache-test
buildctl: set fallback url for gha cache
2 parents 13c7eaa + 1c917b4 commit 4712052

File tree

5 files changed

+76
-32
lines changed

5 files changed

+76
-32
lines changed

README.md

Lines changed: 2 additions & 1 deletion
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)

cache/remotecache/gha/gha_test.go

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package gha
22

33
import (
4+
"maps"
45
"os"
56
"path/filepath"
7+
"strconv"
68
"strings"
79
"testing"
810
"time"
@@ -57,10 +59,25 @@ func testBasicGhaCacheImportExportExtraTimeout(t *testing.T, sb integration.Sand
5759

5860
destDir := t.TempDir()
5961

60-
runtimeToken := os.Getenv("ACTIONS_RUNTIME_TOKEN")
61-
cacheURL := os.Getenv("ACTIONS_CACHE_URL")
62-
if runtimeToken == "" || cacheURL == "" {
63-
t.Skip("ACTIONS_RUNTIME_TOKEN and ACTIONS_CACHE_URL must be set")
62+
var cacheVersion string
63+
if v, ok := os.LookupEnv("ACTIONS_CACHE_SERVICE_V2"); ok {
64+
if b, err := strconv.ParseBool(v); err == nil && b {
65+
cacheVersion = "2"
66+
}
67+
}
68+
69+
cacheAttrs := map[string]string{}
70+
if cacheVersion == "2" {
71+
cacheAttrs["url_v2"] = os.Getenv("ACTIONS_RESULTS_URL")
72+
}
73+
cacheAttrs["url"] = os.Getenv("ACTIONS_CACHE_URL")
74+
if cacheAttrs["url"] == "" {
75+
cacheAttrs["url"] = os.Getenv("ACTIONS_RESULTS_URL")
76+
}
77+
cacheAttrs["token"] = os.Getenv("ACTIONS_RUNTIME_TOKEN")
78+
79+
if cacheAttrs["token"] == "" || (cacheAttrs["url"] == "" && cacheAttrs["url_v2"] == "") {
80+
t.Skip("actions runtime token and cache url must be set")
6481
}
6582

6683
scope := "buildkit-" + t.Name()
@@ -74,6 +91,12 @@ func testBasicGhaCacheImportExportExtraTimeout(t *testing.T, sb integration.Sand
7491
}
7592
}
7693

94+
cacheExportAttrs := map[string]string{
95+
"scope": scope,
96+
"mode": "max",
97+
}
98+
maps.Copy(cacheExportAttrs, cacheAttrs)
99+
77100
_, err = c.Solve(sb.Context(), def, client.SolveOpt{
78101
Exports: []client.ExportEntry{
79102
{
@@ -82,13 +105,8 @@ func testBasicGhaCacheImportExportExtraTimeout(t *testing.T, sb integration.Sand
82105
},
83106
},
84107
CacheExports: []client.CacheOptionsEntry{{
85-
Type: "gha",
86-
Attrs: map[string]string{
87-
"url": cacheURL,
88-
"token": runtimeToken,
89-
"scope": scope,
90-
"mode": "max",
91-
},
108+
Type: "gha",
109+
Attrs: cacheExportAttrs,
92110
}},
93111
}, nil)
94112
require.NoError(t, err)
@@ -104,6 +122,11 @@ func testBasicGhaCacheImportExportExtraTimeout(t *testing.T, sb integration.Sand
104122

105123
destDir = t.TempDir()
106124

125+
cacheImportAttrs := map[string]string{
126+
"scope": scope,
127+
}
128+
maps.Copy(cacheImportAttrs, cacheAttrs)
129+
107130
_, err = c.Solve(sb.Context(), def, client.SolveOpt{
108131
Exports: []client.ExportEntry{
109132
{
@@ -112,12 +135,8 @@ func testBasicGhaCacheImportExportExtraTimeout(t *testing.T, sb integration.Sand
112135
},
113136
},
114137
CacheImports: []client.CacheOptionsEntry{{
115-
Type: "gha",
116-
Attrs: map[string]string{
117-
"url": cacheURL,
118-
"token": runtimeToken,
119-
"scope": scope,
120-
},
138+
Type: "gha",
139+
Attrs: cacheImportAttrs,
121140
}},
122141
}, nil)
123142
require.NoError(t, err)

cmd/buildctl/build/importcache_test.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,22 @@ func TestParseImportCache(t *testing.T) {
5454
{
5555
Type: "gha",
5656
Attrs: map[string]string{
57-
"url": "https://foo.bar",
58-
"token": "foo",
57+
"url": "https://foo.bar",
58+
"url_v2": "https://github.com/testv2", // Set from env below
59+
"token": "foo",
60+
},
61+
},
62+
},
63+
},
64+
{
65+
importCaches: []string{"type=gha,url_v2=https://foo.bar,token=foo"},
66+
expected: []client.CacheOptionsEntry{
67+
{
68+
Type: "gha",
69+
Attrs: map[string]string{
70+
"url": "https://github.com/test", // Set from env below
71+
"url_v2": "https://foo.bar",
72+
"token": "foo",
5973
},
6074
},
6175
},
@@ -66,16 +80,19 @@ func TestParseImportCache(t *testing.T) {
6680
{
6781
Type: "gha",
6882
Attrs: map[string]string{
69-
"url": "https://github.com/test", // Set from env below
70-
"token": "bar", // Set from env below
83+
"url": "https://github.com/test", // Set from env below
84+
"url_v2": "https://github.com/testv2", // Set from env below
85+
"token": "bar", // Set from env below
7186
},
7287
},
7388
},
7489
},
7590
}
7691

7792
// Set values for GitHub parse cache
93+
t.Setenv("ACTIONS_CACHE_SERVICE_V2", "True")
7894
t.Setenv("ACTIONS_CACHE_URL", "https://github.com/test")
95+
t.Setenv("ACTIONS_RESULTS_URL", "https://github.com/testv2")
7996
t.Setenv("ACTIONS_RUNTIME_TOKEN", "bar")
8097

8198
for _, tc := range testCases {

cmd/buildctl/build/util.go

Lines changed: 15 additions & 10 deletions
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 {

hack/test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ baseCreateFlags="--rm --privileged $dockerConfigMount \
125125
-e CGO_ENABLED \
126126
-e GITHUB_REF \
127127
-e ACTIONS_RUNTIME_TOKEN \
128+
-e ACTIONS_CACHE_SERVICE_V2 \
128129
-e ACTIONS_CACHE_URL \
130+
-e ACTIONS_RESULTS_URL \
129131
-e TEST_DOCKERD \
130132
-e BUILDKIT_TEST_ENABLE_FEATURES \
131133
-e BUILDKIT_TEST_DISABLE_FEATURES \

0 commit comments

Comments
 (0)