Home > Software design >  GitHub Actions: Passing JSON data to another job
GitHub Actions: Passing JSON data to another job

Time:07-24

I'm attempting to pass an array of dynamically fetched data from one GitHub Action job to the actual job doing the build. This array will be used as part of a matrix to build for multiple versions. However, I'm encountering an issue when the bash variable storing the array is evaluated.

jobs:
  setup:
    runs-on: ubuntu-latest
    outputs:
      versions: ${{ steps.matrix.outputs.value }}
    steps:
      - id: matrix
        run: |
          sudo apt-get install -y jq && \
          MAINNET=$(curl https://api.mainnet-beta.solana.com -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","id":1, "method":"getVersion"}' | jq '.result["solana-core"]') && \
          TESTNET=$(curl https://api.testnet.solana.com -X POST -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","id":1, "method":"getVersion"}' | jq '.result["solana-core"]') && \
          VERSIONS=($MAINNET $TESTNET) && \
          echo "${VERSIONS[@]}" && \
          VERSION_JSON=$(echo "${VERSIONS[@]}" | jq -s) && \
          echo $VERSION_JSON && \
          echo '::set-output name=value::$VERSION_JSON'
        shell: bash
      - id: debug
        run: |
          echo "Result: ${{ steps.matrix.outputs.value }}"

  changes:
    needs: setup
    runs-on: ubuntu-latest
    # Set job outputs to values from filter step
    outputs:
      core: ${{ steps.filter.outputs.core }}
      package: ${{ steps.filter.outputs.package }}
    strategy:
      matrix:
        TEST: [buy, cancel, create_auction_house, delegate, deposit, execute_sale, sell, update_auction_house, withdraw_from_fee, withdraw_from_treasury, withdraw]
        SOLANA_VERSION: ${{fromJson(needs.setup.outputs.versions)}}
    steps:
      - uses: actions/checkout@v2
      # For pull requests it's not necessary to checkout the code
      - uses: dorny/paths-filter@v2
        id: filter
        with:
          filters: |
            core:
              - 'core/**'
            package:
              - 'auction-house/**'
      - name: debug
        id: debug
        working-directory: ./auction-house/program
        run: echo ${{ needs.setup.outputs.versions }}

In the setup job above, the two versions are evaluated to a bash array (in VERSIONS) and converted into a JSON array to be passed to the next job (in VERSION_JSON). The last echo in the matrix step results in a print of [ "1.10.31", "1.11.1" ], but the debug step prints out this:

Run echo "Result: "$VERSION_JSON""
  echo "Result: "$VERSION_JSON""
  shell: /usr/bin/bash -e {0}
  env:
    CARGO_TERM_COLOR: always
    RUST_TOOLCHAIN: stable
Result: 

The changes job also results in an error:

Error when evaluating 'strategy' for job 'changes'.
.github/workflows/program-auction-house.yml (Line: 44, Col: 25): Unexpected type of value '$VERSION_JSON', expected type: Sequence.

It definitely seems like the $VERSION_JSON variable isn't actually being evaluated properly, but I can't figure out where the evaluation is going wrong.

CodePudding user response:

  • For echo '::set-output name=value::$VERSION_JSON' you need to use double quotes or bash would not expand $VERSION_JSON.

  • set-output is not happy with multi-lined data. For your case, you can use jq -s -c so the output will be one line.

  • Related