How to reuse a strategy matrix across several jobs in Github workflows

Viewed 1243

I would like to avoid repeating a strategy matrix across jobs:

jobs:
  build-sdk:

    runs-on: macOS-latest
    strategy:
      fail-fast: false
      matrix:
        qt-version: ['5.15.1']
        ios-deployment-architecture: ['arm64', 'x86_64']
        ios-deployment-target: '12.0'


    steps:
      …

  create-release:
    needs: build-sdk
    runs-on: macOS-latest
    steps:
      …


  publish-sdk:
    needs: [build-sdk, create-release]
    runs-on: macOS-latest
    strategy:
      fail-fast: false
       matrix: ?????

    steps:
      …

Is this possible (without creating a job to create the matrix as JSON itself)?

1 Answers

There's an action that allows uploading multiple assets to the same release from a matrix build that's triggered on push to a tag. Someone filed an issue about this specific use-case, and the action's author responded with

Assets are uploaded for the GitHub release associated with the same tag so as long as the this action is run in a workflow run for the same tag all assets should get added to the same GitHub release.

This suggests that a workflow like this would probably meet your needs:

on:
  push:
    tags:
      - 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10

jobs:
  release:

    runs-on: macOS-latest
    strategy:
      fail-fast: false
      matrix:
        qt-version: ['5.15.1']
        ios-deployment-architecture: ['arm64', 'x86_64']
        ios-deployment-target: '12.0'


    steps:
      - name: build SDK
        run: ...
      - name: Create Release
        uses: softprops/action-gh-release@v1
        with:
          files: |
            - "SDK_file1" # created in previous build step
            - "SDK_file2"
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          GITHUB_REPOSITORY: username/reponame
      - name: publish SDK
        run: ...

I've simplified what you would need to do, but I'm guessing you might be wanting to upload assets with names reflecting their applicable matrix options. For that detail, I recommend adding an explicit step in your job to create the asset's filename and add it to the job environment, somewhat similar to what I've done here:

      - name: Name asset
        run: |
          BINARY_NAME=sdk-qt${{matrix.qt-version}}-iOS${{matrix.ios-deployment-target}}-${{matrix.ios-deployment-architecture}}
          echo "BINARY_NAME=$BINARY_NAME" >> $GITHUB_ENV

Then, when your build step generates your assets, you can name them with the filename in ${{env.BINARY_NAME}}, and pass that same name to the release creation step like I've done in my asset release step here.

Related