#WithMountedCache cache system directory

1 messages · Page 1 of 1 (latest)

true sparrow
#

I'm a front-end developer and I'm using pnpm as a dependency management tool. Can I install /root/.local/share/pnpm/store/v3 as WithMountedCache and perform a dependency installation

blazing escarp
#

yep, you sure can

bronze owl
#

we do this with .yarn/cache in all our pipelines, super nice!

true sparrow
bronze owl
#

sure, on mobile atm but could you maybe share a snippet of what you have already? It would clarify things.

bronze owl
#

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
}
bronze owl
#
// 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.

true sparrow