Use custom_payload to generate matrix

We want to set matrix dynamicaly, based on some conditions. 

This code:

on:
  repository_dispatch

jobs:
  distribute-codepush-update:
    strategy:
      matrix:
        platform: ${{ github.event.client_payload.platform }}
        env: ${{ github.event.client_payload.env }}
    runs-on: ubuntu-latest
    name: Distribute ${{ matrix.platform }} ${{ matrix.env }} CodePush update
    timeout-minutes: 20

returns this error:

As we can see it is possible to set matrix variables using github.event.client_payload.platform and  it starts 4 jobs and inserts values in the <job>.name properly, but for some reason it is not starting.

1 Like

Hi @kerizer ,

I can repro the same on my side. Currently it’s not supported to set dynamic matrix value, you have to set the exact value for matrix. It’s answered in the similar ticket.

Thanks.

But it’s clear that it starts 4 jobs and use correct values in \<job\>.name, so, whould it be possible to set them dynamically eventually? With current implementation we should either use matrix and fail unwanted jobs on first step or copy-paste same job 4 times (which is a bit hard to support, taking in considararation that we have 5 repos with same workflows and more are coming, so it’s 4*5).

What does your payload look like?  Are you trying to pass a delimited string or is it a proper JSON array? 

Here is an example that works for me.

Workflow that triggers the repositor_dispatch

# This is a basic workflow to help you get started with Actions

name: repo-dispatch

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
  push:
    branches: [master]
  pull_request:
    branches: [master]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  trigger:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      - name: Repository Dispatch
        uses: peter-evans/repository-dispatch@v1
        with:
          token: ${{ secrets.REPO_ACCESS_TOKEN }}
          event-type: 'matrix-ci'
          client-payload: '{"images": ["ubuntu-latest", "windows-latest"] }'

Workflow that recieves it

name: .NET Core. CI 1

on:
  push:
    branches: [master]
  pull_request:
    branches: [master]
  repository_dispatch:
    types: [matrix-ci]

jobs:
  build:
    if: github.event != 'repository_dispatch'
    runs-on: ${{ matrix.image }}
    strategy:
      matrix:
        image: ['ubuntu-latest', 'windows-latest', 'macos-latest']

    steps:
    - uses: actions/checkout@v2
    - name: Setup .NET Core
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 3.1.101
    - name: Build with dotnet
      run: dotnet build --configuration Release

  build-2:
    if: github.event == 'repository_dispatch'
    runs-on: ${{ matrix.image }}
    strategy:
      matrix:
        image: ${{ github.event.client_payload.images }}

    steps:
    - uses: actions/checkout@v2
    - name: Setup .NET Core
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 3.1.101
    - name: Build with dotnet
      run: dotnet build --configuration Release

Successfule run

https://github.com/chrispat/multi-ci-test/actions/runs/68415548

Unless I’m reading it wrong, it looks like that build was run on build rather than build-2. It also ran on macos-latest which your trigger job does not specify.

Hi @kerizer @bo98 ,

Now it looks as fixed. Please check my workflow: https://github.com/weide-zhou/ticket17/actions/runs/69528670

Please check your workflow again, thanks.

2 Likes

I cleaned up the example a little so I don’t have so many duplicate things on the same commit.

This one clearly ran only two jobs https://github.com/chrispat/multi-ci-test/actions/runs/69926570.

That looks awesome! Thank you :slight_smile: