skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    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']"
    

  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search