我需要运行 poetry version
以在每次推送到 master touching pyproject.toml
时获取 pyproject.toml
版本。但是,由于 GitHub Actions runner 虚拟环境中未安装 Poetry,因此我还需要先安装它才能运行任何 Poetry 命令。我想缓存此工具安装,这样我就不必在每次运行时都安装它并用完 Actions 分钟数。我唯一需要运行的 Poetry 命令是 poetry version
,所以我不担心该工具已过时 - 它只需要解析 pyproject.toml
并获取项目版本号。我也不确定将什么用作缓存操作的 key
- 我假设它可以是静态的
所需的操作顺序类似于:
- 查看存储库。
- 检查缓存中的诗歌。如果未安装,请安装。
- 运行
诗歌版
。
最佳答案
actions/cache@v2
的key
输入可以是一个字符串——给它任意的东西。 path
输入是工具的位置。
潜在陷阱:
- 请注意,
path
参数不会像$HOME
那样解析环境变量,但波浪号 (~
) 可用于表示主目录。 - Poetry 必须在每次运行时添加到 PATH 之前,因为默认环境变量不会在运行之间保留。
- Poetry 可能会提示它即将放弃对 Python2 的支持 - 为确保它与 Python 3 一起运行,请确保使用任何 Python 3 版本设置运行。
on:
push:
branches:
- master
paths:
- 'pyproject.toml'
jobs:
pyproject-version:
runs-on: 'ubuntu-latest'
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: '3.7'
# Perma-cache Poetry since we only need it for checking pyproject version
- name: Cache Poetry
id: cache-poetry
uses: actions/cache@v2
with:
path: ~/.poetry
key: poetry
# Only runs when key from caching step changes
- name: Install latest version of Poetry
if: steps.cache-poetry.outputs.cache-hit != 'true'
run: |
curl -sSL https://install.python-poetry.org | python -
# Poetry still needs to be re-prepended to the PATH on each run, since
# PATH does not persist between runs.
- name: Add Poetry to $PATH
run: |
echo "$HOME/.poetry/bin" >> $GITHUB_PATH
- name: Get pyproject version
run: poetry version
关于continuous-integration - 如何在 GitHub Actions 中的运行之间缓存已安装的工具?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65801828/