在Github动作中映射环境变量

问题描述 投票:2回答:2

我创建了带有策略矩阵的GitHub Actions作业,该矩阵创建了一组环境变量。其中之一是machine_architecture,可以是32或64。

[在大多数步骤中,我可以直接使用它,即通过${{ machine_architecture }}。但是某些步骤需要使用诸如“ i386”和“ x86_64”之类的字符串。 github动作中是否有一种简单的方法来创建地图对象,我可以在表达式中使用它:

map_object = { 32: "i386", 64: 'x86_64' }
...
${{ map_object[machine_architecture] }}

如果没有,github动作中解决该问题的惯用方式是什么?

PS:我知道我可以set environment variables in steps,但问题是,这些变量仅可用于以下步骤(即,不能在“运行:”标记中使用)

github-actions
2个回答
0
投票
32=i386 64=x86_64

或您要保留的任何其他格式。

然后,您的工作可以在辅助变量中将其定义为:

jobs: FirstJob: name: job 1 runs-on: ..... steps: - uses: .... - name: Define variables run: | cat $(cat MAP_OBJECT_FILE_NAME) | grep $(cat machine_architecture)= | > MACHINE_ARCHITECTURE_STRING

从此开始,您将拥有MACHINE_ARCHITECTURE_STRING变量可用于所需的作业。当然,您可以执行更简单的连接或其他任何操作,但是在这里,您可以使用映射文件在代码中维护映射并且可以升级。


0
投票
jobs: varMap: strategy: matrix: machine_architecture: [32, 64] runs-on: ubuntu-latest steps: - name: Set arch var id: vars run: | echo ::set-output name=arch::\ $(echo '{ "32": "i386", "64": "x86_64" }' | jq -r 'to_entries[] | select(.key=="${{ matrix.machine_architecture }}") | .value') - name: Test arch var run: echo "Testing ${{ steps.vars.outputs.arch }}"
© www.soinside.com 2019 - 2024. All rights reserved.