6
votes

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)

3

3 Answers

1
votes

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.

0
votes

How about your "map_object" is actually a file mapping machine_architecture values into the values you need, such:

32=i386
64=x86_64

or any other format you want to keep.

Then, your job can define it in a secondary variable as:

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

From there on, you would have the MACHINE_ARCHITECTURE_STRING variable available for the jobs you need. You could of course do it much more simple concatenating or whatever, but here you maintain the mapping in your code with the mapping file and is escalable.

0
votes

Here is a way to do it with JSON and jq. It creates the step output ${{ steps.vars.outputs.arch }} which you can use in later steps.

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 }}"