Skip to content

Commit 008cdea

Browse files
authored
[cache] Upload nix dependencies to cache (#2019)
## Summary ## How was it tested?
1 parent c703d13 commit 008cdea

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

.github/workflows/cache-upload.yml

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
name: cache-upload
2+
# Uploads devbox nix dependencies to cache
3+
4+
on:
5+
pull_request:
6+
push:
7+
branches:
8+
- main
9+
merge_group:
10+
branches:
11+
- main
12+
workflow_dispatch:
13+
schedule:
14+
- cron: '30 8 * * *' # Run nightly at 8:30 UTC
15+
16+
permissions:
17+
contents: read
18+
pull-requests: read
19+
20+
defaults:
21+
run:
22+
shell: bash
23+
24+
env:
25+
DEVBOX_API_TOKEN: ${{ secrets.DEVBOX_API_TOKEN }}
26+
DEVBOX_DEBUG: 1
27+
28+
jobs:
29+
upload-cache:
30+
strategy:
31+
matrix:
32+
os: [ubuntu-latest, macos-latest]
33+
runs-on: ${{ matrix.os }}
34+
timeout-minutes: 10
35+
steps:
36+
- uses: actions/checkout@v4
37+
38+
# Build devbox from scratch because released devbox has a bug that prevents
39+
# DEVBOX_API_TOKEN use
40+
# we can remove this after 0.10.6 is out.
41+
- uses: actions/setup-go@v5
42+
with:
43+
go-version-file: ./go.mod
44+
- name: Build devbox
45+
run: |
46+
go build -o dist/devbox ./cmd/devbox
47+
sudo mv ./dist/devbox /usr/local/bin/
48+
49+
# - name: Install devbox
50+
# uses: jetify-com/devbox-install-action@v0.9.0
51+
# with:
52+
# enable-cache: true
53+
54+
# We upload twice, once before updating and once after. This shows a simple
55+
# method to cache the latest current and latest dependencies.
56+
# If we want read access to cache on multi-user nix installs (e.g. macos),
57+
# we need to call devbox cache configure. This is currently not working
58+
# as expected on CICD.
59+
- name: Upload cache
60+
run: |
61+
devbox cache upload
62+
devbox update
63+
devbox cache upload

internal/devbox/cache.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ func (d *Devbox) UploadProjectToCache(
3232
if err != nil {
3333
return err
3434
}
35+
36+
// Ensure state is up to date before uploading to cache.
37+
// TODO: we may be able to do this more efficiently, not sure everything needs
38+
// to be installed.
39+
if err = d.ensureStateIsUpToDate(ctx, ensure); err != nil {
40+
return err
41+
}
42+
3543
return nix.CopyInstallableToCache(ctx, d.stderr, cacheURI, profilePath, creds.Env())
3644
}
3745

internal/devbox/providers/nixcache/nixcache.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import (
77
"time"
88

99
"go.jetpack.io/devbox/internal/build"
10+
"go.jetpack.io/devbox/internal/cachehash"
1011
"go.jetpack.io/devbox/internal/devbox/providers/identity"
1112
"go.jetpack.io/devbox/internal/redact"
1213
"go.jetpack.io/devbox/internal/setup"
1314
"go.jetpack.io/pkg/api"
1415
nixv1alpha1 "go.jetpack.io/pkg/api/gen/priv/nix/v1alpha1"
16+
"go.jetpack.io/pkg/auth/session"
1517
"go.jetpack.io/pkg/filecache"
1618
)
1719

@@ -80,7 +82,7 @@ func (p *Provider) Credentials(ctx context.Context) (AWSCredentials, error) {
8082
return AWSCredentials{}, err
8183
}
8284
creds, err := cache.GetOrSetWithTime(
83-
"credentials-"+token.IDClaims().Subject,
85+
"credentials-"+getSubOrAccessTokenHash(token),
8486
func() (AWSCredentials, time.Time, error) {
8587
token, err := identity.Get().GenSession(ctx)
8688
if err != nil {
@@ -116,7 +118,7 @@ func (p *Provider) URI(ctx context.Context) (string, error) {
116118
// Landau: I think we can probably remove this cache? This endpoint is very
117119
// fast and we only use this for build/upload which are slow.
118120
uri, err := cache.GetOrSet(
119-
"uri-"+token.IDClaims().Subject,
121+
"uri-"+getSubOrAccessTokenHash(token),
120122
func() (string, time.Duration, error) {
121123
client := api.NewClient(ctx, build.JetpackAPIHost(), token)
122124
resp, err := client.GetBinCache(ctx)
@@ -173,3 +175,12 @@ func (a AWSCredentials) Env() []string {
173175
"AWS_SESSION_TOKEN=" + a.SessionToken,
174176
}
175177
}
178+
179+
func getSubOrAccessTokenHash(token *session.Token) string {
180+
// We need this because the token is missing IDToken when used in CICD.
181+
// TODO: Implement AccessToken Parsing so we can extract sub form that.
182+
if token.IDClaims() != nil && token.IDClaims().Subject != "" {
183+
return token.IDClaims().Subject
184+
}
185+
return cachehash.Bytes([]byte(token.AccessToken))
186+
}

0 commit comments

Comments
 (0)