GitHub Actions中Poetry安装依赖占用过多磁盘空间问题求助
问题分析与解决方案
原因分析
- Poetry全局缓存残留:从头安装依赖时,Poetry会将下载的Python包(.whl文件)存储在全局缓存目录(默认
~/.cache/pypoetry),安装完成后这些包文件仍会保留。若清理操作未覆盖全局缓存,就会导致磁盘被冗余包文件占用。 - 虚拟环境临时文件未清理:Poetry构建虚拟环境、安装依赖的过程中,会生成编译扩展包源码、安装日志等临时文件,这类文件通常不会自动清理,会额外占用磁盘空间。
- 缓存策略覆盖不全:当前仅缓存项目虚拟环境,但未缓存Poetry全局包缓存。无缓存可用时需重新下载所有依赖包,这些下载的包会占用额外磁盘;而复用缓存时,依赖包已存在于全局缓存,无需重复下载。
解决方案
1. 同时缓存Poetry全局缓存与虚拟环境
修改GitHub Actions缓存配置,同时缓存Poetry全局缓存目录和项目虚拟环境,减少重新下载依赖的磁盘占用:
- name: Cache Poetry dependencies uses: actions/cache@v3 with: path: | ~/.cache/pypoetry .venv key: ${{ runner.os }}-poetry-${{ hashFiles('poetry.lock') }} restore-keys: | ${{ runner.os }}-poetry-
2. 安装后清理Poetry全局缓存
依赖安装完成后,主动清理Poetry的下载缓存,释放磁盘空间:
- name: Install dependencies run: poetry install --no-root - name: Clean Poetry cache run: poetry cache clear pypi --all
3. 自定义虚拟环境路径并清理旧环境
指定明确的虚拟环境路径,安装前清理可能残留的旧虚拟环境,避免重复占用:
- name: Set Poetry virtual env path run: poetry config virtualenvs.in-project true - name: Clean existing virtual env run: rm -rf .venv || true - name: Install dependencies run: poetry install --no-root
4. 清理系统临时文件与pip缓存
额外清理系统临时目录和pip缓存,进一步释放磁盘:
- name: Clean system temp files run: | rm -rf /tmp/* rm -rf ~/.cache/pip
5. 优化Poetry安装参数
安装依赖时添加参数减少冗余输出和交互,避免生成不必要的日志文件:
- name: Install dependencies run: poetry install --no-root --no-interaction --no-ansi
内容的提问来源于stack exchange,提问作者Alex
相关产品推荐
相关产品推荐

