githubgithub-actions

Combine dynamic Github Workflow matrix with input values and predefined values


I have a working GitHub workflow that uses a matrix and builds all defined products.

name: Build

on:
  push:
    tags:
      - "*"
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
       product: [yellow, green, black]
       limits: [10,20,50,100]

    steps:
      - uses: actions/checkout@v2
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

    ...

Now a wanted to be able to trigger the build process manually and use input values to only build the product I want. I can enter values on Github interface, but it looks the syntax of using them is not right.

Build : .github#L1 Error when evaluating 'strategy' for job 'build'. .github/workflows/build.yml (Line: 27, Col: 18): Unexpected value 'yellow',.github/workflows/build.yml (Line: 28, Col: 17): Unexpected value '50'

Also, how would one combine previous automatic builds of all predefined products with the one manually done via inputs inside one workflow?

name: Build

on:
  push:
    tags:
      - "*"
  workflow_dispatch:
    inputs:
      product:
        description: "Product"
        default: "yellow"
      limit:
        description: "Limit"
        default: "50"

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        product: ${{ github.event.inputs.product}}
        limits: ${{ github.event.inputs.limit }}

        # product: [yellow, green, black]
        # limits: [10,20,50,100]

    steps:
      - uses: actions/checkout@v2
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

    ...

Solution

  • The matrix values have to be lists, such as [yellow, green, black]. Your input named product was not a list, it was the string "yellow". Unfortunately, input data type can only be one of string, choice, boolean, or environment.

    However, you can convert the string '["yellow", "green", "black"]' to a json list value and specify that as the matrix value using the fromJSON(value) method. The first example in the fromJSON documentation shows computing the json syntax list as a string in the output of one job and using it in the matrix strategy in the next job.

    That example shows using the output computed in a job. I have tried it with a workflow dispatch input, like the following from your question:

    on:
      workflow_dispatch:
        inputs:
          products:
            description: "List of Products"
            default: '["yellow"]'
          limits:
            description: "List of limits"
            default: '[50]'
    
    jobs:
      build:
        runs-on: ubuntu-latest
        strategy:
          matrix:
            product: ${{ fromJSON(github.event.inputs.products)}}
            limits: ${{ fromJSON(github.event.inputs.limits) }}