github-actions

Map an environment variable in Github Actions


I created a GitHub Actions Job with a strategy matrix that creates a set of environment variables. One of them is machine_architecture which is either 32 or 64.

In most steps I can use it directly i.e. via ${{ machine_architecture }}. But some steps requires strings like 'i386' vs 'x86_64'. Is there an easy way in github actions to create a map-object that I can use in expressions like:

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

If not, what is the idiomatic way in github actions to solve that problem?

PS: I am aware, that I can set environment variables in steps, but the problem is, that these variables are only available for the following steps (i.e. not for usage in "run-on:" tag)


Solution

  • In the meantime I found a solution:

    Although GitHub Actions has no syntax for directly creating a Mappings/Objects it can be done indirectly with fromJson():

    ${{ fromJson('{ 32:"i386", 64:"x86_64" }')[machine_architecture] }}
    

    This fromJson() will create a mapping from int to string. the following [] operator resolves the int type machine_architecture to a string type.