0

I am converting a GitHub Actions file from a version that contains multiple mostly copy-paste Docker build steps using environment variables, to a version that uses a one-dimensional matrix with multiple values and a single Docker build action.

Problem is I can't get the matrix expanded from environment variables.

I am aware that one ENV can't contain another ENV, and a solution is to set in one build step and use in another.

I am aware that the matrix entry does not expand environment variables, and an option is use JSON text in one step and fromJSON() in the matrix step.

From this action file, the error is: line 1: {: command not found

It looks like the shell is interpreting the { as a new command and ignoring the \ line continuation in the previous line.

Code snippets:

      run: |
        JSON=[ { \
                  "name": "NxMeta-LSIO Latest Develop", \
                  "enable": ${{ (github.ref == 'refs/heads/develop') }}, \
                  "push": ${{ (github.event_name != 'pull_request') }}, \
                  "context": ./NxMeta-LSIO, \
                  "tags": [ ptr727/nxmeta-lsio:develop, ptr727/nxmeta-lsio:develop-latest, ptr727/nxmeta-lsio:develop-${{ env.NXMETA_LATEST_VERSION }} ], \
                  "args": [ DOWNLOAD_VERSION=${{ env.NXMETA_LATEST_VERSION }}, DOWNLOAD_URL=${{ env.NXMETA_LATEST_URL }} ] \
                }, \
                { \
                  "name": "NxMeta-LSIO Stable Develop", \
                  "enable": ${{ (github.ref == 'refs/heads/develop') }}, \
                  "push": ${{ (github.event_name != 'pull_request') }}, \
                  "context": ./NxMeta-LSIO, \
                  "tags": [ ptr727/nxmeta-lsio:develop-stable, ptr727/nxmeta-lsio:develop-${{ env.NXMETA_STABLE_VERSION }} ], \
                  "args": [ DOWNLOAD_VERSION=${{ env.NXMETA_STABLE_VERSION }}, DOWNLOAD_URL=${{ env.NXMETA_STABLE_URL }} ] \
                }, \
...
      echo "::set-output name=matrix::${JSON//'%'/'%25'}"
matrix:
  containers: ${{fromJson(needs.setmatrix.outputs.matrix)}}

Any ideas?

PieterV
  • 555
  • 1
  • 4
  • 18
  • 1
    I think your problem is that you don't quote the json. Something like `myVar={"foo": "bar"}` is not valid, because your shell is going to interpret the `{`. You need to do `myVar='{"foo": "bar"}'`. – rethab Jul 07 '22 at 06:24
  • I added single quotes, the first step now completes, the error is now in the matrix section: `Error when evaluating 'strategy' for job 'build'. .github/workflows/BuildPublishPipeline.yml (Line: 182, Col: 21): Error parsing fromJson,.github/workflows/BuildPublishPipeline.yml (Line: 182, Col: 21): Invalid property identifier character: \. Path '[0]', line 1, position 4.,.github/workflows/BuildPublishPipeline.yml (Line: 182, Col: 21): Unexpected type of value '', expected type: Sequence.` – PieterV Jul 07 '22 at 14:10
  • I would recommend to start with something small and make that work. – rethab Jul 07 '22 at 14:25
  • I now added single quotes and removed the `\\` line continuation character. I added a step to print the raw variable, and convert to JSON. I can see that the assignment step sets the full string, the verify step echo only prints the first line, and then toJSON() will fail as malformed. Any other ideas? – PieterV Jul 07 '22 at 14:26
  • Some more experimentation; the JSON text set in the first step is now well formed (can echo it fine), by the second step the variable the multiline has been converted to just the first line (echo only prints the first line), and then obviously the JSON is not well formed. Is there some problem with passing variables between steps when multi-line? – PieterV Jul 07 '22 at 15:15
  • Ah, seems it is an issue with multiline: https://github.community/t/set-output-truncates-multiline-strings/16852/2 – PieterV Jul 07 '22 at 15:28

1 Answers1

0

Got it working:

  • Enclose the JSON with single quotes.
  • Do not use line continuation character.
  • Multiline transfer between steps is not supported, encode the text before transfer.
PieterV
  • 555
  • 1
  • 4
  • 18