How to convert String to Array and put it in github actions matrix?

Viewed 2453

I have GitHub actions two jobs. One is running a python script to get a list [[file1, 1.0], [file2, 3.0]] and the other is to put this list in the GitHub actions matrix. But I got from python script is a string and it cannot be put into matrix. How to solve this?

on:
  pull_request:
    branches: [ master, develop ]
    types: [ closed ]

jobs:
  compute-version:
    runs-on: ubuntu-latest
    
    outputs:
      list: ${{ steps.semver.outputs.list }}
    
    steps:
      - name: Run Computer Version Script
        id: semver
        run: |
          python ./.github/bump-version.py $GITHUB_REF
          res=$?
          echo "::set-output name=list::$res"
          
  update-yaml:
    needs: compute-version
    
    runs-on: ubuntu-latest
    
    strategy:
          matrix:
            node: ${{needs.compute-version.outputs.bump_list}}

    steps:
      - name: Checkout
        uses: actions/checkout@v2
      
      - name: Update Version on Yaml File
        uses: fjogeleit/yaml-update-action@master
        with:
          valueFile: ${{ matrix.node['file'] }}
          propertyPath: info.version
          value: ${{ matrix.node['version'] }}
          commitChange: true
          updateFile: true
          targetBranch: ${{needs.compute-version.outputs.target_branch}}
          masterBranchName: master
          createPR: false
          branch:  ${{needs.compute-version.outputs.target_branch}}
          message: Bump version to ${{ matrix.node['version'] }} in ${{ matrix.node['file'] }}
2 Answers

Using toJson and fromJSON is the only way to pass variables like Array in GitHub actions.

If you case, here is a good example on how to build what you are looking for.

More details explained on GitHub Blog post here

name: build
on: push
jobs:
  job1:
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.set-matrix.outputs.matrix }}
    steps:
    - id: set-matrix
      run: echo "::set-output name=matrix::{\"include\":[{\"project\":\"foo\",\"config\":\"Debug\"},{\"project\":\"bar\",\"config\":\"Release\"}]}"
  job2:
    needs: job1
    runs-on: ubuntu-latest
    strategy:
      matrix: ${{fromJson(needs.job1.outputs.matrix)}}
    steps:
    - run: build

Action inputs only support string keys and string values at this time. However, you an JSON array and then convert it using the fromJSON expression (https://docs.github.com/en/actions/learn-github-actions/expressions#fromjson).

This is an example of passing an array to a reusable workflow.

GitHub action

name: CodeQL
on:
 workflow_dispatch:
jobs:
  test:
    uses: GTRekter/Training/.github/workflows/ReusableWorkflow_CodeQL.yml@main
    with:
      languageJson: '["csharp", "javascript"]'

Rusable workflow

name: CodeQL static test
on:
  workflow_call:
    inputs:
      languageJson: 
        description: 'Programming language selection [go, javascript, csharp, python, cpp, java]'
        required: true
        type: string
jobs:
  setup:
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.setVariables.outputs.matrix }}
    steps:
      - id: setVariables
        run: |
          echo "::set-output name=matrix::$input"
        env:
          input: ${{ inputs.languageJson }}
  codeql:
    runs-on: ubuntu-latest
    needs: Setup
    permissions:
      security-events: write
    strategy:
      fail-fast: false
      matrix:
        language: ${{fromJson(needs.setup.outputs.matrix)}}
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
      - name: Initialize CodeQL
        uses: github/codeql-action/init@v2
        with:
          languages: ${{ matrix.language }}
          config-file: ./.github/codeql/codeql-config.yml 
      - name: Autobuild
        if: matrix.language == 'cpp' || matrix.language == 'csharp' 
        uses: github/codeql-action/autobuild@v2
      - name: Build Java
        if: matrix.language == 'java'
        run: |
          make bootstrap
          make release
      - name: Perform CodeQL Analysis
        uses: github/codeql-action/analyze@v2

Result:

enter image description here

Related