From 0b329238d387d736177fb193881f895ac3f14cbf Mon Sep 17 00:00:00 2001 From: Taesung Hwang Date: Fri, 19 Jan 2024 21:34:35 -0800 Subject: [PATCH] Use Turborepo to cache `pip install` for API - Instead of having Vercel install the Python dependencies for the API, run `pip install` with Turborepo so the library files can be cached - Run system library installation before installing API dependencies --- apps/api/.gitignore | 1 + apps/api/package.json | 1 + apps/api/turbo.json | 9 +++++++++ apps/site/copy-api.sh | 6 +++++- apps/site/vercel-lib.sh | 6 ++---- apps/site/vercel.json | 2 +- 6 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 apps/api/turbo.json diff --git a/apps/api/.gitignore b/apps/api/.gitignore index babd53b3..17d8f875 100644 --- a/apps/api/.gitignore +++ b/apps/api/.gitignore @@ -1,3 +1,4 @@ __pycache__ *.key .coverage +lib diff --git a/apps/api/package.json b/apps/api/package.json index 2fb7e255..fe09966f 100644 --- a/apps/api/package.json +++ b/apps/api/package.json @@ -7,6 +7,7 @@ "dev:default": "./run-docker.sh", "test": "pytest", "lint": "flake8 src tests && mypy src tests", + "build": "mkdir -p lib && pip install --target lib -r requirements.txt", "format:write": "black src tests", "format:check": "black --check src tests" }, diff --git a/apps/api/turbo.json b/apps/api/turbo.json new file mode 100644 index 00000000..ff35aead --- /dev/null +++ b/apps/api/turbo.json @@ -0,0 +1,9 @@ +{ + "extends": ["//"], + "pipeline": { + "build": { + "inputs": ["requirements.txt"], + "outputs": ["lib/**"] + } + } +} diff --git a/apps/site/copy-api.sh b/apps/site/copy-api.sh index dc8416eb..34259954 100755 --- a/apps/site/copy-api.sh +++ b/apps/site/copy-api.sh @@ -1,4 +1,8 @@ # Copy API files from apps/api -cp -R ../api/{requirements.txt,configuration} . +cp -R ../api/configuration . cp -R ../api/src/ src/api/ cp ../api/index.py api/index.py + +# Copy Python libraries installed by during turbo build in apps/api +# instead of having Vercel install using requirements.txt +cp -R ../api/lib/* . diff --git a/apps/site/vercel-lib.sh b/apps/site/vercel-lib.sh index a6a51427..d8812cd0 100755 --- a/apps/site/vercel-lib.sh +++ b/apps/site/vercel-lib.sh @@ -21,12 +21,10 @@ lib_files=( libgpg-error.so.0.10.0 ) +# These system libraries will be included in the Lambda deployment files +# and accessible from Python through LD_LIBRARY_PATH mkdir lib for file in "${lib_files[@]}" do cp /usr/lib64/$file lib/ done - -echo $PWD -ls -ls lib diff --git a/apps/site/vercel.json b/apps/site/vercel.json index 12797496..88ec98ca 100644 --- a/apps/site/vercel.json +++ b/apps/site/vercel.json @@ -1,5 +1,5 @@ { - "buildCommand": "cd ../.. && turbo run build --filter={apps/site} && cd apps/site && ./copy-api.sh && ./vercel-lib.sh", + "buildCommand": "./vercel-lib.sh && cd ../.. && turbo run build --filter={apps/site} --filter={apps/api} && cd apps/site && ./copy-api.sh", "functions": { "api/index.py": { "memory": 512,