Include strategy matrix property based on existing matrix nested object

I have a general list of objects which I would like to reuse across environments. This list of objects is used as the strategy matrix in e.g. a deploy-development job.

[
  {
    "name": "name-1"
  },
  {
    "name": "name-2"
  },
  {
    "name": "name-3"
  },
  {
    "name": "name-4"
  },
  {
    "name": "name-5"
  }
]

In the deploy-development job, I need to have a property added to the matrix which is dependent on the entry of my matrix’s current value.
(note that the matrix is being build and passed into this step from a previous step, so I can reuse the same matrix across jobs)

deploy-development:
    runs-on: ubuntu-latest
    if: github.event_name == 'push'
    needs: matrix-builder
    strategy:
      fail-fast: false
      matrix:
        matrix-entries: ${{ fromJson(needs.matrix-builder.outputs.matrix) }}
        include:
        - matrix-entries: { name: name-1 }
          new_property: new_property_value
    environment: development
    steps:
    - name: Checkout repository
      uses: actions/checkout@v2

What I’m trying to accomplish here is to add a new property (new_property) with a given value (new_property_value), when my matrix entry’s name is name-1.

The reason I need this functionality, is because there is values which is specific for the matrix entry and the environment combined.

Surprise surprise, I actually didn’t check this current way of doing it, and it actually works ^^’.
I think the include checks if the matrix entry has a property called name, and if it’s the same value. If so, the new_property is added.

If anyone was wondering about sharing the same matrix across jobs, I used this way of doing it.

If that link somehow dies in the future, here is the job that does it:

matrix-builder:
    runs-on: ubuntu-latest
    if: github.event_name == 'push'
    outputs:
      matrix: ${{ steps.set-matrix.outputs.matrix }}
    steps:
    - uses: actions/checkout@v2
    - name: Assign the matrix from matrix.json
      id: set-matrix
      run: |
        TASKS=$(echo $(cat .github/workflows/domain-matrix.json) | sed 's/ //g' )
        echo "::set-output name=matrix::$TASKS"

It retrieves the json file and appends it as an output variable. Whenever you need to consume it, run ${{ fromJson(needs.matrix-builder.outputs.matrix }} as seen in the original post.

FWIW, the documentation has an example of sharing matrices between jobs: Expressions - GitHub Docs

1 Like