Handle case of missing GH variables (#492)

Closes #482 

I traced the problem to where buildx parses the cli args
([here](fa4461b9a1/util/buildflags/cache.go (L14))),
and confirmed it applies defaults based on GHA environment variables and
ignores the cacheTo/cacheFrom directive altogether when the variables
aren't available.

The fix is to ignore the GHA cache directive when it the upstream parser
ignores it.
This commit is contained in:
Eron Wright
2025-03-20 09:27:59 -07:00
committed by GitHub
parent f41c8c927d
commit 20f5f536dc
4 changed files with 100 additions and 12 deletions

View File

@@ -25,6 +25,7 @@ import (
_ "github.com/docker/buildx/driver/docker-container"
"github.com/distribution/reference"
pb "github.com/docker/buildx/controller/pb"
"github.com/moby/buildkit/client"
"github.com/moby/buildkit/exporter/containerimage/exptypes"
"github.com/regclient/regclient/types/descriptor"
@@ -818,7 +819,6 @@ func TestImageDiff(t *testing.T) {
}
func TestValidateImageArgs(t *testing.T) {
t.Parallel()
t.Run("invalid inputs", func(t *testing.T) {
t.Parallel()
args := ImageArgs{
@@ -917,6 +917,85 @@ func TestValidateImageArgs(t *testing.T) {
assert.Len(t, opts.Exports, 0)
})
t.Run("environment variables", func(t *testing.T) {
tests := []struct {
name string
envs map[string]string
args ImageArgs
wantCacheFrom *pb.CacheOptionsEntry
wantCacheTo *pb.CacheOptionsEntry
}{
{
name: "gha environment",
envs: map[string]string{
"ACTIONS_CACHE_URL": "test-cache-url",
"ACTIONS_RUNTIME_TOKEN": "test-runtime-token",
},
args: ImageArgs{
Context: &BuildContext{Context: Context{Location: "testdata/noop"}},
CacheFrom: []CacheFrom{{GHA: &CacheFromGitHubActions{}}},
CacheTo: []CacheTo{{GHA: &CacheToGitHubActions{
CacheFromGitHubActions: CacheFromGitHubActions{},
}}},
},
wantCacheFrom: &pb.CacheOptionsEntry{
Type: "gha",
Attrs: map[string]string{
"token": "test-runtime-token",
"url": "test-cache-url",
},
},
wantCacheTo: &pb.CacheOptionsEntry{
Type: "gha",
Attrs: map[string]string{
"token": "test-runtime-token",
"url": "test-cache-url",
},
},
},
{
name: "non-gha environment",
envs: map[string]string{
"ACTIONS_CACHE_URL": "",
"ACTIONS_RUNTIME_TOKEN": "",
},
args: ImageArgs{
Context: &BuildContext{Context: Context{Location: "testdata/noop"}},
CacheFrom: []CacheFrom{{GHA: &CacheFromGitHubActions{}}},
CacheTo: []CacheTo{{GHA: &CacheToGitHubActions{
CacheFromGitHubActions: CacheFromGitHubActions{},
}}},
},
wantCacheFrom: nil,
wantCacheTo: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
for k, v := range tt.envs {
t.Setenv(k, v)
}
validate := func(preview bool) {
opts, err := tt.args.validate(true, preview)
require.NoError(t, err)
if tt.wantCacheFrom != nil {
assert.Equal(t, tt.wantCacheFrom, opts.CacheFrom[0])
} else {
assert.Len(t, opts.CacheFrom, 0)
}
if tt.wantCacheTo != nil {
assert.Equal(t, tt.wantCacheTo, opts.CacheTo[0])
} else {
assert.Len(t, opts.CacheTo, 0)
}
}
validate(true)
validate(false)
})
}
})
t.Run("multiple exports pre-0.13", func(t *testing.T) {
t.Parallel()
args := ImageArgs{