6

I'm writing a github actions workflow where I take in an array of strings as a global env variable and want to read it in as a part of the matrix strategy.

I get the following error:

Error when evaluating 'strategy' for job '$JOB_NAME'. .github/workflows/deploy.yaml (Line: 53, Col: 18): Error parsing fromJson,.github/workflows/deploy.yaml (Line: 53, Col: 18): Input string '9.0.0' is not a valid number. Path '[0]', line 1, position 6.,.github/workflows/deploy.yaml (Line: 53, Col: 18): Unexpected value ''

This is what my setup currently looks like. I have the job 'prepare-matrix' set up since matrix can't access the env context directly.

env:
  VERSIONS: '[9.0.0, 11.0.0]'

jobs:
  prepare-matrix:
    name: Prepare Matrix Output
    runs-on: ubuntu-latest
    outputs: 
      all_versions: ${{ steps.step1.outputs.matrix }}
    steps: 
      - name: Create Matrix Variable
        id: step1
        run: echo "matrix=${{env.VERSIONS}}" >> $GITHUB_OUTPUT

  deploy-gke:
    name: Deploy to GKE
    runs-on: ubuntu-latest
    needs: [prepare-matrix]
    strategy: 
      max-parallel: 2
      matrix: 
        version: ${{ fromJSON(needs.prepare-matrix.outputs.all_versions) }}
    steps:
      ...(several steps to complete job)....

I believe there is some formatting issue wrong somewhere either in env.VERSIONS, or in matrix.version, and I've tried changing parentheses, quotes, and removing the fromJSON(), but nothing seems to resolve the issue.

3
  • Try: VERSIONS: '["9.0.0", "11.0.0"]' Commented Apr 18, 2023 at 13:31
  • still running into the same issue Commented Apr 18, 2023 at 16:23
  • 1
    Need to escape those quotes as well i.e. VERSIONS: '[\"9.0.0\", \"11.0.0\"]'. Commented Apr 18, 2023 at 16:59

4 Answers 4

4

For anyone else struggling with the same problem, what ended up working was actually a simple fix - I simply needed to use ' within the array and " around it.

So the variable should be defined as:

VERSIONS: "['9.0.0', '11.0.0']"
Sign up to request clarification or add additional context in comments.

Comments

1

You need to convert the values in the VERSIONS array to strings:

VERSIONS: '["9.0.0", "11.0.0"]'

and, then transform the whole JSON to a raw string literal for fromJSON by escaping double quotes:

VERSIONS: '[\"9.0.0\", \"11.0.0\"]'

Alternatively, you may automate this escaping using jq while setting the output i.e. with:

VERSIONS: '["9.0.0", "11.0.0"]'

set output like this:

echo "matrix=$(jq -cr '@json' <<< "$VERSIONS")" >> $GITHUB_OUTPUT

or,

echo "matrix=$(jq -cr '@json' <<< "${{ env.VERSIONS }}")" >> $GITHUB_OUTPUT

See jq's Format strings and escaping for more details.

Comments

1

I'm an iOS developer, and I faced a similar challenge. However, I'm not very proficient in this, so I took a slightly different approach. I used actions/github-script@v6 (JavaScript) to achieve my goal.

Below is the template I'd like to share:

jobs:
  pre-job:
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.set-matrix.outputs.result }}
    steps:
    - uses: actions/github-script@v6
      id: set-matrix
      env:
        # comma-separated values
        # example: ${{ vars.YOUR_DYNAMIC_MATRIX }}
        ARRAY_STRING: "eg,1,2"
      with:
        result-encoding: string
        script: |
          // JavaScript code
          const { ARRAY_STRING } = process.env;
          var objects = ARRAY_STRING
              .split(',')
              .map(value => ({varName1: 'thing', varName2: value}) );

          // [
          //   { varName1: 'thing', varName2: 'eg' },
          //   { varName1: 'thing', varName2: '1' },
          //   { varName1: 'thing', varName2: '2' }
          // ]
          console.log(objects);
          return JSON.stringify(objects);

  main-job:
    name: "varName2: ${{ matrix.object.varName2 }}"
    runs-on: ubuntu-latest
    needs: pre-job
    strategy:
      fail-fast: false
      matrix: 
        object: ${{ fromJson(needs.pre-job.outputs.matrix) }}
    steps:
    - run: |
        echo "varName1: ${{ matrix.object.varName1 }}"

Hope you enjoy.

Comments

0

I fixed the issue by adding '' around the ${{FromJson(...)}}

Generate a a json array from one job that would output: ["object1","object2"], pass that to an output, and then add '' like said above

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.