Extract yarn install into a separate step

If multiple yarn.#Run commands run in parallel, they will corrupt each
other's yarn cache mount. Because we extract yarn install into a
separate step, LLB will dedup the yarn install step and only run it
once, regardless how many yarn.#Run commands run in parallel.

Fixes https://github.com/dagger/dagger/issues/1670

Signed-off-by: Gerhard Lazu <gerhard@lazu.co.uk>
This commit is contained in:
Gerhard Lazu 2022-03-01 19:17:13 +00:00
parent d8af79166e
commit 089d464802
No known key found for this signature in database
GPG Key ID: A28DE70C9444D7A6

View File

@ -16,7 +16,7 @@ import (
script: *"build" | string script: *"build" | string
} }
// Build a Yarn package // Run a Yarn command
#Run: { #Run: {
// Custom name for the build. // Custom name for the build.
// When building different apps in the same plan, assign // When building different apps in the same plan, assign
@ -50,29 +50,52 @@ import (
// FIXME: not implemented. Are they needed? // FIXME: not implemented. Are they needed?
secrets: [string]: dagger.#Secret secrets: [string]: dagger.#Secret
_build: docker.#Build & {
steps: [
// FIXME: Yarn's version depends on Alpine's version // FIXME: Yarn's version depends on Alpine's version
// Yarn version // Yarn version
// yarnVersion: *"=~1.22" | string // yarnVersion: *"=~1.22" | string
// FIXME: custom base image not supported // FIXME: custom base image not supported
_buildImage: alpine.#Build & { alpine.#Build & {
packages: { packages: {
bash: {} bash: {}
yarn: {} yarn: {}
} }
},
docker.#Copy & {
dest: "/src"
contents: source
},
bash.#Run & {
// FIXME: move shell script to its own file
script: contents: #"""
yarn --cwd "$YARN_CWD" install --production false
"""#
mounts: "yarn cache": {
dest: "/cache/yarn"
contents: dagger.#CacheDir & {
// FIXME: are there character limitations in cache ID?
id: "universe.dagger.io/yarn.#Run \(name)"
}
} }
// Run yarn in a docker container env: {
container: bash.#Run & { YARN_CACHE_FOLDER: "/cache/yarn"
input: *_buildImage.output | docker.#Image YARN_CWD: cwd
}
workdir: "/src"
},
bash.#Run & {
// FIXME: move shell script to its own file // FIXME: move shell script to its own file
script: contents: #""" script: contents: #"""
# Create $ENVFILE_NAME file if set # Create $ENVFILE_NAME file if set
[ -n "$ENVFILE_NAME" ] && echo "$ENVFILE" > "$ENVFILE_NAME" [ -n "$ENVFILE_NAME" ] && echo "$ENVFILE" > "$ENVFILE_NAME"
yarn --cwd "$YARN_CWD" install --production false
opts=( $(echo $YARN_ARGS) ) opts=( $(echo $YARN_ARGS) )
yarn --cwd "$YARN_CWD" run "$YARN_BUILD_SCRIPT" ${opts[@]} yarn --cwd "$YARN_CWD" run "$YARN_BUILD_SCRIPT" ${opts[@]}
if [ ! -z "${YARN_BUILD_DIRECTORY:-}" ]; then if [ ! -z "${YARN_BUILD_DIRECTORY:-}" ]; then
@ -82,21 +105,13 @@ import (
fi fi
"""# """#
mounts: { mounts: "yarn cache": {
"yarn cache": {
dest: "/cache/yarn" dest: "/cache/yarn"
contents: dagger.#CacheDir & { contents: dagger.#CacheDir & {
// FIXME: are there character limitations in cache ID? // FIXME: are there character limitations in cache ID?
id: "universe.dagger.io/yarn.#Build \(name)" id: "universe.dagger.io/yarn.#Run \(name)"
} }
} }
"package source": {
dest: "/src"
contents: source
}
}
export: directories: "/build": _
env: { env: {
YARN_BUILD_SCRIPT: yarnScript YARN_BUILD_SCRIPT: yarnScript
@ -113,8 +128,15 @@ import (
} }
workdir: "/src" workdir: "/src"
},
]
} }
// The final contents of the package after build // The final contents of the package after build
output: container.export.directories."/build" _output: dagger.#Subdir & {
input: _build.output.rootfs
path: "/build"
}
output: _output.output
} }