Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a String to an Array for GitHub Actions matrix?

I have two jobs in GitHub Actions. One is running a Python script to get a list [[file1, 1.0], [file2, 3.0]] and the other is to put this list into the GitHub Actions matrix. From the Python script I receive a string which cannot be put into the matrix. How can I 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'] }}
like image 369
Weiwei Fan Avatar asked Jul 22 '26 00:07

Weiwei Fan


1 Answers

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

Reusable 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

like image 105
Ivan Porta Avatar answered Jul 24 '26 14:07

Ivan Porta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!