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
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:
You need to convert the values in the
VERSIONS
array to strings:and, then transform the whole JSON to a raw string literal for
fromJSON
by escaping double quotes:Alternatively, you may automate this escaping using
jq
while setting the output i.e. with:set output like this:
or,
See
jq
‘s Format strings and escaping for more details.