Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 216
mrh1997 Avatar asked Feb 14 '20 11:02

mrh1997


People also ask

Where do I put environment variables in GitHub?

To set a custom environment variable, you must define it in the workflow file. The scope of a custom environment variable is limited to the element in which it is defined. You can define environment variables that are scoped for: The entire workflow, by using env at the top level of the workflow file.

How do I echo variables in GitHub Actions?

You should use run: echo "$GITHUB. REPOSITORY" and run: echo "$GITHUB. REPOSITORY_OWNER" to see them directly on your workflow.


1 Answers

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.

like image 196
mrh1997 Avatar answered Sep 19 '22 13:09

mrh1997