#WithMountedCache cache system directory
1 messages · Page 1 of 1 (latest)
yep, you sure can
we do this with .yarn/cache in all our pipelines, super nice!
I found that after binding the cache volume with WithMountedCache, pnpm install does not show the reused package, can you give me some help about this part or how your code is implemented, can you give me some hints, thanks a lot!
sure, on mobile atm but could you maybe share a snippet of what you have already? It would clarify things.
here are a few snippets on how I do it 🧵
// function to explicitly load the files context
func getProject(client *dagger.Client, dir string) *dagger.Directory {
d := client.Host().Directory(dir, dagger.HostDirectoryOpts{
Include: []string{
".git",
".envrc",
".eslintrc.json",
".npmrc",
".prettierignore",
".prettierrc",
"babel.config.json",
"codecov.yml",
"eslint-local-rules.js",
"jest.config.js",
"jest.preset.js",
"jest.resolver.js",
"package.json",
"tsconfig.base.json",
"yarn.lock",
".nxignore",
".yarnrc.yml",
"nx.json",
"workspace.json",
".yarn/releases",
"infra/",
"scripts/",
"apps/",
"libs/",
"tools/",
},
Exclude: []string{
"scripts/ci/pipelines/",
},
})
return d
}
// dagger code is stored in a subdir of project root
func getParentDir() string {
wd, err := os.Getwd()
if err != nil {
panic(err)
}
return filepath.Dir(wd)
}
baseImage := "node:18.8.0-alpine3.15"
yarnCacheDir := "/tmp/yarn/cache" // setting the yarn cache dir in the container
project := getProject(client, baseDir)
path := "/build"
baseDir := getParentDir()
img := client.Container().From(baseImage)
base := img.Pipeline("install").
WithEnvVariable("CI", "true").
// This env does the magic. Instead of loading .yarn/cache, it will install everything once and subsequent runs will be cached
WithEnvVariable("YARN_CACHE_FOLDER", yarnCacheDir).
WithEntrypoint(nil).
WithWorkdir(path).
WithMountedDirectory("./", project).
WithMountedCache("./node_modules", client.CacheVolume("node_modules")).
// loading the named cache volume
WithMountedCache(yarnCacheDir, client.CacheVolume("yarn")).
WithMountedCache("./yarn/install-state.gz", client.CacheVolume("install-state")).
WithExec([]string{"/bin/sh", "-c", `
apk add --no-cache build-base python3 gcc jq git openjdk17-jre-headless && \
ln -sf /usr/bin/python3 /usr/bin/python && \
yarn install --immutable
`})
you could load the local .yarn/cache dir ofcourse, but since you might want to upload the cache to a remote storage you probably want the container to generate the .yarn/cache and not your machine.
Referring to your configuration, I configured pnpm and he worked successfully, thanks a lot!