我正在尝试使用 CodeBuild 缓存我的 Python 项目的虚拟环境文件夹 (.venv)。
这是我的
buildspec.yml
文件:
version: 0.2
env:
shell: bash
phases:
install:
commands:
- python3 -m venv .venv && source .venv/bin/activate
- pip3 install -r requirements.txt
build:
commands:
- pytest -v tests/
cache:
paths:
- .venv/**/*
这是我得到的错误:
[Container] 2022/11/10 11:33:20 MkdirAll: /codebuild/local-cache/custom/11615810f/.venv
[Container] 2022/11/10 11:33:20 Symlinking: /codebuild/output/src682375820/src/hello.org/demo/.venv => /codebuild/local-cache/custom/11615810f/.venv
[Container] 2022/11/10 11:34:01 Running command python3 -m venv .venv && source .venv/bin/activate
Error: Unable to create directory '/codebuild/output/src682375820/src/hello.org/demo/.venv'
[Container] 2022/11/10 11:34:01 Command did not exit successfully python3 -m venv .venv && source .venv/bin/activate exit status 1
[Container] 2022/11/10 11:34:01 Phase complete: INSTALL State: FAILED
[Container] 2022/11/10 11:34:01 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: python3 -m venv .venv && source .venv/bin/activate. Reason: exit status 1
问题是您无法将 Python 虚拟环境实例化到符号链接文件夹中。
有什么想法吗?
CodeBuild 使用符号链接来链接缓存目录,并且
python -m venv
尝试通过符号链接创建新目录,但这是不可能的。
尝试在
python3 -m venv
目录中运行 .venv
命令:install:
commands:
- cd .venv && python3 -m .venv . && cd -
- source .venv/bin/activate
- pip3 install -r requirements.txt
如果您检查 CodeBuild 日志,您会看到符号链接是从一开始就创建的:
# Example when /opt/poetry is cached
[Container] 2023/12/11 08:10:45.046132 Expanded cache path /opt/poetry/
[Container] 2023/12/11 08:10:45.495963 MkdirAll: /codebuild/local-cache/custom/5694e39f8fd7f99453b01828b7946ba4ef802b635c8c9ec0be9b56824a16d47c/opt/poetry
[Container] 2023/12/11 08:10:45.496095 Symlinking: /opt/poetry => /codebuild/local-cache/custom/5694e39f8fd7f99453b01828b7946ba4ef802b635c8c9ec0be9b56824a16d47c/opt/poetry