Github Actions:矩阵步骤后合并构件。

3

我在Github Actions工作流中有一个任务,它运行单元测试,然后将报告上传到Jira Xray。问题是测试步骤需要相当长的时间才能完成,因此我想使用矩阵将任务执行拆分成几个较小的块。

我已经为代码检查做到了这一点,并且效果很好,但是对于单元测试,我正在努力想出如何收集和合并所有报告,以便在所有矩阵步骤完成后上传。

以下是当前单元测试步骤的外观

 unit-test:
    runs-on: ubuntu-latest
    needs: setup
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - uses: actions/cache@v3
        with:
          path: ${{ env.CACHE_NODE_MODULES_PATH }}
          key: build-${{ hashFiles('**/package-lock.json') }}
      - run: npx nx affected:test --parallel=3 --base=${{ env.BASE_REF}} --head=HEAD # actual unit tests
      - name: Check file existence #checking whether there're reports at all
        if: success() || failure()
        id: check_files
        uses: andstor/file-existence-action@v1
        with:
      # all reports will be placed in this directory
      # for matrix job reports will be separated between agents, so it's required to merge them
          files: 'reports/**/test-*.xml' 
      - name: Import results to Xray
        if: (success() || failure()) && steps.check_files.outputs.files_exists == 'true' && github.event_name == 'push'
        uses: mikepenz/xray-action@v2
        with:
          username: ${{ secrets.XRAY_CLIENT_ID }}
          password: ${{ secrets.XRAY_CLIENT_SECRET }}
          testFormat: 'junit'
          testPaths: 'reports/**/test-*.xml' # that's where I need to grab all reports
          projectKey: 'MY_KEY'
          combineInSingleTestExec: true

针对代码检查的矩阵作业看起来是这样的。我想为单元测试做同样的事情,但同时我希望像上面的作业一样收集所有报告。

linting:
    runs-on: ubuntu-latest
    needs: [setup]
    strategy:
      matrix:
        step: ${{ fromJson(needs.setup.outputs.lint-bins) }} # this will be something like [1,2,3,4]
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - uses: actions/cache@v3
        with:
          path: ${{ env.CACHE_NODE_MODULES_PATH }}
          key: build-${{ hashFiles('**/package-lock.json') }}
      # some nodejs logic to run few jobs, it uses "execSync" from "child_process" to invoke the task
      - run: node scripts/ci-run-many.mjs --target=lint --outputTarget=execute --partNumber=${{ matrix.step }} --base=${{ env.BASE_REF}} --head=HEAD 
1个回答

3
我自己搞定了。
  unit-test:
    runs-on: ubuntu-latest
    needs: [setup]
    strategy:
      fail-fast: false
      matrix:
        step: ${{ fromJson(needs.setup.outputs.unit-test-bins) }}
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - uses: actions/cache@v3
        with:
          path: ${{ env.CACHE_NODE_MODULES_PATH }}
          key: build-${{ hashFiles('**/package-lock.json') }}
      - run: node scripts/ci-run-many.mjs --target=test --outputTarget=execute --partNumber=${{ matrix.step }} --base=${{ env.BASE_REF}} --head=HEAD
      - name: Upload reports' artifacts 
        if: success() || failure()
        uses: actions/upload-artifact@v3
        with:
          name: ${{ env.RUN_UNIQUE_ID }}_artifact_${{ matrix.step }}
          if-no-files-found: ignore
          path: reports
          retention-days: 1

  process-test-data:
    runs-on: ubuntu-latest
    needs: unit-test
    if: success() || failure()
    steps:
      - uses: actions/checkout@v3

      - name: Download reports' artifacts
        uses: actions/download-artifact@v3
        with:
          path: downloaded_artifacts
          
      - name: Place reports' artifacts
        run: rsync -av downloaded_artifacts/*/*/ unit_test_reports/
      - name: Check reports existence
        id: check_files
        uses: andstor/file-existence-action@v1
        with:
          files: 'unit_test_reports/**/test-*.xml'
      - name: Import results to Xray
        run: ls -R unit_test_reports/

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接