如何在GitHub Actions中的运行之间缓存已安装的工具? [英] How do I cache an installed tool between runs in GitHub Actions?
问题描述
我需要运行 poetry version
,以在每次触摸 pyproject.toml
时获得 pyproject.toml
版本.但是,由于Poetry未安装在GitHub Actions运行程序虚拟环境中,因此我还需要先安装它,然后才能运行任何Poetry命令.我想缓存此工具的安装,因此不必在每次运行时都安装它并用尽Actions分钟.我唯一需要运行的Poetry命令是 poetry version
,因此我不必担心该工具已过时-它只需要解析 pyproject.toml
并获取项目版本号.我也不确定要使用什么作为我的 key
缓存操作-我认为它可以是静态的
I need to run poetry version
to get the pyproject.toml
version on every push to master touching pyproject.toml
. However, since Poetry is not installed on GitHub Actions runner virtual environments, I also need to install it before I can run any Poetry commands. I want to cache this tool installation so I don't have to install it on every run and use up Actions minutes. The only Poetry command I need to run is poetry version
, so I'm not worried about the tool being outdated - it only needs to parse the pyproject.toml
and get the project version number. I'm also not sure what to use as my key
for the caching action - I assume it can be static
所需的操作顺序如下:
- 签出仓库.
- 检查缓存中的诗歌.如果尚未安装,请安装它.
- 运行
诗歌版本
.
推荐答案
actions/cache @ v2
的 key
输入可以是字符串-提供任意内容. path
输入是工具的位置.
The key
input of actions/cache@v2
can be a string - provide it something arbitrary. The path
input is the location of the tool.
- 请注意,
path
参数不会解析诸如$ HOME
之类的环境变量,但代字号(〜
)可用于表示主目录. - 由于每次运行之间都不会保留默认的环境变量,因此每次运行时都必须在PATH之前添加诗歌.
- 诗歌可能会抱怨它即将放弃对Python2的支持-为确保它与Python 3一起运行,请确保使用任意Python 3版本设置运行.
- Note that the
path
argument does NOT resolve environment variables like$HOME
, but the tilde (~
) can be used to signify the home directory. - Poetry has to be prepended to the PATH on every run, as default environment variables are not preserved between runs.
- Poetry may complain that it's dropping support for Python2 soon - to ensure it's running with Python 3, make sure to setup the run with any of the Python 3 versions.
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://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | 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
这篇关于如何在GitHub Actions中的运行之间缓存已安装的工具?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!