最近,我开始将一些开源python库从特拉维斯迁移到Github行动。为了更加独立于CI/CD平台,我决定首先使用nox描述所有测试环境和配置。
让我们考虑以下原型noxfile.py:
import nox
@nox.session(python=["3.7", "3.8"])
def tests(session):
print("Interesting stuff going on here")
@nox.session(python=["3.7", "3.8"])
@nox.parametrize("a", [-1, 1])
@nox.parametrize("b", [False, True])
def tests_with_params(session, a, b):
print("Interesting stuff going on here")
@nox.session(python="3.7")
def other(session):
print("another session")导致以下nox --list输出:
* tests-3.7
* tests-3.8
* tests_with_params-3.7(b=False, a=-1)
* tests_with_params-3.7(b=True, a=-1)
* tests_with_params-3.7(b=False, a=1)
* tests_with_params-3.7(b=True, a=1)
* tests_with_params-3.8(b=False, a=-1)
* tests_with_params-3.8(b=True, a=-1)
* tests_with_params-3.8(b=False, a=1)
* tests_with_params-3.8(b=True, a=1)
* other我希望github操作工作流运行tests或tests_with_params的所有会话。在我的工作流yaml定义文件中使用硬编码的构建矩阵可以工作,例如,这里我列出了tests的两个会话
# (in .github/workflows/base.yml)
jobs:
run_all_tests:
strategy:
fail-fast: false
matrix:
# Here we manually list two nox sessions
nox_session: ["tests-3.7", "tests-3.8"]
name: Run nox session ${{ matrix.nox_session }}
runs-on: ubuntu-latest
steps:
# (other steps before this: checkout code, install python and nox...)
- run: nox -s "${{ matrix.nox_session }}"但是,如上文所示,必须在矩阵中手动复制会话列表。因此,如果我决定改变我的nox会话的参数化,这将需要更新。
因此,让github操作工作流"ask nox“动态获取这个列表要容易得多。我们怎么能这么做?
发布于 2021-03-22 13:47:49
(回答我自己的问题,因为我找不到这样的条目)
多亏了这个伟大的职位,我找到了一种方法来做到这一点。该解决方案基于两个步骤:
gha_list,该任务将打印给定基本会话名的所有会话名称的列表。1.创建一个nox任务以打印所需的会话列表
让我们将这个任务添加到我们的noxfile.py中
import itertools
import json
@nox.session(python=False)
def gha_list(session):
"""(mandatory arg: <base_session_name>) Prints all sessions available for <base_session_name>, for GithubActions."""
# get the desired base session to generate the list for
if len(session.posargs) != 1:
raise ValueError("This session has a mandatory argument: <base_session_name>")
session_func = globals()[session.posargs[0]]
# list all sessions for this base session
try:
session_func.parametrize
except AttributeError:
sessions_list = ["%s-%s" % (session_func.__name__, py) for py in session_func.python]
else:
sessions_list = ["%s-%s(%s)" % (session_func.__name__, py, param)
for py, param in itertools.product(session_func.python, session_func.parametrize)]
# print the list so that it can be caught by GHA.
# Note that json.dumps is optional since this is a list of string.
# However it is to remind us that GHA expects a well-formatted json list of strings.
print(json.dumps(sessions_list))我们可以测试它是否在终端中工作:
>>> nox -s gha_list -- tests
nox > Running session gha_list
["tests-3.7", "tests-3.8"]
nox > Session gha_list was successful.
>>> nox -s gha_list -- tests_with_params
nox > Running session gha_list
["tests_with_params-3.7(b=False, a=-1)", "tests_with_params-3.7(b=True, a=-1)", "tests_with_params-3.7(b=False, a=1)", "tests_with_params-3.7(b=True, a=1)", "tests_with_params-3.8(b=False, a=-1)", "tests_with_para
ms-3.8(b=True, a=-1)", "tests_with_params-3.8(b=False, a=1)", "tests_with_params-3.8(b=True, a=1)"]
nox > Session gha_list was successful.2.编辑github操作工作流,将此列表动态注入生成矩阵中。
现在我们能够打印想要的列表了,我们修改了github操作工作流,在运行测试的作业之前添加一个作业,调用它为。
# (in .github/workflows/base.yml)
jobs:
list_nox_test_sessions:
runs-on: ubuntu-latest
steps:
# (other steps before this: checkout code, install python and nox...)
- name: list all test sessions
id: set-matrix
run: echo "::set-output name=matrix::$(nox -s gha_list -- tests)"
# save the list into the outputs
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
run_all_tests:
needs: list_nox_test_sessions
strategy:
fail-fast: false
matrix:
nox_session: ${{ fromJson(needs.list_nox_test_sessions.outputs.matrix) }}
name: Run nox session ${{ matrix.nox_session }}
runs-on: ubuntu-latest
steps:
# (other steps before this: checkout code, install python)
- run: nox -s "${{ matrix.nox_session }}"示例
在这个回购中可以找到一个示例,其工作流如下:

其他可行的替代办法
我还考虑在grep输出上使用nox --list,这可能也会奏效。但是,对于轻松使用python但不容易使用grep的开发人员来说,调试可能会更困难。
https://stackoverflow.com/questions/66747359
复制相似问题