json GitHub操作基于矩阵测试的环境变量

uxhixvfz  于 2023-02-06  发布在  Git
关注(0)|答案(1)|浏览(149)

最近GitHub Action Workflows定义运行时变量replacing the "set-ouput" approach with environment variables的方式发生了变化,我对此感到很纠结。
去年夏天,我花了几个小时才弄明白下面的代码是否能满足我的需求。定义一个操作系统和python版本的矩阵,以便CI工作流可以创建相应的环境并在其上运行pytest。
是否有机会获得关于如何最好地转换到新方法的支持?

env:
  # JSON variables (used in our strategy/matrix)
  SUPPORTED_PYTHON_VERSIONS: '\"python-version\":[\"3.8\", \"3.9\"]'
  SUPPORTED_OPERATING_SYSTEMS: '\"os\":[\"ubuntu-latest\", \"macos-latest\", \"windows-latest\"]'

jobs:
  # The set-env job translates the json variables to a usable format for the workflow specifications.
  set-env:
    runs-on: ubuntu-latest
    outputs:
      py-os-matrix: ${{ steps.set-matrix-vars.outputs.py-os-matrix }}
      # ^ this represents:
      # matrix:
      #   - os: [ubuntu-latest, ...]
      #   - python-version: [3.7, ...]
      os-matrix: ${{ steps.set-matrix-vars.outputs.os-matrix }}
      # ^ this represents:
      # matrix:
      #   - os: [ubuntu-latest, ...]
    steps:
      - id: set-matrix-vars
        run: |
          echo "::set-output name=py-os-matrix::{${{ env.SUPPORTED_PYTHON_VERSIONS }},${{ env.SUPPORTED_OPERATING_SYSTEMS }}}"
          echo "::set-output name=os-matrix::{${{ env.SUPPORTED_OPERATING_SYSTEMS }}}"

  test-run:
    name: test on ${{ matrix.os }} - ${{ matrix.python-version }}
    needs: set-env
    strategy:
      fail-fast: true
      matrix: ${{ fromJson(needs.set-env.outputs.py-os-matrix) }}
uyto3xhc

uyto3xhc1#

同时,有一种更优雅的方法可以使用matrix关键字函数,我的新实现要干净得多,最重要的是,避免了上面提到的将env变量打印到stdout的过时函数,然而,如何集成python版本〉=3.10 ...

jobs:
  test-run:
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest]
        python-version: [3.8, 3.9]
    defaults:
      run:
        shell: bash
    runs-on: ${{ matrix.os }}
    steps:
      - name: Check out repository code
        uses: actions/checkout@v3
      - name: Install python
        id: setup-python
        uses: actions/setup-python@v4
        with:
          python-version: ${{ matrix.python-version }}
          check-latest: true

setup-python之后的每一步都在当前选定的操作系统和python组合中执行。

相关问题