github动作矩阵中的成对值
我想针对 C 和 C++ 项目的三个编译器版本进行构建:gcc、gcc-8 和 clang(对于 C 编译器),对于 C++ 编译器,它们应该分别使用 g++、g++-8 和 clang++。
总共3个配置。我不想使用所有 C 和 C++ 编译器版本的产品进行构建,即。没有 gcc/g++-8 等。
如何指定具有这三种配置的矩阵,每种配置都设置两个变量?
目前我正在使用它(请注意,指定了 2 个操作系统,因此总共有 6 个配置):
strategy:
matrix:
os: [ubuntu-16.04, ubuntu-latest]
cpp_compiler: [g++, g++-8, clang++]
include:
- c_compiler: gcc
- cpp_ompiler: g++-8
c_compiler: gcc-8
- cpp_compiler: clang++
c_compiler: clang
本质上就是把C++编译器( cpp_compiler)作为主版本,然后include用hacky的方式c_compiler根据cpp_compiler版本来设置,但一定有更好的...
回答
我刚刚尝试了这个,结果证明你可以在构建矩阵中嵌套数组,但它没有记录。因此,您可以在job.matrix. 让我们称之为compiler-version:compiler-version: [[g++, gcc], [g++-8, gcc-8], [clang++, clang]]。然后,您可以使用compiler-version零索引访问数组的每个元素中的每个值:matrix.compiler-version[0]对于 C++ 编译器matrix.compiler-version[1]及其对应的 C。
您的工作流程将变成:
strategy:
matrix:
os: [ubuntu-16.04, ubuntu-latest]
compiler-version: [[g++, gcc], [g++-8, gcc-8], [clang++, clang]]
steps:
# .... checkout steps
- name: Compile file with C++ compiler
env:
COMPILER: ${{ matrix.compiler-version[0] }}
run: |
$COMPILER file.c
./a.out
- name: Compile file with C compiler
env:
COMPILER: ${{ matrix.compiler-version[1] }}
run: |
$COMPILER file.c
./a.out
我已经用 2 个 Python 版本和 2 个 Ubuntu 版本的矩阵编写了一个最小示例,其中 3.7 与 Ubuntu-18.04 配对,3.8 与 Ubuntu-20.04 配对。你可以看到工作流文件在这里和其相应的运行位置。
此解决方案解决了必须在工作流文件中的 2 个位置定义 C++ 选项的尴尬。即使它没有记录,我认为它是稳定的,因为 GitHub Actions 上下文对象是(或至少被视为)JavaScript 对象,所以我们实际上只是将数组文字定义为矩阵对象属性并访问它们的值。
- Some additions: The values of `matrix` can store `map` as well. Example: `val: [ { x: 'str1' }, { y: 'str2' } ]`, usage: `${{ matrix.val.x }}`. This will make the code more readable. But whether it's an `array` or a `map`, Actions' syntax checker will mark it with a red underline, and report an error `Matrix options must only contain primitive values`. It actually works, just ignore the error. However, since it is currently undocumented, all of this may change in the future.