使用 actions/setup-python 的可重用 GitHub 操作工作流失败,因为它找不到要求.txt



GitHub存储库A包含一个可重用的GitHub操作工作流,其中myscript是一个围绕python依赖项的bash脚本包装器:

name: 'A'
[...]
runs:
using: 'composite'
steps:
- uses: actions/setup-python@v4
with:
python-version: '3'
cache: 'pip'
- run: pip install -r requirements.txt
shell: bash
- run: myscript
shell: bash

现在在存储库B中,我重用该操作:

name: 'B'
on:
workflow_dispatch:
push:
branches:
- master
jobs:
shacl:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Build and Validate
uses: username/repoa@v1
with: [...]

然而,现在我在操作日志中得到一个错误:

Run actions/setup-python@v4
with:
python-version: 3
cache: pip
check-latest: false
token: ***
update-environment: true


Successfully set up CPython (3.10.6)
Error: No file in /home/runner/work/ontology/ontology matched to [**/requirements.txt], make sure you have checked out the target repository

现在,如果这只是由于文件在错误的存储库中引起的,这将是一个足够的问题,但当完全删除requirements.txt的任何用法时,错误甚至会持续存在:

[...]
runs:
using: 'composite'
steps:
- uses: actions/setup-python@v4
with:
python-version: '3'
cache: 'pip'
- run: pip install mydependency
shell: bash
[...]

错误仍然会发生,因为setup python将尝试使用requirements.txt作为其缓存密钥,但失败了。setup python有一个cache-dependency-path属性,但我没有任何文件可以指向它,因为所有文件都在存储库a中,但我已经签出了存储库B。

如何在具有缓存的可重用工作流中使用actions/setup-python而不会出现此错误?

工作流中存在两个独立的问题:

1.对远程文件的本地引用

这是通过使用github.action_path从存储库B引用存储库A:中的myscript文件来修复的

- run: ${{ github.action_path }}/myscript
shell: bash

2.在没有文件的情况下使用actions/setup python进行哈希

默认依赖文件requirements.txt可以使用cache-dependency-path重写。然而,由于找不到文件,这与${{ github.action_path }}组合不起作用:

# do not do this, it fails
- uses: actions/setup-python@v4
with:
python-version: '3'
cache: 'pip'
cache-dependency-path: "${{ github.action_path }}/requirements.txt" # file not found

这可以通过直接安装依赖项、禁用操作/设置python缓存并使用通用缓存操作来解决:

- uses: actions/setup-python@v4
with:
python-version: '3'
- uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip
- run: pip install 'mydependency<2'
shell: bash
- run: ${{ github.action_path }}/myscript
shell: bash

最新更新