Skip to content

Commit d2cdbdd

Browse files
committed
ci: Uses worker cache from main GHA cache
be79188 added in new GHA caches for worker images. The hope here is to be able to use these to cut down on a bunch of CI time. Signed-off-by: Brian Goff <cpuguy83@gmail.com>
1 parent 40028b7 commit d2cdbdd

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ jobs:
153153
run: docker run --rm --privileged tonistiigi/binfmt:latest --install all
154154
- name: Setup source policy
155155
uses: ./.github/actions/setup-source-policy
156+
- name: Expose GitHub tokens for caching
157+
uses: crazy-max/ghaction-github-runtime@3cb05d89e1f492524af3d41a1c98c83bc3025124 # v3.1.0
156158
- name: Run integration tests
157159
run: |
158160
set -ex

docker-bake.hcl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ target "runc-azlinux" {
7878
tags = tgt == "container" ? ["runc:mariner2"] : []
7979
// only output non-container targets to the fs
8080
output = tgt != "container" ? ["_output"] : []
81+
cache-from = [
82+
{
83+
type = "gha"
84+
scope = "main.${distro}/worker"
85+
}
86+
]
8187
}
8288

8389
target "runc-jammy" {
@@ -101,6 +107,12 @@ target "runc-jammy" {
101107
tags = tgt == "container" ? ["runc:jammy"] : []
102108
// only output non-container targets to the fs
103109
output = tgt != "container" ? ["_output"] : []
110+
cache-from = [
111+
{
112+
type = "gha"
113+
scope = "main.jammy/worker"
114+
}
115+
]
104116
}
105117

106118
target "runc-test" {
@@ -176,6 +188,12 @@ dependencies:
176188
}
177189
target = "${distro}/container/depsonly"
178190
tags = ["local/dalec/deps-only:${distro}"]
191+
cache-from = [
192+
{
193+
type = "gha"
194+
scope = "main.${distro}/worker"
195+
}
196+
]
179197
}
180198

181199
target "test-deps-only" {

test/testenv/build.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ import (
44
"bufio"
55
"context"
66
"encoding/json"
7+
"fmt"
78
"io"
89
"os"
10+
"path"
911
"path/filepath"
12+
"strings"
1013
"testing"
1114

1215
"github.com/moby/buildkit/client"
@@ -209,3 +212,39 @@ func lookupProjectRoot(cur string) (string, error) {
209212

210213
return cur, nil
211214
}
215+
216+
func withCICache(opts *client.SolveOpt) {
217+
if os.Getenv("GITHUB_ACTIONS") != "true" {
218+
// not running in a github action, nothing to do
219+
return
220+
}
221+
222+
// token and url are required for the cache to work.
223+
// These need to be exposed as environment variables in the GitHub Actions workflow.
224+
// See the crazy-max/ghaction-github-runtime@v3 action.
225+
token := os.Getenv("ACTIONS_RUNTIME_TOKEN")
226+
if token == "" {
227+
fmt.Fprintln(os.Stderr, "::warning::GITHUB_ACTIONS_RUNTIME_TOKEN is not set, skipping cache export")
228+
return
229+
}
230+
231+
url := os.Getenv("ACTIONS_CACHE_URL")
232+
if url == "" {
233+
fmt.Fprintln(os.Stderr, "::warning::ACTIONS_CACHE_URL is not set, skipping cache export")
234+
return
235+
}
236+
237+
target, _, ok := strings.Cut(opts.FrontendAttrs["target"], "/")
238+
if !ok && target == "" {
239+
return
240+
}
241+
242+
opts.CacheImports = append(opts.CacheImports, client.CacheOptionsEntry{
243+
Type: "gha",
244+
Attrs: map[string]string{
245+
"scope": "main." + path.Join(target, "worker"),
246+
"token": token,
247+
"url": url,
248+
},
249+
})
250+
}

test/testenv/buildx.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,8 @@ func (b *BuildxEnv) RunTest(ctx context.Context, t *testing.T, f TestFunc, opts
357357
f(&so)
358358
}
359359

360+
withCICache(&so)
361+
360362
_, err = c.Build(ctx, so, "", func(ctx context.Context, gwc gwclient.Client) (*gwclient.Result, error) {
361363
gwc = &clientForceDalecWithInput{gwc}
362364

0 commit comments

Comments
 (0)