【问题标题】:GitHub Actions Environment variables for matrix-based testingGitHub Actions 用于基于矩阵的测试的环境变量
【发布时间】:2023-02-02 05:20:54
【问题描述】:

我正在努力应对 GitHub Action Workflows 如何定义运行时变量的最新变化,replacing the "set-ouput" approach with environment variables

去年夏天,我花了几个小时才弄清楚下面的代码是否符合我的要求。定义操作系统和 python 版本矩阵,以便 CI 工作流可以创建相应的环境并在其上运行 pytests。

是否有机会获得有关如何最好地转换为新方法的支持?

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) }}

【问题讨论】:

    标签: json yaml github-actions


    【解决方案1】:

    同时,可以使用更优雅的方式使用矩阵关键字函数。我的新实现更加简洁,最重要的是,避免了上述将环境变量打印到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 组合中执行。

    【讨论】:

      猜你喜欢
      • 2014-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-13
      • 2022-10-23
      • 1970-01-01
      相关资源
      最近更新 更多