英文:
Github Actions, set shell via matrix variable
问题
I have a job in Github Actions that uses a matrix. I want to run some of the job steps on a custom shell that depends on a matrix variable.
Specifically, the image has a foreign chroot so I can emulate any hardware architecture and run native binaries for it. The custom shell is just a wrapper script around bash which runs the commands in the chroot and with the emulator for the desired target architecture.
The matrix looks about like this:
cross_build:
strategy:
matrix:
job:
- { release: bullseye , arch: armhf , ocaml-version: 4.14.0 , publish: true }
I have tried
shell: bash-${{ matrix.job.arch }} {0}
(bash-$ARCH
being the wrapper script), but get an error:
The workflow is not valid. .github/workflows/CI.yml (Line: mmm, Col: nn): Unrecognized named-value: 'matrix'.
If I hardcode the architecture in the shell, everything works:
shell: bash-armhf {0}
So the issue really is about resolving variables in the shell
element.
Is there a way to pass a variable in this place so that different instances of the job run on different custom shells?
If not, I would need to have one bash-wrapper
script and somehow pass it the information about the target platform I would like to use. A command line argument is presumably not an option if I can’t pass variables in shell
– so what is the standard way of passing that information to the custom shell?
英文:
I have a job in Github Actions that uses a matrix. I want to run some of the job steps on a custom shell that depends on a matrix variable.
Specifically, the image has a foreign chroot so I can emulate any hardware architecture and run native binaries for it. The custom shell is just a wrapper script around bash which runs the commands in the chroot and with the emulator for the desired target architecture.
The matrix looks about like this:
cross_build:
strategy:
matrix:
job:
- { release: bullseye , arch: armhf , ocaml-version: 4.14.0 , publish: true }
I have tried
shell: bash-${{ matrix.job.arch }} {0}
(bash-$ARCH
being the wrapper script), but get an error:
The workflow is not valid. .github/workflows/CI.yml (Line: mmm, Col: nn): Unrecognized named-value: 'matrix'.
If I hardcode the architecture in the shell, everything works:
shell: bash-armhf {0}
So the issue really is about resolving variables in the shell
element.
Is there a way to pass a variable in this place so that different instances of the job run on different custom shells?
If not, I would need to have one bash-wrapper
script and somehow pass it the information about the target platform I would like to use. A command line argument is presumably not an option if I can’t pass variables in shell
– so what is the standard way of passing that information to the custom shell?
答案1
得分: 0
如Azeem指出,shell
中的变量当前不受支持。
这个方法有效:
在工作流程定义中,将以下内容添加到作业中:
env:
CROSS_ARCH: ${{ matrix.job.arch }}
然后,对于自定义 shell,请指定包装器脚本的路径 - 一个单一脚本,不考虑目标体系结构的变量在 shell
中无法使用。
在包装器脚本中,评估 CROSS_ARCH
并基于此选择模拟器和 chroot。
由于我已经有多个副本的单个脚本,这些脚本将根据调用名称确定体系结构,因此实施起来非常容易,甚至比通过去掉开头将 /bin/bash-ARCH
转换为 ARCH
更清晰。
英文:
As pointed out by Azeem, variables in shell
are currently not supported.
This works:
In the workflow definition, add the following to the job:
env:
CROSS_ARCH: ${{ matrix.job.arch }}
Then, for the custom shell, specify the path to the wrapper script – one single script regardless of target architecture, thus no use of variable in shell
.
In the wrapper script, evaluate CROSS_ARCH
and choose the emulator and chroot based on that.
Since I already had multiple copies of a single script, which would determine the architecture by the name with which it was invoked, this was very easy to implement and is even cleaner than converting /bin/bash-ARCH
into ARCH
by stripping the beginning.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论