如何在 Github 工作流中跨多个作业重用策略矩阵

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

我想避免跨工作重复策略矩阵:

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:
      …

这可能吗(无需创建作业来将矩阵创建为 JSON 本身)?

有一个关于此特定用例的 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,操作的作者回复了

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.

这表明像这样的工作流程可能会满足您的需求:

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: ...

我已经简化了您需要做的事情,但我猜您可能想要上传名称反映其适用矩阵选项的资产。对于这个细节,我建议在您的工作中添加一个明确的步骤来创建资产的文件名并将其添加到工作环境中,这有点类似于我所做的 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

然后,当您的构建步骤生成您的资产时,您可以使用 ${{env.BINARY_NAME}} 中的文件名命名它们,并将相同的名称传递给发布创建步骤,就像我在资产发布步骤中所做的那样 here.