Skip to content

Commit 4ea0dd8

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 <[email protected]>
1 parent b40ae87 commit 4ea0dd8

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ jobs:
155155
run: docker run --rm --privileged tonistiigi/binfmt:latest --install all
156156
- name: Setup source policy
157157
uses: ./.github/actions/setup-source-policy
158+
- name: Expose GitHub tokens for caching
159+
uses: crazy-max/ghaction-github-runtime@3cb05d89e1f492524af3d41a1c98c83bc3025124 # v3.1.0
158160
- name: Run integration tests
159161
run: |
160162
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/main_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import (
1717
"go.opentelemetry.io/otel"
1818
"go.opentelemetry.io/otel/propagation"
1919
"go.opentelemetry.io/otel/sdk/trace"
20+
21+
_ "github.com/Azure/dalec/targets/plugin"
2022
)
2123

2224
var (

test/testenv/build.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ import (
44
"bufio"
55
"context"
66
"encoding/json"
7+
"fmt"
78
"io"
89
"os"
10+
"path"
911
"path/filepath"
12+
"runtime"
13+
"sync"
1014
"testing"
1115

16+
"github.com/Azure/dalec/internal/plugins"
1217
"github.com/moby/buildkit/client"
1318
"github.com/moby/buildkit/client/llb"
1419
"github.com/moby/buildkit/exporter/containerimage/exptypes"
@@ -218,3 +223,59 @@ func lookupProjectRoot(cur string) (string, error) {
218223

219224
return cur, nil
220225
}
226+
227+
var ciLoadCacheOptions = sync.OnceValue(func() (out []client.CacheOptionsEntry) {
228+
if os.Getenv("GITHUB_ACTIONS") != "true" {
229+
// not running in a github action, nothing to do
230+
return
231+
}
232+
233+
// token and url are required for the cache to work.
234+
// These need to be exposed as environment variables in the GitHub Actions workflow.
235+
// See the crazy-max/ghaction-github-runtime@v3 action.
236+
token := os.Getenv("ACTIONS_RUNTIME_TOKEN")
237+
if token == "" {
238+
fmt.Fprintln(os.Stderr, "::warning::GITHUB_ACTIONS_RUNTIME_TOKEN is not set, skipping cache export")
239+
return nil
240+
}
241+
242+
url := os.Getenv("ACTIONS_CACHE_URL")
243+
if url == "" {
244+
fmt.Fprintln(os.Stderr, "::warning::ACTIONS_CACHE_URL is not set, skipping cache export")
245+
return nil
246+
}
247+
248+
// Unfortunately we need to load all the caches because at this level we do
249+
// not know what the build target will be since that will be done at the
250+
// gateway client level, where we can't set cache imports.
251+
filter := func(r *plugins.Registration) bool {
252+
return r.Type != plugins.TypeBuildTarget
253+
}
254+
255+
for _, r := range plugins.Graph(filter) {
256+
target := path.Join(r.ID, "worker")
257+
out = append(out, client.CacheOptionsEntry{
258+
Type: "gha",
259+
Attrs: map[string]string{
260+
"scope": "main." + target,
261+
"token": token,
262+
"url": url,
263+
},
264+
})
265+
}
266+
267+
fmt.Fprintln(os.Stderr, "::add-mask::"+token)
268+
_, f, l, _ := runtime.Caller(1)
269+
if len(out) == 0 {
270+
_, f, l, _ := runtime.Caller(1)
271+
fmt.Fprintf(os.Stderr, "::error file=%s,line=%d::No build targets found, skipping cache export\n", f, l)
272+
}
273+
for _, o := range out {
274+
fmt.Fprintf(os.Stderr, "::notice file=%s,line=%d::Adding cache import: %s %v\n", f, l, o.Type, o.Attrs)
275+
}
276+
return out
277+
})
278+
279+
func withCICache(opts *client.SolveOpt) {
280+
opts.CacheImports = append(opts.CacheImports, ciLoadCacheOptions()...)
281+
}

test/testenv/buildx.go

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

381+
withCICache(&so)
382+
381383
_, err = c.Build(ctx, so, "", func(ctx context.Context, gwc gwclient.Client) (*gwclient.Result, error) {
382384
gwc = &clientForceDalecWithInput{gwc}
383385

0 commit comments

Comments
 (0)